Skip to content

Instantly share code, notes, and snippets.

@joseadrian
Forked from pitpit/Sublime Text 2 URL Launcher.md
Last active August 29, 2015 14:05
Show Gist options
  • Save joseadrian/54ba2276e3d2a7f99267 to your computer and use it in GitHub Desktop.
Save joseadrian/54ba2276e3d2a7f99267 to your computer and use it in GitHub Desktop.
Script to make "Jump to file" of PHP-Console extension work with Sublime Text on OS X
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").

imgur

Edit Info.plist

in the application you just created and add the next lines

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

Launch the app just once

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment