Tetsujin
Tetsujin

Reputation: 121

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

Upvotes: 1

Views: 3748

Answers (2)

andiOak
andiOak

Reputation: 396

AppleScript to open Tabs in Finder from a list of paths in POSIX

Running this in the application Script Editor opens a predefined list of tabs from a list that one can set in normal POSIX file path, like /path/to/folder-or-file. To get a link to your folder or file, either press CMD+i or press File->Get Info in the menu (or just right-click on the file/folder itself). In the small window popping up, copy the path from the field named General -> Where: or get it from the terminal using print working directory (pwd) command and copy-pasting into the item-vars in the script below. Using a repeat loop we go through the vars included in the list and pull up one tab for each item. Very handy for those projects that use the same folders but lots of them!

on convertPathToAlias(thePath)
    tell application "System Events"
        try
            return (path of disk item (thePath as string)) as alias
        on error
            return (path of disk item (path of thePath) as string) as alias
        end try
    end tell
end convertPathToAlias


set item1 to "/Users/username/Desktop/myfolder1"
set item2 to "/Users/username/Desktop/myfolder2"
set item3 to "/Users/username/Desktop/myfolder3"
set item4 to "/Users/username/Desktop/myfolder4"

set myList to {item1, item2, item3, item4}

set default_path to convertPathToAlias(item1)

tell application "Finder"
   activate
   open default_path
end tell

repeat with theItem in myList

    set current_path to convertPathToAlias(theItem)

    tell application "System Events" to keystroke "t" using command down
    delay 0.3
    tell application "Finder" to set target of front window to current_path

end repeat

Upvotes: 2

user3439894
user3439894

Reputation: 7555

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.

Upvotes: 4

Related Questions