sábado, 7 de junho de 2008

Textmate protocol behavior on any editor

Textmate has a nice feature that allows You to open files in it through Your browser just doing:

txmt://open?url=file://file/to/open

Rails Footnotes plugin use this to open Your controller, view or layout directly through Your browser and I wanted to have it on my Notepad++!

The first step is to register the procotol within Your browser. I'm using Firefox and it has a well explained tutorial showing how to do it on Windows, MacOS and Linux here.

Then I've registered the notepad protocol with network.protocol-handler.external.notepad and the path:

network.protocol-handler.app.notepad => /path/to/notepad

Finally, I've tried to open a file typing the address notepad:/file/to/open

Bad surprise!

It wasn't opening the file because Firefox was sending the whole URL, i.e. notepad:/file/to/open, and, of course, Notepad++ doesn't have a clue what to do with the protocol at the beginning.

The solution?

I've decided to send the whole URL to a ruby script, then I would parse it and send to Notepad++. Since I could parse the URL, I've defined as default a Textmate-like-URL:

notepad://open?url=file://file/to/open

Then I set in Firefox the following:

network.protocol-handler.app.notepad => /bin/ruby path/to/script.rb

On Windows, the last line of the registry file would be:

@="\"C:\\ruby\\bin\\ruby.exe\" \"C:\\path\\to\\script.rb\" \"%1\""

And then the ruby script file:

file = ARGV.first.split('file://').last.split('&').first

`"/path/to/notepad" "#{file}"`


The script catches the argument sent, get everything that is between file:// and the first & and sent to Notepad++.

Doing all this, I was finally able to set on my Footnotes plugin:

Footnotes::Filter.prefix = 'notepad://open?url=file://'

And open some files through Firefox! =)

5 comentários:

Dr Nic disse...

Nice solution. Perhaps post a link to this article in the plugin README?

José Valim disse...

Done! =)

gauda disse...

and this is how to go on linux and netbeans

lillian disse...

I recently came across your blog and have been reading along. I thought I would leave my first comment. I don't know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often.


Sarah

http://adoptpet.info

José Valim disse...

@Sarah,

Thanks for the kind words! This for sure motivates me to keep writing. :)