1

I'm new to AppleScript and Automator. I have explored the different threads at Apple but unable to find something useful.

Well, my problem is that I have a folder i.e "work" I have applied folder action on it. On creation of new folder at "work" i.e "abc" I want to create three new folders in "abc". Check out my code

on adding folder items to this_folder after receiving added_items
  tell application Finder
    repeat with this_item in added_items
        make new folder at this_item with properties {name:"Main"}
        make new folder at this_item with properties {name:"Lower"}
        make new folder at this_item with properties {name:"Upper"}
   end repeat
  end tell
end adding folder items to
1

The first thing I see wrong in your code is tell application Finder, as Finder in this case is a variable, since it's not double-quoted. It should be: tell application "Finder"

That said, you have as Tags, macos, mac, applescript and automator and did not state in the OP where and how you've applied the code to the target folder, this_folder, as a Folder Action. In other words, if you've created the Folder Action in Automator and have just that code as the code in a Run AppleScript action, even with the double-quotes around Finder, it will fail. That coding structure is only valid as an AppleScript .scpt file assigned to a Folder Action using Folder Actions Setup... on the Services section of the contact menu in Finder.

The following example code is how I'd write it as an AppleScript .scpt file assigned to a Folder Action using Folder Actions Setup... on the Services section of the contact menu in Finder.

on adding folder items to this_folder after receiving added_items
    try
        tell application "Finder"
            repeat with this_item in added_items
                if (class in (get properties of this_item) as string) is "«class cfol»" then
                    make new folder at this_item with properties {name:"Main"}
                    make new folder at this_item with properties {name:"Lower"}
                    make new folder at this_item with properties {name:"Upper"}
                end if
            end repeat
        end tell
    end try
end adding folder items to

Note that even though by using the try statement, it should eat any errors, nonetheless, I've also added an if statement to make sure the item being acted upon is a folder, not a file. Note that while the if statement is not absolutely necessary, nonetheless, it's a more proper way to write code in my opinion, however, it could be left out if you want.

User's AppleScript .scpt files for Folder Actions should be saved to, e.g.:

/Users/me/Library/Scripts/Folder Action Scripts/filename.scpt

If you are creating the Folder Action via Automator, then in the Run AppleScript action use the following example code_:

on run {input, parameters}
    try
        tell application "Finder"
            repeat with this_item in input
                if (class in (get properties of this_item) as string) is "folder" then
                    make new folder at this_item with properties {name:"Main"}
                    make new folder at this_item with properties {name:"Lower"}
                    make new folder at this_item with properties {name:"Upper"}
                end if
            end repeat
        end tell
    end try
end run

Note that I have tested both example code blocks under the conditions stated above and it works as coded.

  • i have a folder in documents named "workdir" i applied folder action on it and in the workflow dragged the Run AppleScript utility copied your code and saved it but it is not working when creating a new folder in "workdir" – Breathe Hacker Aug 11 '17 at 5:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .