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

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

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:
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
Tätä
 
Posts: 7
Joined: Fri Mar 04, 2016 6:45 am

Hazel does not send in $2. That would be the destination folder which you wanted to be able to specify in the script.
Mr_Noodle
Site Admin
 
Posts: 11872
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Thank you Mr. Noodle for your quick response,

Well, in the moment I have no idea how to specify $2 in the script (same with $1 and so forth), so I have to do some google search for this :D ... or somebody has some advice for me.
Tätä
 
Posts: 7
Joined: Fri Mar 04, 2016 6:45 am

$1 is the only thing you should reference. That is the full path of the file sent in by Hazel. You don't need $2. The whole purpose of the script is for you to specify the destination, right? So there's no need to have that sent in (and Hazel doesn't send that in anyways).
Mr_Noodle
Site Admin
 
Posts: 11872
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Ahh, ok, so I will try to code this in a appropriate manner.
Tätä
 
Posts: 7
Joined: Fri Mar 04, 2016 6:45 am


Return to Support