SimonDesautels
8/28/2014 - 6:48 AM

Sublime Text URL Launcher.md

on splitString(theString, theDelimiter)
  -- save delimiters to restore old settings
  set oldDelimiters to AppleScript's text item delimiters
  -- set delimiters to delimiter to be used
  set AppleScript's text item delimiters to theDelimiter
  -- create the array
  set theArray to every text item of theString
  -- restore the old setting
  set AppleScript's text item delimiters to oldDelimiters
  -- return the result
  return theArray
end splitString

on strReplace(this_text, search_string, replacement_string)
  set AppleScript's text item delimiters to the search_string
  set the item_list to every text item of this_text
  set AppleScript's text item delimiters to the replacement_string
  set this_text to the item_list as string
  set AppleScript's text item delimiters to ""
  return this_text
end strReplace

on open location theUri
  set cleanPath to my strReplace(theUri, "open/?file=", "")
  set theParts to my splitString(strReplace(cleanPath, "%2F", "/"), "&")
  set theUrl to first item of theParts
  set theAnchor to last item of theParts
  
  set Delimiters to AppleScript's text item delimiters
  set AppleScript's text item delimiters to "editor://"
  set Filepath to item 2 of the text items of theUrl
  set AppleScript's text item delimiters to Delimiters
  set theLine to 1
  
  tell application "System Events"
    if not theAnchor = theUrl then
      set theLine to theAnchor
    end if
    do shell script "\"/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl\" " & Filepath & ":" & my strReplace(theLine, "line=", "")
  end tell
  
end open location

This step by step explains how to create an URI protocol (a scheme) to directly edit a file in Sublime Text from browser (or from the terminal), and mostly from notifications of the PHP-Console extension.

Create the URI launcher for "editor://" URI

Launch AppleScript and export the open_editor.scpt script as an Application (name it "Sublime Text URL Launcher").

Edit Info.plist

in the application you just created and add the next lines before the last 2 closing tags :

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLName</key>
        <string>Sublime Text URL Launcher</string>
        <key>CFBundleURLSchemes</key>
        <array>
                <string>editor</string>
        </array>
    </dict>
</array>

Launch the app just once