Skip to content

Instantly share code, notes, and snippets.

@MarioBinder
Last active March 14, 2022 17:18
Show Gist options
  • Save MarioBinder/683d96cae19b85358b094dece8525ec2 to your computer and use it in GitHub Desktop.
Save MarioBinder/683d96cae19b85358b094dece8525ec2 to your computer and use it in GitHub Desktop.
open sublime links from webpage

sublime-protocol-win

Setting up Custom URI scheme handler for Windows to open files in Sublime editor
Created to parse URI's generated by filp/whoops
Parsing is done with VBScript, as that was the only way I found to open Sublime editor, while avoiding annoying command prompt window to pop up and close.

NOTE

After downloading / cloning the repository, you have to edit files to update path to open_file.vbs and to add path to Sublime editor
Currently I don't have time to create install/uninstall scripts, so any help is welcome
Also, README.md is all upside-down, so that will be updated soon

Whoops

whoops is an error handler framework for PHP. More about whoops

When using the pretty error page feature, whoops comes with the ability to open referenced files directly in your IDE or editor.

More on how to setup sublime here

Setup

In command prompt, navigate to location where you want to clone repository, and run this command
git clone https://github.com/ljubadr/sublime-protocol-win.git
Take a note of a folder path where you cloned this repository, as it will be used later
Navigate to the folder where you cloned repository (downloaded)

sublime-custom-protocol.reg

Before you merge this registry file, you need to edit this file.
Open file sublime-custom-protocol.reg in your favourite editor
On the last line
@="WSCRIPT \"C:\\sublime-protocol-win\\open_file.vbs\" \"%1\""
change
C:\\sublime-protocol-win\\
to the path where you cloned this repository
Take a note that you need \\ in the folder path
Save the file
Double click sublime-custom-protocol.reg to add new registry entry.

To remove subl:// protocol

Press Start button, then type regedit
Wait a little bit and in the search result will get regedit.exe (Registry Editor)
Open HKEY_CLASSES_ROOT, and then find subl, right click -> delete

open_file.vbs

This is the script that will be called to process new custom URI protocol subl://
Open file open_file.vbs in editor, and find line
sublime_path = "C:\Program Files (x86)\Sublime Text 3\sublime_text.exe"
Replace existing path with the path of your Sublime instalation
Or if you're using Sublime portable, enter path to .exe file

You're all set

All pages that have whoops errors will generate url with the link to a file
When you click on the link for the first time, browser will ask about permissions, so be sure to select allow

Little note for Chrome users, opening files will only work by clickning on generated link
Trying to paste generated link in URL will not work

URLDecode

URLDecode function was taken from http://www.motobit.com/tips/detpg_URLDecode/

' As this was my first experience with VBScript, I apologize to anyone looking at this code
' Test this script in command prompt
' open_file.vbs "subl://open?url=file://<path_to_file>&line=4"
' path to sublime_text.exe
Dim sublime_path
' I've tried to start sublime with `subl <path-to-file>:<line>`, but in my case it was
' much slower than opening directly with sublime
' >>> EDIT THIS PATH TO MATCH YOUR SUBLIME INSTALL PATH / OR PATH TO SUBLIME PORTABLE <<<
sublime_path = "C:\Program Files\Sublime Text 3\sublime_text.exe"
Dim text, decoded
' get first command line argument
text = WScript.Arguments.Item(0)
' decode URL
decoded = URLDecode(text)
Dim get_params, params
' Split
get_params = Split(decoded, "?")(1)
params = Split(get_params, "&")
Dim file_name, line
For Each field In params
If InStr(field, "url=") > 0 Then
file_name = Replace(field, "url=", "")
If InStr(file_name, "file://") > 0 Then
file_name = Replace(file_name, "file://", "")
End If
ElseIf InStr(field, "line=") > 0 Then
line = Replace(field, "line=", "")
End If
Next
' Final command
Dim run_command
run_command = """"&sublime_path&""" """&file_name&":"&line&""""
' For debugging, echo generated command
' WScript.Echo run_command
' Wscript.Quit
Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run(run_command)
Set objShell = Nothing
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' URLDecode from http://www.motobit.com/tips/detpg_URLDecode/
',,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
Function URLDecode(ByVal What)
'URL decode Function
'2001 Antonin Foller, PSTRUH Software, http://www.motobit.com
Dim Pos, pPos
'replace + To Space
What = Replace(What, "+", " ")
on error resume Next
Dim Stream: Set Stream = CreateObject("ADODB.Stream")
If err = 0 Then 'URLDecode using ADODB.Stream, If possible
on error goto 0
Stream.Type = 2 'String
Stream.Open
'replace all %XX To character
Pos = InStr(1, What, "%")
pPos = 1
Do While Pos > 0
Stream.WriteText Mid(What, pPos, Pos - pPos) + _
Chr(CLng("&H" & Mid(What, Pos + 1, 2)))
pPos = Pos + 3
Pos = InStr(pPos, What, "%")
Loop
Stream.WriteText Mid(What, pPos)
'Read the text stream
Stream.Position = 0
URLDecode = Stream.ReadText
'Free resources
Stream.Close
Else 'URL decode using string concentation
on error goto 0
'UfUf, this is a little slow method.
'Do Not use it For data length over 100k
Pos = InStr(1, What, "%")
Do While Pos>0
What = Left(What, Pos-1) + _
Chr(Clng("&H" & Mid(What, Pos+1, 2))) + _
Mid(What, Pos+3)
Pos = InStr(Pos+1, What, "%")
Loop
URLDecode = What
End If
End Function
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\subl]
"URL Protocol"=""
@="URL:sublime Protocol"
[HKEY_CLASSES_ROOT\subl\shell]
@="open"
[HKEY_CLASSES_ROOT\subl\shell\open]
[HKEY_CLASSES_ROOT\subl\shell\open\command]
@="WSCRIPT \"D:\\Apps\\OpenInSublime\\sublime-protocol-win\\open_file.vbs\" \"%1\""
<span class="right">
[Edit in Sublime](subl://open?url=file://d:\dev\spielwiese\mydailytasks2022\content\post\Buecher\index.md&line=16)
</span>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment