Open URL with AutoKey (Python question)

539 views
Skip to first unread message

Bohemian con man

unread,
Sep 24, 2010, 11:34:38 AM9/24/10
to autokey-users
Hi

I have a beginner question:
How do I open a URL like http://www.python.org in the default browser?

After a long search online I've managed to find a script that will
open a website in Firefox.

import webbrowser
webbrowser.get("firefox").open("http://www.python.org")

But I don't want to be restricted to one browser. What's the general
form of this?

And more generally asked:
I'm a windows user who is used to having a Run command for opening any
program, website, folder or file.
Is there something like that for python?

Thanks!

BTW: I have some scripting knowledge in AutohotKey and VBA, but
nothing fancy. Completely new to Python.

Luke Faraone

unread,
Sep 24, 2010, 11:49:16 AM9/24/10
to autoke...@googlegroups.com, Bohemian con man
On 09/24/2010 11:34 AM, Bohemian con man wrote:
> Hi
>
> I have a beginner question:
> How do I open a URL like http://www.python.org in the default browser?
>
> After a long search online I've managed to find a script that will
> open a website in Firefox.
>
> import webbrowser
> webbrowser.get("firefox").open("http://www.python.org")
>
> But I don't want to be restricted to one browser. What's the general
> form of this?

webbrowser.open() should work. See
http://docs.python.org/library/webbrowser.html#webbrowser.open

> And more generally asked:
> I'm a windows user who is used to having a Run command for opening any
> program, website, folder or file.
> Is there something like that for python?

subprocess.call().
http://docs.python.org/library/subprocess.html#subprocess.call

--
╒═════════════════════════════════════════════════════════════════╕
│Luke Faraone ╭Debian / Ubuntu Developer╮│
http://luke.faraone.cc ╰Sugar Labs, Systems Admin╯│
│PGP: 5189 2A7D 16D0 49BB 046B DC77 9732 5DD8 F9FD D506 │
╘═════════════════════════════════════════════════════════════════╛

Derek

unread,
Sep 30, 2010, 9:15:08 AM9/30/10
to autoke...@googlegroups.com
This should answer both questions. The xdg-open command is probably
what you want. The URL has to start with "http://" or "https://" for it
to be recognized as a web URL, and it will open in your default web
browser. Otherwise, you could have your script check if the text looks
like a web URL and prefix it if appropriate. An absolute the path to a
file or directory will be opened as if you double-clicked its icon.
Alt+F2 will bring up a window that similarly opens whatever you type
into it.

import subprocess

text = clipboard.get_selection()

args = ['xdg-open', text]
subprocess.Popen(args)

Derek

Bohemian con man said the following on 09/24/2010 08:34 AM:

Boris

unread,
Oct 5, 2010, 3:33:48 PM10/5/10
to autoke...@googlegroups.com
Thank you, works beautifully.
It's exactly what I was looking for.
B

2010/9/30 Derek <dv8bo...@yahoo.com>
Reply all
Reply to author
Forward
0 new messages