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.
You can also watch this video at YouTube.
Watch more videos about related subjects: Automator (50 videos).
You can also watch this video at YouTube.
Watch more videos about related subjects: Automator (50 videos).
Video Transcript
Hi, this is Gary with MacMost.com. Today let me show you how to use Automator to create a folder action to automatically move files once they are placed into a drop-zone folder.
MacMost is brought to you thanks to a great group of more than 750 supporters. Go to MacMost.com/patreon. There you can read more about the Patreon Campaign. Join us and get exclusive content and course discounts.
So you can use Automator to create something called a Folder Action. This is an Automator workflow that is triggered when you put a file into a folder. One of the things that you can do with that is automatically have that file move from that folder into another folder. So you can create a drop-zone folder. A folder where you can put files in it and it automatically looks at them and figures out where they need to go based on something, like say the file name.
So here I'm in Automator and I'm going to create a folder action. I already have a folder on the desktop called Drop. There's nothing in it. It doesn't have to be on the desktop. You could put this folder in your Documents folder. Anywhere you want. But to make it easy I'll just have it here on the desktop so it's ready for me to drop files into it. When I create a Folder Action I get to choose, at the top, Folder Action receives files and folders added to and then choose the folder. So I'm going to choose Other and go to the desktop and choose this Drop folder.
Now it's time to create the Workflow. Now first let's do it using just Automator actions. So I'm going to do Filter and use Filter Finder Items and drag that over and say when the Name contains and let's say the words daily report. So if you have a file called Daily Report, maybe you get it emailed to you all the time or something, you can drop it in here and something will be done with that file. So now we're going to look for Move and we have Move Finder Items. Now we're going to specify where it gets moved to. So I'm going to choose Other and then go to the Documents folder and there I have a folder called Daily Reports. So I'll choose that one.
So this is all pretty simply. I'm going to Save this and give it the name Drop Zone Organizer. I'll give it the proper permissions and now I'm ready to try this out. So I'm going to create this little simple test file here in TextEdit. I'm going to save it to the desktop and call it Daily Report 1 and Save it. We'll move this into the Drop Zone and we'll see what happens. Here I've got Daily Reports and I could see there's nothing in it. If I move this into the folder called Drop normally it will just stay there. But our folder actions should look at it, figure out that the name has Daily Report in it, and move it automatically. So I'll move it there and then wait a second. You could see it moved it into Daily Reports. If I look in the Drop Zone folder you could see it's empty. It will only do that if the name has Daily Report in it because that's what we put in the actions. So if I had put something else in there it doesn't move it and if I look in the Drop folder it just stays there.
Now what if we wanted to have more of an organizer that looks for various different things, not just Daily Report. Well we could create several of these. So I could create Drop Zone Organizer 1 or Drop Zone Organizer Daily Report and then create another one Drop Zone Organizer TPS Report and Drop Zone Organizer Zip Files and keep adding these automated workflows to this folder as Folder Actions. I could also create one long workflow here that takes the input from this action here and puts it into a variable and then gets the variable, filters it, moves it, gets the variable again, filters it, moves it and repeats those three actions over and over again for every variation.
But instead of doing that let's use a shell script instead. So I've cleared this out and instead I'm going to look for shell and move Run Shell Script here. I'm going to stick with the z shell which is the default for maxnow. Next, this is very important, I'm going to change Pass Input to as arguments. Make sure you do that. That's very important else you won't be able to get the input into the script here in the way that I'm doing it. Let's expand this a bit and you could see it puts a little script in here. It loops through all of the pieces of input and puts them into a variable called f. Then is just outputs that variable. We don't want to do that. We want to actually process this.
So let's test the file name. It's actually the entire path to the file for the characters Daily Report. I'm going to do that with this line here. It's going to be an IF statement. I put double square brackets around it and I test this f variable, you have to put the dollar sign before it when you're actually using the variable, two equal signs to test for equality, and then I'm looking for these characters and I could have anything before it and anything after it. Now if that's true then what I want to do is move the file. So I'm going to use the mv command and then dollar sign f so now we want to get the path to the destination folder. So we can do that by simply dragging and dropping that folder into the script. So I'll take Daily Reports here and drag that in and put it right there. You can see it inserts the path without us having to type it.
Now since we have a space here we want to make sure we put quotes around the full path. Then we want to end the IF statement here. Instead of IF we do FI, the opposite that's how it works in shell scripts like that. Now we're ready to go. So let's give it a try. Let's Save and then we'll watch the Daily Reports folder there as we drop this into the Drop Folder. We can see that it moved it in. So that Shell Script worked.
The idea of using a shell script is now it's a little easier for us to customize this and add more options. So let's set this up as a proper script here. Instead of doing each comparison and then doing a move command I'm going to set a destination variable. So we'll use capital letters here as they do a lot in shell scripts. Set the DEST equal to nothing, just empty quotes right now. Then we'll look to see if Daily Report is part of the title. If it is we'll set the DEST equal to this folder. Now we can easily add other conditions to this. So I'm going to add an else to this IF statement. The way to do that in shell script is elif. Then we can take a condition just like this one but let's change it from Daily Report to TPS Report. Now let's have a then and a new destination for that. So I'll copy this and paste it in here. The DEST is very similar. It's just tps reports instead of Daily Report. You can see the folder there.
Let's go and create another one. So I'm going to Copy and Paste the entire things but instead of looking for something in the name let's look for the file extension .tst. This time let's move it to a different destination folder. I'm going to clear out what's in the quotes here. Go to the Finder and drag and drop this folder in here so I make sure I get the folder right. You can see it puts it in there. Let me make sure the double quotes are actually around that. Great. Now the destination variable will be set to something other than a blank string if one of these three conditions is met.
Let's do another IF statement here and this time let's test DEST and make sure is not equal to blank and if it's not equal to blank we're going to move it. So we'll do then and we'll do mv dollar sign f, the original file in its original location, to dollar sign DEST, the new location. Then we can end with the fi. This will work but let's go one step further and add notification.
You can easily add a notification to this by triggering a bit of Apple Script. So I'm going to have the notification appear here just before the move. It's going to use osascript dash e to trigger the Apple Script. In quotes here is the Apple Script and the Apple Script is display notification. Now we have to put what notifications display inside of quotes. You can't put quotes inside of other quotes so we use backslash quote to have the interior quotes in there. We're going to say Moved and then the location and file name and then destination. So that should put out a notification.
But let's go even further than that and have a notification if the file isn't moved. So if it doesn't meet one of those conditions and it's going to sit there in the Drop folder then let's do an elif then and let's do the same thing here but we're going to modify it and say dollar sign f and instead of displaying a destination we'll say not moved period. Now we'll save the and we'll try it out.
Here I'm in the Finder in List View. I have Daily Report opened up. Got Text Files opened up and TPS Reports opened up. So I'll be able to see if any file appears in those. Drag and drop Daily Report and you could see here I get a notification and the file goes there. Let's drag and drop both of these at the same time in here. Now that's not going to work great with the notifications. It's not going to show us both. But you can see it does actually move both files. Now what happens if we have a file that doesn't meet any of those conditions. Drop it in here and I get a notification saying that the file wasn't moved.
So now you can continue to customize this script looking for things in the file name or the extension and putting them in the folders you want. Remember the drop folder doesn't need to be named drop. It can be named anything you want. It can be anywhere you want. You can even assign this folder action to an existing folder. Like say your Downloads folder. So when you download say your Daily Report it automatically will move from the downloads folder to the proper folder.
If you ever want to see the folder actions assigned to a folder you can Control click on them, go to Services and select Folder Action Setup, then run Service if it prompts you for that. Then you can see a list of the folder actions and even go right to the Workflow itself to edit in case you've forgotten where the folder actions are stored. You could even temporarily disable it if you want.
Another thing you can do is this will work even if you're creating files on the fly. So for instance I'm going to start typing something here and say oh this is going to be a daily report. So I'm going to Save it and I'm going to save it in the Drop Zone and I'm going to call it Daily Report 2. Now I'm going to hit Save and watch what happens. I saved it here but it's going to trigger the folder action and then it's going to move it to Daily Reports. Now you would think that might be a problem since I'm actually working on this file but if I Control click here you can see TextEdit knows that the new location of the file is in Daily Reports. I could continue typing stuff here and it will work just find even though I never actually saved it to Daily Reports. I could close it and reopen it again or anything like that.
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
Shouldn’t there be 'then' after every elif?
Sumanth: Not in a shell script. Try it and see.
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!
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.
hunter: Hard for me to guess. Check and double-check everything to make sure it is exactly like I have in the video.
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
Manuel: Not sure. Try else instead of elif. Experiment. Play around with it.
elseif works but still no moving of the files...
Larissa
I have the same issue (Big Sur). elif has error, replace with elsie and it reports no error but doesn't move any files.
I was getting parse errors until I deleted the notification lines.
Now there are no errors, but files are not moving.
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