Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 105 lines (80 sloc) 2.849 kb
8ef4cc5d »
2011-05-07 doc. update
1 #!/usr/bin/env python
2
3 """
4 Open URLs in tabs
5 =================
6 Author: Laszlo Szathmary, 2011 (jabba.laci@gmail.com)
7 Website: https://ubuntuincident.wordpress.com/2011/03/09/open-urls-in-browser-tabs-simultaneously/
8 GitHub: https://github.com/jabbalaci/Bash-Utils
9
10 Read URLs from the standard input and open them in separated tabs.
11
12 Usage:
13 ------
14 cat url_list.txt | ./open_in_tabs.py
15
16 Options:
17 -n, --new-window Open URLs in a new browser window.
18 -s, --simultaneously Open URLs simultaneously.
5f5974e1 »
2012-08-09 linted
19
8ef4cc5d »
2011-05-07 doc. update
20 Warning! The combination -ns is experimental!
21 """
22
23 import webbrowser
24 import sys
25 import commands
26 import shlex
27
28 from optparse import OptionParser
29 from subprocess import call
30 from time import sleep
31
32 __version__ = '0.3.0'
33
34 FIREFOX = webbrowser.get('firefox')
f18ad4f1 »
2011-10-29 reddit_top10 added
35 FIREFOX_PROCESS = 'firefox' # look for it in the output of 'ps'
8ef4cc5d »
2011-05-07 doc. update
36 FIREFOX_EXE = 'firefox' # Firefox executable
37 DELAY = 3.0 # seconds
38
39
40 def is_firefox_running():
41 output = commands.getoutput('ps ux')
42 return FIREFOX_PROCESS in output
5f5974e1 »
2012-08-09 linted
43
44
8ef4cc5d »
2011-05-07 doc. update
45 def open_in_new_window(url, options, arguments):
46 if options.simultaneously:
47 command = "{0} -new-window {1}".format(FIREFOX_EXE, url)
48 call(shlex.split(command))
49 sleep(DELAY)
50 else:
51 FIREFOX.open_new(url)
5f5974e1 »
2012-08-09 linted
52
53
8ef4cc5d »
2011-05-07 doc. update
54 def open_in_new_tab(url, options, arguments):
55 if options.simultaneously:
56 command = "{0} -new-tab {1}".format(FIREFOX_EXE, url)
57 call(shlex.split(command))
58 else:
59 FIREFOX.open_new_tab(url)
5f5974e1 »
2012-08-09 linted
60
8ef4cc5d »
2011-05-07 doc. update
61
62 def main():
63 parser = OptionParser(usage='%prog [options]', version=__version__)
64
65 #[options]
66 parser.add_option('-n',
67 '--new-window',
68 action='store_true',
69 default=False,
70 help='Open URLs in a new browser window.')
71 parser.add_option('-s',
72 '--simultaneously',
73 action='store_true',
74 default=False,
75 help='Open URLs simultaneously.')
76
77 options, arguments = parser.parse_args()
5f5974e1 »
2012-08-09 linted
78
8ef4cc5d »
2011-05-07 doc. update
79 if options.new_window and options.simultaneously:
80 print >>sys.stderr, \
81 "# {0}: this combination is experimental.".format(sys.argv[0])
82 #sys.exit(2)
5f5974e1 »
2012-08-09 linted
83
8ef4cc5d »
2011-05-07 doc. update
84 if not is_firefox_running():
85 print >>sys.stderr, \
86 "{0}: error: firefox is not running.".format(sys.argv[0])
87 sys.exit(1)
88
89 first = True
90 for url in sys.stdin.readlines():
91 url = url.rstrip("\n")
92
93 if options.new_window:
94 if first:
95 open_in_new_window(url, options, arguments)
96 first = False
97 else:
98 open_in_new_tab(url, options, arguments)
99 else:
100 open_in_new_tab(url, options, arguments)
5f5974e1 »
2012-08-09 linted
101
8ef4cc5d »
2011-05-07 doc. update
102 return 0
103
104 if __name__ == '__main__':
105 sys.exit(main())
Something went wrong with that request. Please try again.