Skip to content

Instantly share code, notes, and snippets.

@endolith
Last active March 22, 2018 21:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save endolith/77635 to your computer and use it in GitHub Desktop.
Save endolith/77635 to your computer and use it in GitHub Desktop.
Microsoft .URL file launcher
MIT License
Copyright (c) 2008 endolith@gmail.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
#!/usr/bin/env python
# Script for launching the URL contained in Microsoft .URL files
# (MIME-type: application/x-mswinurl)
#
# endolith@gmail.com 2008-12-13
#
# The file format is described in http://www.cyanwerks.com/file-format-url.html
# Related: http://ubuntuforums.org/showthread.php?p=3281092
# http://leuksman.com/pages/Linux#Nsurl
# https://bugs.launchpad.net/bugs/185165
import sys
import ConfigParser
import commands
import thread
import webbrowser
def callBrowser(url):
"""Calls an external Browser to upen the URL."""
# Try to find user's preferred browser via xdg-open. If that fails
# (xdg-utils not installed or some other error), fall back to Python's
# semi-optimal solution.
# (From rnghelpers.py in Reportbug-NG (GPL) by Bastian Venthur)
# http://blog.venthur.de/2007/07/01/python-osfork-webbrowser-and-debbugs/
status, output = commands.getstatusoutput('xdg-open "%s"' % url)
if status != 0:
print("xdg-open %s returned (%i, %s), falling back to python's webbrowser.open" % (url, status, output))
thread.start_new_thread(webbrowser.open, (url,))
def getURL(filename):
"""Given a .url filename, returns the main URL, or None if it can't
find one."""
# Windows .URL file format is compatible with built-in ConfigParser class.
config = ConfigParser.RawConfigParser()
try:
config.read(filename)
except:
return None
# Return the URL= value from the [InternetShortcut] section.
if config.has_option('InternetShortcut','url'):
return config.get('InternetShortcut','url').strip('"')
# If there is none, return the BASEURL= value from the [DEFAULT] section.
if 'baseurl' in config.defaults().keys():
return config.defaults()['baseurl'].strip('"')
else:
return None
files = sys.argv[1:]
if files:
for filename in files:
url = getURL(filename)
if url:
callBrowser(url)
else:
print 'No URL found in file', filename
else:
sys.exit("You must provide at least one .url file to launch")
@axs-gentoo
Copy link

Hey Endolith -- could you add a note about a license to this script? Copyright notice alone doesn't really suffice to actually allow its use in linux distributions.

@endolith
Copy link
Author

endolith commented Mar 20, 2018

@axs-gentoo Sorry, I don't get any notifications of comments. It's MIT license.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment