Skip to content

Instantly share code, notes, and snippets.

@pjbriggs
Created July 2, 2014 13:26
Show Gist options
  • Save pjbriggs/89a664dde4048c86afa1 to your computer and use it in GitHub Desktop.
Save pjbriggs/89a664dde4048c86afa1 to your computer and use it in GitHub Desktop.
Open multiple arbitrary URLs automatically in a webbrowser
#!/bin/env python
#
# Open multiple arbitrary URLs in a webbrowser
import optparse
import webbrowser
import time
import sys
if __name__ == '__main__':
p = optparse.OptionParser(usage="%prog [-f FILE] [-b BROWSER] [-p INTERVAL] "
"[URL [URL,...]]",
description="Launch URLs supplied on command line in "
"BROWSER (or read URLS from FILE)")
p.add_option('-b','--browser',action='store',dest='browser',default='firefox',
help="browser to load URLs into (default is firefox)")
p.add_option('-f','--file',action='store',dest='file',default=None,
help="read URLs from FILE (one URL per line)")
p.add_option('-p','--pause',action='store',dest='interval',default=5,type='int',
help="time to pause between opening each URL (seconds; default is 5s)")
options,args = p.parse_args()
urls = []
for arg in args:
urls.append(arg)
if options.file is not None:
print "Reading URLs from file '%s'" % options.file
for line in open(options.file,'rU'):
if line.startswith('#'):
continue
urls.append(line.rstrip('\n'))
if not urls:
print "No URLs specified; nothing to do"
sys.exit(0)
print "Fetching browser instance '%s'" % options.browser
try:
browser = webbrowser.get(options.browser)
except webbrowser.Error,ex:
print "Unable to acquire browser instance for '%s'" % options.browser
print "Exception: %s" % ex
sys.exit(1)
print "Opening new browser window"
browser.open(urls[0],new=1,autoraise=True)
for url in urls[1:]:
if options.interval:
time.sleep(options.interval)
print "Opening %s" % url
browser.open_new_tab(url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment