How to get folder path in Automator Folder Action

automatorfinderfolders

How do I get a path (or a name) of the folder an Automator Folder Action is attached to and save it into a variable?

My folder action is attached to Downloads, I copy newly dropped files into a temporary folder, process them and want to move them back into Downloads. I don't want to use hardcoded value with Move Finder Items as the folder action may be attached to different folders. Instead, I need folder action to be smart enough to move items back to the folder it is attached to.

Best Answer

I don't think you can get the folder an action is attached to, but I think a good analog would be to get the parent folder of the current file. So, the parent folder of a file in ~/Downloads/MyDownloadedFile.pdf will be the Downloads folder, and you could save that to a variable.

This action is not built in to Automator. However, if you are running macOS 10.13 or earlier, you can purchase a package of third-party Automator Actions from http://automatedwork.server323.com/products/apps/automator-actions-for-mac-os-x-10-610-7-snow-leopard-lion/. The action you want is "Get Parent Folders of Finder Items" in the "File and Folder Automator Action Pack". At the time that I bought it (a few months ago), the developer charged $20.


(You may also be able to accomplish this without third party actions by using an Applescript block—something like tell application "Finder" to return container of (input as alias). However, when I tried this myself, Automator seemed unable to save the variable properly. I'm not sure if that's due to a limitation of Automator or a problem with my Applescript.)