How to get a hyperlink on a web page to open a file in a text editor (Sublime Text) - sublimetext

I'm working on a large static website (Jekyll) and would like to be able to click on a link on a page in browser (Chrome) which will open it's corresponding source file on the local machine (Sublime). I can get the absolute link of the file.
From the console (Ubuntu) I can do:
subl path/to/file.txt
to open a file, so perhaps an extension that allows command execution on trusted domains?

Ubuntu
There is a solution for Ubuntu
Windows Method
You first have to install a Protocol Handler in the Windows registry.
(Reference:
Custom protocol handler in chrome)
This will get Chrome to run a command when a subl:path/to/file link is clicked.
But you can't simply run sublime_text.exe subl:path/to/file, because Sublime doesn't understand the subl:path/to/file parameter. You first have to extract the filename with a script and then call Sublime with a single file parameter.
Solution 1
This is a Solution for Windows, that opens links with the format
subl://open?url=file://<path_to_file>&line=4
It first registers a Script as a Protocol Handler, which in turn calls Sublime
Solution 2
I edited Solution 1 to open links in the format
subl://path/to/file:line
Just follow Solution 1, but change open_file.vbs to:
' path to sublime_text.exe
Dim sublime_path
' CHANGE THIS:
sublime_path = "C:\Programme\Sublime Text 3\sublime_text.exe"
Dim text, filename
' get first command line argument
text = WScript.Arguments.Item(0)
filename = Split(text, ":/")(1)
Dim run_command
run_command = """"&sublime_path&""" """&filename&""""
Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run(run_command)
Set objShell = Nothing
(You have to change the path to your sublime_text.exe)
Mac
https://github.com/corysimmons/subl-handler

There's a Chrome extension, that let's you open a link with an external application: Open with external application

Old question, but you can try this: subl://path/to/file.txt

Related

Create shortcut to website in chrome on desktop automatically

I am trying to write a script to automatically save a shortcut to a user's desktop which opens in full screen like an app.
To do this manually you click chrome options -> More Tools -> Add to Desktop
You then need to click open as window and Add, and chrome will save the app to the desktop, and it will open in fullscreen.
I want to be able to automate this so that it can be setup on everyone's computer through group policy, or by the user just running a script.
The shortcut chrome creates is something like ...chrome.exe -app-id=hjnwjndjsn... So it looks like chrome needs to know about it so it can link it can create the app. i.e. its not just a simple link to chrome with the website url.
I am happy for this be done in any language ... I have put vbscript and batch in the tags just as a suggestion.
I'm not sure about to the desktop but you can echo it
echo.
echo. Website:
echo [InternetShortcut] > "%Path%"
echo URL=%URL_PATH% >> "Path%"
echo idlist= >> "%Path%"
echo.
It's not that easy, if at all to do, without linking to a batch file in the server.
Hyperlink it to a .bat file which will be like this:
copy \SERVER\FOLDER\FOLDER\SHORTCUT.lnk c:\users\USER\desktop
Easy as can be!
You should see this link,
but here is the code what you need (in VBScript):
To web page:
WshShell = CreateObject("WScript.Shell")
strDesktop = WshShell.SpecialFolders("Desktop")
oUrlLink = WshShell.CreateShortcut(strDesktop+"\Microsoft Web Site.URL")
oUrlLink.TargetPath = "http://www.microsoft.com"
oUrlLink.Save
To aplication (like "...chrome.exe -app-id=hjnwjndjsn..."):
oMyShortcut = WshShell.CreateShortcut(strDesktop + "\Sample.lnk")
OMyShortcut.TargetPath = "%windir%\notepad.exe"
oMyShortCut.Save

Could Not find windows command to convert dos file to unix

The scenario where I am stuck is that I have a normal windows text file 'command.txt' which I am copying on remote(unix) server from my local machine(windows) using 'pscp' command from cmd.
The file contains some unix commands which are required to run on remote server. The copy is done successfully, but it is getting copied as dos file. That is why, when I am trying to run command.txt file from my local machine using 'plink' as:
plink -pw password user#host bash -m /location of command.txt on the server/command.txt
It is unable to execute command.txt. So, I need some windows command or may be some pscp option to convert my file to unix text file before copying it on the remote server.
The solution I know is using dos2unix, but I don't want to use it.
You seem to have problems with LF (line feed) characters in your text files produced on Windows box. If the file is not big you can use i.e. VBScript (it's on all Windows boxes, so no need to install additional tools) to remove the LF characters:
Dim objFSO
Dim objOutput
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutput = objFSO.CreateTextFile("out.txt")
arrData = Split(objFSO.OpenTextFile("in.txt").ReadAll, vbLf)
For Each Item In arrData
strData = strData & Replace(Item, vbLf, "")
Next
objOutput.WriteLine strData
objOutput.Close
Script presented above processes in.txt file into out.txt file, but can be easily extended to accept filenames from command line.
You can execute above script using cscript command:
cscript rem.vbs
You can run dos2unix on Windows too. Dos2unix is not limited to Linux. It runs on many platforms, including Windows. You only need to install it.
Thank you for the replies.
The thing is that I have to perform this task only with the environment provided to me which is Windows and without downloading new features(I know its silly but I have no other option). One solution I found was to open the file in notepad++, go to the format tab and select 'convert to UNIX format'. It is working fine but as soon as I copy the file at some other location, its format again reverts back to windows format.
Is there any method to persist the changes in the format?

Launch .bat files from a custom URL handler without showing the console window

I registered a custom URL handler in Windows, in order to be able to launch a local program from an URL. Following the MSND documentation, I inserted the following values in the Registry:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\TestLaunch]
#="URL:TestLaunch Protocol"
"URL Protocol"=""
[HKEY_CLASSES_ROOT\TestLaunch\DefaultIcon]
#="\"c:\\temp\\test.bat\""
[HKEY_CLASSES_ROOT\TestLaunch\shell]
[HKEY_CLASSES_ROOT\TestLaunch\shell\open]
[HKEY_CLASSES_ROOT\TestLaunch\shell\open\command]
#="\"c:\\temp\\test.bat\" %1"
This works, but when I click a TestLaunch: link and the batch file starts I can see the console windows appearing. Since the role of the batch file is just parsing the argument url and launching another application, I would like the console non to appear (or at least being minimized), even if it is just for a fraction of a second.
The only thing that came to my mind is creating a link to the batch file (test.bat.lnk) and set it to start as minimized, but that won't work.
Any other ideas ? I'm open to an alternative to batch files, but I'd like to stick to what Windows provides
You could use VBS?
#="\"WSCRIPT c:\\temp\\test.vbs\" %1"
Using
if wscript.arguments.length > 0 then
wscript.createobject( "WScript.Shell" ).run("app.exe " & wscript.arguments(0))
end if
Some workarounds can be found here:
https://superuser.com/questions/62525/run-a-completly-hidden-batch-file
Launch a program from command line without opening a new window
http://www.tomshardware.co.uk/forum/245566-45-batch-file-window-poping
http://www.joeware.net/freetools/tools/quiet/index.htm
http://www.ntwind.com/software/hstart.html

Double Click and Open an undefined file from within VBscript

I want to open .mp3 files with mpg123.exe silently when a .mp3 file is double clicked from within Windows Explorer. For this I wrote a VBScript as bellow and changed the default program for playing .mp3 files by assigning my VBScript to it via Open with → Choose default program. My script is working well from within command line (cmd.exe) but when a .mp3 file is double clicked I get an error message that double clicked .mp3 file is not an executable file in Windows. Here is my VBScript, please let me know where I am doing wrong.
if Wscript.arguments.count = 0 then
WScript.quit
else
strSoundFile = WScript.Arguments.Item(0)
end if
Set objShell = CreateObject("Wscript.Shell")
strCommand = "mpg123.exe -q " & chr(34) & strSoundFile & chr(34)
objShell.Run strCommand, 0, True
Why don't you just associate mp3 files with mpg123.exe and set up the associated parameters (eg: -q "%1") instead?
Since I couldn't find a notable existing example, I've whipped up an example for you. (tested to work on Windows 7 with mpg123.exe). The response was too image heavy to post here. I hope it helps you.

Determine default program to open given file extension - VBS

There are a handful of answers I've found but nothing that deals specifically with my problem exactly, or not with VBS.
I am looking for a way to determine the full path to a the default program when providing a specific file extension.
My ultimate goal is to automatically create a shortcut to whatever program opens ".DOC" files (typically MS Word). But this will obviously vary on different windows machines.
I would love to do something like:
strDefaultDOCProgram = WshShell.FindAssociatedProgram("doc")
where
strDefaultDOCProgram = "C:\Program Files\Microsoft Office 15\root\office15\winword.exe"
Maybe helpful?
Ask Windows 7 - what program opens this file by default
I ultimately decided to use the assoc and ftype commands just in case we want to use this script on any other Windows versions. Here's a function that will do everything I needed. I hope it's helpful to someone!
Dim WshShell
Set WshShell = CreateObject("WScript.Shell")
' Should supply the program extension with period "." included
Function GetProgramPath(ext)
Dim strProg, strProgPath
' Get Program Association Handle
Set oExec = WshShell.Exec("cmd.exe /c assoc " & ext)
strProg = oExec.StdOut.ReadLine()
strProg = Split(strProg, "=")(1)
' Get Path To Program
Set oExec = WshShell.Exec("cmd.exe /c ftype " & strProg)
strProgPath = oExec.StdOut.ReadLine()
strProgPath = Split(strProgPath, """")(1)
' Return the program path
GetProgramPath = strProgPath
End Function
strPath = GetProgramPath(".doc")
WScript.Echo strPath
use assoc
assoc /?
Displays or modifies file extension associations
ASSOC [.ext[=[fileType]]]
.ext Specifies the file extension to associate the file type with
fileType Specifies the file type to associate with the file extension
and ftype
type /?
isplays or modifies file types used in file extension associations
TYPE [fileType[=[openCommandString]]]
fileType Specifies the file type to examine or change
openCommandString Specifies the open command to use when launching files
of this type.
like
assoc .doc
.doc=OpenOffice.org.Doc
ftype OpenOffice.org.Doc
OpenOffice.org.Doc="C:\Program Files\OpenOffice.org 3\program\\swriter.exe" -o "%1"
via a script that executes those programs with .Exec.
Update:
Cut the file spec from the command:
>> sCmd = """C:\Program Files\OpenOffice.org 3\program\\swriter.exe"" -o ""%1"""
>> WScript.Echo sCmd
>> WScript.Echo Split(sCmd, """")(1)
>>
"C:\Program Files\OpenOffice.org 3\program\\swriter.exe" -o "%1"
C:\Program Files\OpenOffice.org 3\program\\swriter.exe
Update II:
Don't use .RegRead to try to find the info in this week's version of the registry; assoc and ftype are the tools your operating system provides for your problem.
The means for opening files has changed over time. You are talking about legacy ways of opening a file, which is still the most common.
The beginning.
To support office you could put in win.ini *.doc=c:\winword.exe.
Asociations are per user and per machine with per user settings overriding machine settings.
In NT/Win 95 it was expanded. So HKCR.ext could hold the open string (\shell\open) for compat with win.ini but more typically poined to a file class, eg HKCR.txt=txtfile. Looking up HKCR\txtfile\shell\open gave you the command.
Due to programs stealing file associations this now has an layer of other associations put over it. So the command is built from above and these newer keys HKEY_CLASSES_ROOT\SystemFileAssociations (which also includes associations for a newer concept of a general type of file - picture or music) or HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts.
For Word, it was opened using DDE (also specified at the above registry keys) rather than just a command line. That is it was started as a DDE server then had fileopen commands sent to it with the name of the file to be opened
New Ways of Opening Files.
Files are now opened using COM. The program registers IDropTarget under above keys.
Context menu handlers can override the above. They are also registered above.
The best way is to shellexec the file. It will just open like it was double clicked.

Resources