Open file in Sublime Text tab via AppleScript

applescriptsublimetext

Using the following AppleScript code, I can open a .txt file in a new window of Sublime Text:

tell application "Sublime Text"
    open "Users:me:Desktop:foo.txt"
end tell

Is there a way to open this file NOT as its own new window, but rather as a new tab of my existing window of Sublime Text?

Best Answer

Sublime Text doesn't provide an AppleScript suite, but does provide a command line utility ‘subl’ in:

/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl

Usage is available with --help.

subl is aware of the current window when opening files and adds the file as a tab.

/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl /path/to/file

To run this in AppleScript, use ‘do shell script’.

do shell script ¬
    "/Applications/Sublime\\ Text.app/Contents/SharedSupport/bin/subl /path/to/file"
do shell script 
    "/Applications/Sublime\\ Text.app/Contents/SharedSupport/bin/subl " ¬
    & quoted form of POSIX path of (alias "path:to:file")