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?

1 Answer 1


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")
  • 1
    You're a wizard! The only tweak I had to make was to put two backslashes to escape the space in your AppleScript example, but it worked like a charm!
    – puterguy01
    Commented Jun 27, 2018 at 22:35
  • 1
    You might need to use quoted form of POSIX path of..., which will escape the path string before sending it to do shell script ( e.g. if /path/to/file contains any spaces, etc.).
    – CJK
    Commented Jun 27, 2018 at 23:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .