Skip to content

Instantly share code, notes, and snippets.

@detain
Created June 29, 2022 13:44
Show Gist options
  • Save detain/9dc1da24e4b4d5b061cd11ba4c1226a5 to your computer and use it in GitHub Desktop.
Save detain/9dc1da24e4b4d5b061cd11ba4c1226a5 to your computer and use it in GitHub Desktop.
Register editor:// URL handler to open your fav text editor

How to setup the editor:// URL Handler

  • copy the open-in-editor.js and register editor handler.cmd somewhere on your system. (They must be in the same directory)
  • edit the open-in-editor.js and uncomment the editor you use commenting the other out
  • change the mappings lines around to rewrite the remote filename paths into the path your editor will expect
  • run the register editor handler.cmd (You must reload any existing browser tabs before it will work in them)
var settings = {
// editor: '"C:\\Program Files\\JetBrains\\PhpStorm 2018.1.2\\bin\\phpstorm64.exe" --line %line% "%file%"', // PhpStorm
// editor: '"C:\\Program Files\\NetBeans 8.1\\bin\\netbeans.exe" "%file%:%line%" --console suppress', // NetBeans
// editor: '"C:\\Program Files\\SciTE\\scite.exe" "-open:%file%" -goto:%line%', // SciTE
// editor: '"C:\\Program Files\\EmEditor\\EmEditor.exe" "%file%" /l %line%', // EmEditor
// editor: '"C:\\Program Files\\PSPad editor\\PSPad.exe" -%line% "%file%"', // PSPad Editor
// editor: '"C:\\Program Files\\Vim\\vim73\\gvim.exe" "%file%" +%line%', // gVim
// editor: '"C:\\Program Files\\Sublime Text 2\\sublime_text.exe" "%file%:%line%"', // Sublime Text 2
editor: '"C:\\Program Files\\NuSphere\\PhpED\\phped.exe" "%file%" --line=%line%', // Nusphere PHPEd
title: 'Nusphere PHPEd',
mappings: {
//'/homme/my': 'E:\\dev\\myadmin\\',
//'/home/vortex': 'E:\\dev\\myadmin\\',
//'/home/sites/mystage': 'E:\\dev\\myadmin\\'
'/home/vortex': '[SFTP:vortex] /',
'/home/my': '[SFTP:mynew] /',
'/home/sites': '[SFTP:mynewsites] /'
}
};
if (!settings.editor) {
WScript.Echo('Create variable "settings.editor" in ' + WScript.ScriptFullName);
WScript.Quit();
}
var url = WScript.Arguments(0);
var match = /^editor:\/\/(open|create|fix)\/\?file=([^&]+)&line=(\d+)(?:&search=([^&]*)&replace=([^&]*))?/.exec(url);
if (!match) {
WScript.Echo('Unexpected URI ' + url);
WScript.Quit();
}
for (var i in match) {
match[i] = decodeURIComponent(match[i]).replace(/\+/g, ' ');
}
var action = match[1];
var file = match[2];
var line = match[3];
var search = match[4];
var replace = match[5];
var shell = new ActiveXObject('WScript.Shell');
var fileSystem = new ActiveXObject('Scripting.FileSystemObject');
for (var id in settings.mappings) {
if (file.indexOf(id) === 0) {
file = settings.mappings[id] + file.substr(id.length);
break;
}
}
if (action === 'create' && !fileSystem.FileExists(file)) {
shell.Run('cmd /c mkdir "' + fileSystem.GetParentFolderName(file) + '"', 0, 1);
fileSystem.CreateTextFile(file);
} else if (action === 'fix') {
var lines = fileSystem.OpenTextFile(file).ReadAll().split('\n');
lines[line-1] = lines[line-1].replace(search, replace);
fileSystem.OpenTextFile(file, 2).Write(lines.join('\n'));
}
var command = settings.editor.replace(/%line%/, line).replace(/%file%/, file);
shell.Exec(command);
if (settings.title) {
shell.AppActivate(settings.title);
}
@echo off
:: This Windows batch file sets open-editor.js as handler for editor:// protocol
if defined PROCESSOR_ARCHITEW6432 (set reg="%systemroot%\sysnative\reg.exe") else (set reg=reg)
%reg% ADD HKCR\editor /ve /d "URL:editor Protocol" /f
%reg% ADD HKCR\editor /v "URL Protocol" /d "" /f
%reg% ADD HKCR\editor\shell\open\command /ve /d "wscript \"%~dp0open-in-editor.js\" \"%%1\"" /f
%reg% ADD HKLM\SOFTWARE\Policies\Google\Chrome\URLWhitelist /v "123" /d "editor://*" /f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment