Skip to content

Instantly share code, notes, and snippets.

@pitpit
Created September 13, 2012 09:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pitpit/3713150 to your computer and use it in GitHub Desktop.
Save pitpit/3713150 to your computer and use it in GitHub Desktop.
Create an URI protocol (edit://) in OSX to directly edit a file from browser to Sublime Text 2

This step by step explains how to create an URI protocol (a scheme) to directly edit a file in Sublime Text 2 from browser (or from the terminal).

Then it explains how to add this kind of link to Symfony2 Exceptions in order to allow opening file directly when an Exception is thrown.

Designed for OSX

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

Launch AppleScript and save the following script as an Application (name it "Sublime Text 2 URL Launcher"):

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 open location theUri

    set theParts to my splitString(theUri, "#")
    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 "edit://"
    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 2.app/Contents/SharedSupport/bin/subl\" " & Filepath & ":" & theLine
    end tell

end open location

Edit Info.plist in the application:

 vim /Applications/Sublime\ Text\ 2\ URL\ Launcher.app/Contents/Info.plist

And add:

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

before (at the end of file):

</dict>
</plist>

Customise Symfony2 Exceptions

Create the file app/Resources/TwigBundle/views/Exception/trace.html.twig containing:

{% set replace = {"/mnt/workspace": "~/Workspace"} %} {% if trace.function %} at {{ trace.short_class }} {{ trace.type ~ trace.function }} ({{ trace.args|format_args }}) {% endif %}

{% if trace.file is defined and trace.file and trace.line is defined and trace.line %}
    {{ trace.function ? '<br />' : '' }}
    in {{ trace.file|format_file(trace.line) }}
    {% spaceless %}
    <a href="edit://{{ trace.file|replace(replace) }}#{{ trace.line }}" title="Edit the file"><img style="vertical-align:top;" width="16" height="16" alt="star" src="data:image/gif;base64,R0lGODlhEAAQAMQAAORHHOVSKudfOulrSOp3WOyDZu6QdvCchPGolfO0o/XBs/fNwfjZ0frl3/zy7////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAkAABAALAAAAAAQABAAAAVVICSOZGlCQAosJ6mu7fiyZeKqNKToQGDsM8hBADgUXoGAiqhSvp5QAnQKGIgUhwFUYLCVDFCrKUE1lBavAViFIDlTImbKC5Gm2hB0SlBCBMQiB0UjIQA7" /></a>&nbsp;
    <a href="#" onclick="toggle('trace_{{ prefix ~ '_' ~ i }}'); switchIcons('icon_{{ prefix ~ '_' ~ i }}_open', 'icon_{{ prefix ~ '_' ~ i }}_close'); return false;">
        <img class="toggle" id="icon_{{ prefix ~ '_' ~ i }}_close" alt="-" src="{{ asset('bundles/framework/images/blue_picto_less.gif') }}" style="visibility: {{ 0 == i ? 'display' : 'hidden' }}" />
        <img class="toggle" id="icon_{{ prefix ~ '_' ~ i }}_open" alt="+" src="{{ asset('bundles/framework/images/blue_picto_more.gif') }}" style="visibility: {{ 0 == i ? 'hidden' : 'display' }}; margin-left: -18px" />
    </a>
    {% endspaceless %}
    <div id="trace_{{ prefix ~ '_' ~ i }}" style="display: {{ 0 == i ? 'block' : 'none' }}" class="trace">
        {{ trace.file|file_excerpt(trace.line) }}
    </div>
{% endif %}

It adds an img link on each trace line.

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