Automatically Organize Files With an Automator Folder Action Shell Script

By using a Shell script in an Automator Folder Action you can create a "drop zone" folder that moves any file placed into it. By using a Shell script it is easy to add new conditions and destination folders.



Here’s the script I’m using in the video:

for f in "$@"
do
	DEST=""

	if [[ $f == *"dailyreport"* ]]
	then
		DEST="/Users/macmost/Documents/Daily Reports"
	elif [[ $f == *"tpsreport"* ]]
		DEST="/Users/macmost/Documents/TPS Reports"
	elif [[ $f == *".txt" ]]
		DEST="/Users/macmost/Documents/Text Files"
	fi

	if [[ $DEST != "" ]]
	then
		osascript -e "display notification \"Moved $f to $DEST\""
		mv $f $DEST
	elif
		osascript -e "display notification \"$f Not Moved.\""
	fi
done

Comments: 11 Responses to “Automatically Organize Files With an Automator Folder Action Shell Script”

    Sumanth Kumar
    4 years ago

    Shouldn’t there be 'then' after every elif?

    4 years ago

    Sumanth: Not in a shell script. Try it and see.

    Lachlan
    4 years ago

    Hi, this was really helpful - I'm a very amateur coder and have never done shell script before. The one bit of feedback I have is that I couldn't get this to work without adding a 'then' after every 'elif'. That was the case with /bin/zsh and /bin/bash (the default) whatever those mean... Otherwise, really useful and has got me started. All I need now is to understand more conditional arguments such as AND, OR etc. Also, would be really helpful to understand how to add tags to files! Thanks!

    hunter
    4 years ago

    so when I run this in either zsh, bash, or sh. I get error near fi. what's the issue? I tried adding ; after fi but still not working.

    4 years ago

    hunter: Hard for me to guess. Check and double-check everything to make sure it is exactly like I have in the video.

    Manuel
    4 years ago

    Hey Gary,

    Is it possible that they changed something in BigSur? I get the same error like hunter. I tried both options, because in the video you use "then" after elif, and in the script above you don't, but neither option is working.

    What's more interesting, I tried only the first part of the script, it runs, but the files are not moved.

    for f in "$@"
    do
    DEST=""

    if [[ $f == *"dailyreport"* ]]
    then
    DEST="/Users/macmost/Documents/Daily Reports"
    fi
    done

    tnx, manuel

    4 years ago

    Manuel: Not sure. Try else instead of elif. Experiment. Play around with it.

    Larissa
    4 years ago

    elseif works but still no moving of the files...

    Larissa

    Joe
    4 years ago

    I have the same issue (Big Sur). elif has error, replace with elsie and it reports no error but doesn't move any files.

    David Sofi
    3 years ago

    I was getting parse errors until I deleted the notification lines.
    Now there are no errors, but files are not moving.

    Dinindu Udana
    3 years ago

    This code is working for me in Big Sur

    for f in "$@"
    do
    DEST=""

    if [[ $f == *"dailyreport"* ]]
    then
    DEST="/Users/dtdinidu7/Desktop/untitled folder/test/"
    elif [[ $f == *"daily"* ]]
    then
    DEST="/Users/dtdinidu7/Desktop/untitled folder/test/"
    fi

    if [[ $DEST != "" ]]
    then
    mv "$f" "$DEST"
    fi
    done

Comments Closed.