Thursday, July 04, 2013

Open Files/Folders Selected in Finder with Sublime Text 3 in Mac OS X

Sublime Text is a great text editor. Here's a little AppleScript that can help open folders or files selected in the finder directly with Sublime Text 3. To use the script:


  1.  Open AppleScriptEditor 
  2.  Open a new editor (File -> New) 
  3. Paste the code below into the editor 
  4. Press the compile button to make sure it was copied correctly 
  5. Export as an Application (File -> Export, Make sure you select 'Application' as the File Format. I usually save my scripts to '~/Library/Scripts' but the location isn't so important. 
  6. Drag your newly saved application onto the Finder's toolbar 
  7. That's it. Just press it whenever you want to open selected Files/Folders in Sublime Text 3 
  8. You can change the icon on your new Application by following these instructions.

(*
 Open in Sublime Text
 To use:
  * Drag Open In TextMate to the toolbar of any finder
  window to add it to the toolbar
*)

on run
 tell application "Finder"
  if selection is {} then
   set finderSelection to folder of the front window as string
  else
   set finderSelection to selection as alias list
  end if
 end tell
 
 subl(finderSelection)
end run

-- script was drag-and-dropped onto
on open (theList)
 subl(theList)
end open

-- open in Sublime Text
on subl(listOfAliases)
 tell application "Sublime Text"
  activate
  open listOfAliases
 end tell
end subl

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Hi,

    I was using your script for a while ago, but noticed that the startup was sluggish when sublime was not already running.
    Found the following applescript to work a little better (from https://forum.sublimetext.com/t/open-in-sublime-finder-toolbar-app-for-os-x/4538/13 (NGRAY)):

    -- script was opened by click in toolbar
    on run
    set st2 to POSIX path of (path to application "Sublime Text 2")
    set subl to st2 & "Contents/SharedSupport/bin/subl"
    tell application "Finder"
    copy selection to theSelected
    set outputPathList to {}
    repeat with anItem in theSelected
    copy "'" & ((POSIX path of (anItem as alias)) as string) & "'" to end of outputPathList
    end repeat
    set AppleScript's text item delimiters to " "
    set currentPath to outputPathList as string
    set AppleScript's text item delimiters to ""
    if currentPath is equal to "" then
    set currentPath to "'" & (POSIX path of (target of front window as string)) & "'"
    end if
    tell current application to do shell script "'" & subl & "' " & currentPath
    end tell
    end run

    ReplyDelete

Note: Only a member of this blog may post a comment.