TransWikia.com

How to get folder path in Automator Folder Action?

Ask Different Asked by peetonn on September 4, 2020

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.

2 Answers

Personally, I find that creating Folder Actions directly in Script Editor.app, gives me much more flexibility. Once I save the document as a .scpt file into the following folder “/Users/YOUR SHORT NAME/Library/Workflows/Applications/Folder Actions/“, your new Folder Action will be available systemwide and you will then be able to attach your new folder action to any folder using the Folder Actions Setup.app.

This following AppleScript Folder Action code will store the full path of the folder the Folder Action is attached to,, in the variable theContainingFolder

property theContainingFolder : missing value
property theMoveToFolder : path to documents folder -- Define Your Own Temporary Move To Folder
property movedFiles : missing value
   
on adding folder items to theFolder after receiving theNewItems
    --  Called after items have been added to a folder
    --
    --  theFolder is a reference to the modified folder
    --  theNewItems is a list of references to the items added to the folder 
    
    tell application "Finder"
        set theContainingFolder to container of item 1 of theNewItems as alias
        -- Move And Process Your Files
        set movedFiles to move theNewItems to theMoveToFolder
        
        -- Continue Processing Files Before Moving Them Back To Original Folder
        
        -- Move Processed Files Back To The Original Folder Action Attached Folder      
        move movedFiles to theContainingFolder
    end tell
end adding folder items to

Here is the AppleScript code for the Automator workflow.

on run {input, parameters}
    tell application "Finder"
        set theContainingFolder to container of (item 1 of input) as alias
    end tell
    return theContainingFolder --as text
end run

Using this return theContainingFolder as the last line of the AppleScript code, the “Containing Folder” , stores the variable as a Posix Path. (As shown in the image below)

enter image description here

Using this return theContainingFolder as text as the last line of the AppleScript code, the “Containing Folder” , stores the variable as an HFS Path… which is the same as the path as an alias but without the word “alias”(As shown in the image below)

enter image description here

Answered by wch1zpink on September 4, 2020

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.)

Answered by Wowfunhappy on September 4, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP