As a resolution for Summer 2018 I decided I’m going to try work on keeping my files better organized, and one thing I’ve committed to is keeping my Mac Desktop clean. I sorted and moved everything that had just been sitting there to a catch-all junk folder I named “Scratch” and added some Aliases to important folders I use every day. This has definitely been an improvement for my productivity, but how do I keep it this way?
Enter Automation
Automator is a powerful tool for Mac that you probably know about, but if you’re anything like me, probably forget you should use. In this post, I’m going to walk through setting up an Automator application which takes files on your desktop and moves them somewhere else.
On its own, this is a relatively simple task, so I’ve added a few detours. This application will also explore adding if
/ else
control structures to Automator using AppleScript, setting and retrieving variables, and how to transition between one automator results & input thread to another. Because of this, steps 4 & 5 and 6 & 7 are optional. Feel free to skip over them if they don’t suit your organizing methods.
1. New Application
Create a new application
2. Get Specified Finder Items
Because this is an app that will run on its own (as opposed to a droplet or folder action, for example) we need to start by using the Get Specified Finder Items
action, and use the Add...
button to select the Desktop folder (at ~/Desktop
) so we can pass it as a result on to the next action.
3. Get Folder Contents
Now that Automator knows which folder we want to start with, we need to tell it to select the items inside that folder and work with those. To do this, we’ll use the Get Folder Contents
action, which will return an object which contains a list of items for the next action to consume.
4. Filter Finder Items
Now, in my case there are a few things on my desktop that I don’t want to move. To achieve this, I’ll show you how to filter the list of desktop items being cleaned up. However, if you’re one of those people with the “show disks” options disabled in Finder preferences, and want a completely minimalist desktop view, you can skip both this and the next step.
My desktop contains Alias files (shortcuts) to folders I use every day, and I don’t want to remove those. I also keep my “Scratch” or junk folder on the desktop itself (at ~/Desktop/Scratch
) and I don’t want to move that either.
In order to remove those items from the list of items the app will move, I’ll use a Filter Finder Items
action, which takes a list of items as input and returns a result containing only the ones which match your criteria. In my case that’s:
All
of the following are true:Kind
is not
other
Alias
Name
is not
Scratch
Now my Automator results should only include the files I want to move.
Just a note: This action seems to take a lot longer than it should, typically 10-15 seconds for me. This also seems to have been reported by a handful of other users in support forums, but there weren’t any good explanations as to why, other than that this relies heavily on spotlight indexes which (with about 7TB of data to account for in my case) are probably slow to work with.
5. Deciding What To Do With The Results
At this point you could simply move the files listed in the filter results to your own catch-all junk folder or wherever, but I’m not here to keep things simple. I’ve decided to add a step where the items are placed into a subfolder with the current date on it. However we only need this to run if there are files being moved, which means we’re going to need some kind of control structure.
Automator is essentially designed to string actions together, and doesn’t have the capacity to perform conditional logic. So we’re going to have to include an AppleScript to decide what to do with the filter results. To do that, use the Run AppleScript
action, and add the following code:
on run {input}
# Determine if there are files to clean up
if (0 < (count (input))) then
# If files, do somethin' with it
return input
end if
# If there are no files to move, stop execution
error number -128
end run
This intention of this simple AppleScript does the following:
- Take the results from the previous action (Filter)
- Count the number of items in that list of results, and…
- IF the number of items is greater than zero…
- Pass those results on to the next Automator action.
If the condition for the number of items is not met, meaning the number of items is zero or somehow negative, the AppleScript does not return them to be passed onto the next action, and will continue on to an error number -128
notice, which should cause the entire application to quit silently.
6. Handling The Files We’ve Determined Should Be Moved
As I mentioned earlier, I’m going to be moving the items into a newly created subfolder inside ~/Desktop/Scratch
, which takes a few steps. If you don’t want to use subfolders, you can skip on down to the last step.
Now, because the default behaviour for creating a new folder is that if a list of items is passed into, it those items are copied into the new folder. Since we want these files to be moved off the desktop, we’ll need to find a way to create the subfolder first and then move the files.
So for now, we need to interrupt our current automator thread which has been passing along this list of items as a result since it started running. Since we still need to keep track of this list of items, we’ll store this list of items into an Automator variable using Set Value of Variable
. Choose New variable...
from the select menu, and instead of the default name, lets call this Items List
. This now lets us shift Automator’s focus to managing folders for the next few steps.
7. Create A Dated Destination Subfolder
At this point we know for sure (thanks to the conditional AppleScript) that there are items to clean up, so we can go ahead use the New Folder
action to create a destination subfolder for them.
Since my workflow plan is to use the current date for for the subfolder name, we’ll also need to add a variable for that. Under Variables
we’ll use the Today's date
variable format, and I’ve set a custom format of YYYYMMDD
using the custom format builder. You can now set this value as the name of the new folder. (Yes, there is also a New Dated Folder
action, but this uses a rule builder and I’m not a fan of it at all.)
Now, some very important points for this step. First, you need to open the Options
panel for this action, and select Ignore this action's input
, to prevent the default behaviour of simply copying the files in the results list to this new folder. And second, make sure the new folder is being created somewhere other than the desktop. In my case, I’m using a folder called ~/Desktop/Scratch
.
And finally, in order to use this newly created destination subfolder in the next step, you’ll also need to store the name / location as a variable, using another Set Value of Variable
action. Make sure you use the Variable:
select, and create a new variable so that you don’t overwrite one of the existing ones. In this case, I’ve called mine New Folder
.
8. Move Items Into Catch-All Folder
After all that, we’re ready to move the items which were selected from the desktop and filtered, into the newly created subfolder. This does involve a little bit more juggling of action results and variables but we’re finally ready to put it all together.
It’s time to utilize that Items List
variable, so we’ll need to retrieve it with a Get Value of Variable
action. And since we want just list list of items, and not the results from the previous action (the location of the subfolder), it’s important to select Ignore this action's input
here as well.
Finally, we can pass this list of items into a Move Finder Items
action. To use the location of the newly created dated subfolder, select the New Folder
variable and drag it onto the To:
select menu.
You should now be able to run the task and see all the stray files on your desktop get swept up into an out-of-your-way dated folder, but your Alias files will still be there on the Desktop when you need them.
Wrapping Up
Be sure to save this app using a name you’ll remember. My advice would be to run it using Spotlight (⌘+Space
), and typing the name of the application whenever your Desktop gets too cluttered, either right before or right after working on a big project, or you could use an iCalendar repeating task which runs the app on a regular schedule.
Notes
Development environment details, September 2018:
- MacBook Pro (Retina, 15-inch, Mid 2015)
- MacOS High Sierra, Version 10.13.3
- Automator, Version 2.8 (444.4)