Hazels' "Move --> to folder:" as Apple/Shell Script

Hello,
I like to implement Hazel's "Move to folder:"-command as an external Apple/Shell Script, which I can run inside Hazel ("Run shell/apple script --> other..."). Reason: I can change the destination folder from several (50 - 100) rules at once by adjusting the path in the external script.
The following Apple Script is doing well, but with errors if the file exists already in the destination folder. So I have some problems to code the "If file exists: rename the file"-option from Hazel, and I'am not sure if this is possible with Apple Script or if it's better to code this with a Shell Script:
Apple Script without renaming:
In others forums I have found the following two Shell Scripts with renaming-the-file-if-it-exists-in-the-destination-folder, but I could not adapt the scripts for my purpose. I'am struggling primarily with the $1 and the file-name and file-extension in Hazel's Shell Script area and the source/destination folder...
Perhaps somebody has some suggestions? Thanks in advance!
Shell Script No1 with renaming:
Shell Script No 2 with renaming:
I like to implement Hazel's "Move to folder:"-command as an external Apple/Shell Script, which I can run inside Hazel ("Run shell/apple script --> other..."). Reason: I can change the destination folder from several (50 - 100) rules at once by adjusting the path in the external script.
The following Apple Script is doing well, but with errors if the file exists already in the destination folder. So I have some problems to code the "If file exists: rename the file"-option from Hazel, and I'am not sure if this is possible with Apple Script or if it's better to code this with a Shell Script:
Apple Script without renaming:
- Code: Select all
on hazelProcessFile(theFile)
set DestinationFolder to POSIX file "/Volumes/Data/Business/Bill" as alias
tell application "Finder"
move theFile to DestinationFolder
end tell
end hazelProcessFile
In others forums I have found the following two Shell Scripts with renaming-the-file-if-it-exists-in-the-destination-folder, but I could not adapt the scripts for my purpose. I'am struggling primarily with the $1 and the file-name and file-extension in Hazel's Shell Script area and the source/destination folder...
Perhaps somebody has some suggestions? Thanks in advance!
Shell Script No1 with renaming:
- Code: Select all
source=dirA/file.ext
dest_dir=dirB
file=$(basename file.ext)
basename=${file%.*}
ext=${file##*.}
if [[ ! -e "$dest_dir/$basename.$ext" ]]; then
# file does not exist in the destination directory
mv "$source" "$dest_dir"
else
num=2
while [[ -e "$dest_dir/$basename$num.$ext" ]]; do
(( num++ ))
done
mv "$source" "$dest_dir/$basename$num.$ext"
fi
Shell Script No 2 with renaming:
- Code: Select all
#!/bin/bash
source=$1
dest=$2
file=$(basename $source)
basename=${file%.*}
ext=${file##*.}
if [[! -e "$dest/$basename.$ext" ]]; then
mv "$source" "$dest"
else
num=1
while [[ -e "$dest/$basename$num.$ext" ]]; do
(( num++ ))
done
mv "$source" "$dest/$basename$num.$ext"
fi