AppleScript – How to Batch Add Folders to Folder Actions

applescriptautomatorfolder-actionfolders

I actively manage a directory of folders that contain images that are organised by modification dates. Due to this, I had made a quick action that used an applescript and bash to help me ensure that newly added files had the right modification date.

This was time consuming, and eventually I learned that I could set up Folder Actions that could automatically do that work for me. I did so, and now I face the struggle of adding multiple (lots of) folders to the Folder Actions Setup window.

In the spirit of automating everything, is there a way to add Folders to the Folder Actions Setup window and all added folders to have my folder action enabled? Not sure where to start!

There was an app made for this, unfortunately the download link no longer works and the developer has removed it from his page of projects.

Best Answer

This following AppleScript code will ask you to choose the script file to attach then we'll give you two separate options for adding multiple folders to attach that script file to

This works for me using the latest version of MacOS Mojave.

property pathToFolderActions : ((path to workflows folder as text) & "Applications:Folder Actions:")

tell application "Finder"
    set resourceName to (choose file with prompt ¬
        "Choose The Script File To Attach")
    set resourceName2 to name of resourceName
    set folderActionScript to (container of resourceName as text) & resourceName as string
    try
        duplicate resourceName to folder pathToFolderActions with replacing
    end try
end tell

activate
set theFolders to (choose folder with prompt ¬
    "Choose Folders" default location (path to desktop) ¬
    invisibles false ¬
    with multiple selections allowed)

activate
set chooseAgain to button returned of (display dialog ¬
    "Would You Like To Choose  Additional Folders?" buttons {"NO", "YES"} ¬
    default button "YES" with title ¬
    "Would You Like To Choose  Additional Folders?" with icon 1)

if chooseAgain is "YES" then
    activate
    set moreFolders to (choose folder with prompt ¬
        "Choose Folders" default location (path to desktop) ¬
        invisibles false ¬
        with multiple selections allowed)
    set theFolders to theFolders & moreFolders
end if

repeat with i from 1 to count of theFolders
    set thisItem to item i of theFolders
    tell application "Finder"
        set nameOfTriggerFolder to name of folder thisItem
        set attachFolderActionTo to (container of thisItem as text) & nameOfTriggerFolder
    end tell

    tell application "Folder Actions Setup"
        activate
        try
            set addedFolderAction to make new folder action with properties {name:nameOfTriggerFolder, path:attachFolderActionTo}
        end try
        try
            delay 0.5
            tell addedFolderAction to make new script with properties {name:resourceName2, path:(pathToFolderActions & resourceName2)}
        end try
        if not folder actions enabled then
            set folder actions enabled to true
        end if
    end tell
end repeat

delay 5
tell application "Folder Actions Setup" to quit