directoryapplescriptfinder

Improve Speed of AppleScript that Opens Multiple Folders in Multiple Tabs in new Finder Window?


I have a series of subfolders with a parent folder called "Inventory" within a internal hard drive.

Each subfolder name consists of a number, a hyphen and some text, such as:

"329 - Antique Photo Album".

The following script, compiled as a app, displays a dialog box into which a number is typed.

If one of the subfolders matches the user inputed number the script opens that subfolder in a new Finder window.

The script then opens each of that folder's sub subfolders, named "_R", "GS", "K", "O", "T", "WA", "WM" in new tabs in the Finder window just opened.

This takes about 32 seconds.

Is there any way to improve the following code so it works faster?

set theReply2 to (display dialog "Enter new replacement text" default answer "" buttons {"Cancel", "Continue"} default button "Continue")

set replaceText to text returned of theReply2

set theSubs to {"_R", "GS", "K", "O", "T", "WA", "WM"}

try
    
    set thePath to "Volumes:DA2.0:Inventory:" as alias
    
    tell application "Finder" to set theSubFolder to (first folder of thePath whose name starts with replaceText & " - ")
    
    tell application "Finder" to open theSubFolder
    
    repeat with x from 1 to count of items in theSubs
        
        
        tell application "Finder" to set theSubSubFolder to (first folder of theSubFolder whose name is item x of theSubs)
        
        tell application "System Events"
            tell application process "Finder" to set frontmost to true
            keystroke "t" using command down
        end tell
        
        tell application "Finder" to set target of front window to theSubSubFolder as alias
        
    end repeat
    
end try

Per Ted Wrigley suggestion below the following altered code reduces the time to 19 seconds.

Yes, I am using Mac Pro 2.8GHz Quad-Core Intel Xeon with High Sierra and four internal drives and 48GB of RAM.

set theReply2 to (display dialog "Enter new replacement text" default answer "" buttons {"Cancel", "Continue"} default button "Continue")

set replaceText to text returned of theReply2

set theSubs to {"_R", "GS", "K", "O", "T", "WA", "WM"}

try
    
    --set thePath to "Volumes:DA2.0:Inventory:" as alias
    
    set thePath to POSIX file "/Volumes/DA2.0/Inventory/"
    
    --tell application "Finder" to set theSubFolder to (first folder of thePath whose name starts with replaceText & " - ")
    
    tell application "Finder" to set theSubFolder to (first folder of folder thePath whose name starts with replaceText & " - ")
    
    tell application "Finder" to open theSubFolder
    
    
    repeat with x from 1 to count of items in theSubs
        
        
        tell application "Finder" to set theSubSubFolder to (first folder of theSubFolder whose name is item x of theSubs)
        
        tell application "System Events"
            tell application process "Finder" to set frontmost to true
            keystroke "t" using command down
        end tell
        
        tell application "Finder" to set target of front window to theSubSubFolder as alias
        
    end repeat
    
    
end try


Solution

  • Generally, it is faster to use System Events than Finder to grind through folders of folders. Here is something to try.

    set returnDialog to (display dialog "Enter new replacement text" default answer "" buttons {"Cancel", "Continue"} default button "Continue")
    set returnedText to text returned of returnDialog
    
    set containFolder to "Volumes:DA2.0:Inventory" as text) as alias -- set container folder (eg 'inventory')
    
    tell application "System Events"
        set specf to (first folder of containFolder whose name begins with (returnedText & " - ")) -- locate specified folder
        open specf -- open specified folder (eg '329 - antiques photo albumju')
        
        set specf to path of specf as alias -- coerce specf reference to alias
        set subf to folders of specf -- list subfolders of specified folder
    
    end tell
    
    -- work with front window
    tell application "Finder" to activate
    repeat with eaf in subf -- cycle each subfolder
        tell application "System Events"
            keystroke "t" using command down -- open in new tab
            set mypath to path of eaf
            tell application "Finder" to set target of front window to mypath
        end tell
    end repeat
    

    Thanks for your input.

    I've modified your code which now works in 6-7 seconds.