Home Download Buy Blog Forum Support

open web browser with specified link and selected text

open web browser with specified link and selected text

Postby panamajack on Wed Apr 13, 2011 3:44 am

I'm learning a new language and find myself constantly looking up the language spec in google or on msdn. I would love to be able to double click on a word, and press a hotkey ( F1 ) and have sublime open my web browser with a hardcoded link and insert the selected text into the URL

does anyone have anything like this already? Is it possible? I'm using the very latest sublime text 2 on OSX.

thanks in advance!
panamajack
 
Posts: 13
Joined: Wed Apr 13, 2011 3:38 am

Re: open web browser with specified link and selected text

Postby jbrooksuk on Wed Apr 13, 2011 8:08 am

This should do you :)

Code: Select all
import sublime, sublime_plugin
import webbrowser

class OpenHelpCommand(sublime_plugin.TextCommand):

   def run(self,edit):
      msdnLink = 'http://social.msdn.microsoft.com/Search/en-gb?query={0}';
      for region in self.view.sel():
         if not region.empty():
            syntax = self.view.substr(region)
            webbrowser.open_new(msdnLink.format(syntax))

I assigned the key to Ctrl+F1 :)

Select the function/word/whatever you want to lookup and it'll open a new browser window (tab in Chrome, probably Firefox/Opera too) taking you to MSDN :)
jbrooksuk
 
Posts: 773
Joined: Sun Apr 11, 2010 10:37 am
Location: England

Re: open web browser with specified link and selected text

Postby panamajack on Wed Apr 13, 2011 1:09 pm

wow, thanks! :) Looks simple enough?

Unfortunately, it doesn't seem to be working for me under OSX. I copied the code exactly and saved it to:

Code: Select all
/Users/jack/Library/Application Support/Sublime Text 2/Packages/User/OpenHelpCommand.py

then bound a key with it in the User Key Bindings with this:

Code: Select all
{ "keys": ["ctrl+f1"], "command": "OpenHelpCommand" },


The console shows the python file compiling fine, but nothing happens. I can't get the sample hello world plugin to work either ( or any other plugin I've tried to write ). Is there something else I need to install on the Mac?

I appreciate the help though :)
panamajack
 
Posts: 13
Joined: Wed Apr 13, 2011 3:38 am

Re: open web browser with specified link and selected text

Postby panamajack on Wed Apr 13, 2011 1:45 pm

ah.. never would have guessed that. I'll have to read up on creating plugins.

ctrl+f1 still doesn't work. It's worth mentioning I'm using the windows keybindings in the mac version.

the binding below does work however and it's plenty good. Thanks for your help

Code: Select all
   { "keys": ["f1"], "command": "open_help" },
panamajack
 
Posts: 13
Joined: Wed Apr 13, 2011 3:38 am

Re: open web browser with specified link and selected text

Postby jbrooksuk on Wed Apr 13, 2011 1:56 pm

panamajack wrote:ah.. never would have guessed that. I'll have to read up on creating plugins.

ctrl+f1 still doesn't work. It's worth mentioning I'm using the windows keybindings in the mac version.

the binding below does work however and it's plenty good. Thanks for your help

Code: Select all
   { "keys": ["f1"], "command": "open_help" },

Woops forgot to mention. Command is always dropped, and the definition is snake cased.

Code: Select all
OpenHelpCommand - open_help

Try ctrl+f2 ?
jbrooksuk
 
Posts: 773
Joined: Sun Apr 11, 2010 10:37 am
Location: England


Return to General Discussion

Who is online

Users browsing this forum: Google [Bot] and 27 guests