Search code examples
tabsapplescript

Applescript - open a new tab


My AppleScript abilities are rather limited, so please forgive what may be a simple question.

I have this script as an Automator Service which will open a series of aliases in new windows.
Triggered by key command in Finder via prefs>keyboard>shortcuts>services.
Service receives selected files or folders in Finder

on run {input, parameters}
    repeat with aFile in input
        tell application "Finder"
            try
                set origFile to original item of aFile
                set aWindow to make new Finder window
                set aWindow's target to origFile's parent
                select origFile
            end try
        end tell
    end repeat
end run

I'd like to try open in tabs instead, preferably without resorting to GUI scripting.
set aWindow to make new Finder window appears to have no equivalent set aWindow to make new Finder tab & scouring Apple's online documentation for 'make' or 'tab' has proven pretty fruitless... or rather much fruit, all of the wrong variety :/

I have a GUI version from another source

on new_tab()
    tell application "System Events" to tell application process "Finder"
        set frontmost to true
        tell front menu bar to tell menu "File" to tell menu item "New Tab"
            perform action "AXPress"
        end tell
    end tell
end new_tab

so, failing the direct approach, how could I fold this into my existing script?

MacOS 10.13.4


Solution

  • With the macOS defaults for both the Open folder in tabs instead of new windows preference in Finder unchecked, and the Dock preference Prefer tabs when opening documents: in System Preferences set to In Full Screen Only, then the following example AppleScript code should work as wanted with incorporating your original AppleScript code and the code of the new_tab handler.

    on run {input, parameters}
        set madeNewWindow to false
        repeat with i from 1 to count input
            tell application "Finder"
                if (kind of item i of input) is equal to "Alias" then
                    set origFile to original item of item i of input
                    if not madeNewWindow then
                        set theWindow to make new Finder window
                        set madeNewWindow to true
                    else
                        my makeNewTab()
                    end if
                    set theWindow's target to origFile's parent
                    select origFile
                end if
            end tell
        end repeat
    end run
    
    on makeNewTab()
        tell application "System Events" to tell application process "Finder"
            set frontmost to true
            tell front menu bar to tell menu "File" to tell menu item "New Tab"
                perform action "AXPress"
            end tell
        end tell
    end makeNewTab
    
    • On my system it was not necessary for me to use the delay command however, delay commands may or may not be needed on your system and if so, add as necessary while adjusting the value as appropriate.

    • Coded for use in a Run AppleScript action in an Automator service where Service receives selected [files or folders] in [Finder].

    • Requires Finder to be added to Accessibility under Security & Privacy in System Preferences.

    • Tested under macOS High Sierra.


    Note: The example AppleScript code is just that and does not employ any other error handling then what's shown and is meant only to show one of many ways to accomplish a task. The onus is always upon the User to add/use appropriate error handling as needed/wanted.