Automator: Applescript to execute keystrokes for each nested folder in Finder

applescriptautomatorfinder

Help!!

I have put in many hours trying to solve this and feel it is time to get some help 🙂

I am trying to write an Applescript in Automator that will execute certain keystrokes in Finder for the parent folder and each sub-folder.
So far I can only get the keystrokes to be applied to the parent folder that is selected during the execution of the script; the sub-folders remain as they were without having had the keystrokes applied.

The keystrokes are specific to Finder and make the folder arranged by "Kind" and sorted by "Name".

Here is my code:

activate application "Finder"

tell application "Finder"
    set theFolders to every folder of (choose folder) as alias list
end tell

tell application "System Events"
    repeat with eachFolder in theFolders
        keystroke "2" using {control down, command down}
        keystroke "1" using {control down, option down, command down}
    end repeat
end tell

The code makes sense to me in my limited knowledge of Applescript, but obviously it is not working correctly.

Any advice?

Best Answer

If I understood what you want, i.e. select a folder and then it and every sub-folder within it, including sub-folders of subfolders etc., gets these setting applied.

In order to pass the keystrokes to the target folders each needs to be the active window in Finder first, then System Events can send the keystrokes.

So, I'd write the code as in the example below.

tell application "Finder"
    activate
    set parentFolder to (choose folder) as alias
    set theFolders to every folder of entire contents of parentFolder as alias list
    set theFolders to {parentFolder} & theFolders    -- # Adds the chosen folder to the list of folders to be acted upon.
    repeat with eachFolder in theFolders
        open eachFolder
        delay 0.5
        activate eachFolder    -- # This is done to ensure the target folder's window has focus before being acted upon.
        tell application "System Events"
            keystroke "2" using {control down, command down}    -- # Sets: View > Arrange By > Kind
            keystroke "1" using {control down, option down, command down}    -- # Sets: View > Sort By > Name
        end tell
        close eachFolder
    end repeat
end tell

If you're wanting to act only on the top level sub-folders within the parent folder, then in place of:

set theFolders to every folder of entire contents of parentFolder as alias list

Use the following:

set theFolders to every folder of parentFolder as alias list    

Note that the delay 0.5 may not be absolutely necessary however I added it because without it, the series of opening and closing folder's windows can go by so fast it's disconcerting and slowing it down a bit eases that. Although it may also be necessary, depending on how fast/slow the processing takes place, to ensure each folder's window in the repeat loop is actually available and has focus in order to receive the keystrokes. In either case, the value of delay may/can be changed as necessary/wanted to allow the tasks to properly occur and in a visual manner that appears more natural then a barrage of opening/closing windows leaving one to wonder, what just happened.