-
Notifications
You must be signed in to change notification settings - Fork 0
/
openChrome
executable file
·144 lines (113 loc) · 4.02 KB
/
openChrome
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/python
# Copyright 2014 Alex K (wtwf.com)
"""
Open Chrome in a profile based on URL pattern matching, see https://github.com/arkarkark/openChrome.app
Usage: openChrome http://facebook.com/
Set as your default web opener with https://github.com/Lord-Kamina/SwiftDefaultApps/releases
also see https://crbug.com/549275
http://superuser.com/questions/373701/create-bash-script-to-open-url-in-mac-os-x
http://apple.stackexchange.com/questions/32386/how-to-register-an-applescript-as-a-potential...
"""
__author__ = "wtwf.com (Alex K)"
import getopt
import json
import logging
import os
import re
import subprocess
import sys
PROFILE_DIR = "~/Library/Application Support/Google/Chrome"
CHROME_BIN = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
DEFAULT_PROFILE = "Default"
def usage(code, msg=""):
"""Show a usage message."""
if code:
out = sys.stderr
else:
out = sys.stdout
PROGRAM = os.path.basename( # pylint: disable=invalid-name,possibly-unused-variable
sys.argv[0]
)
print >> out, __doc__ % locals()
if msg:
print >> out, msg
sys.exit(code)
def open_url_in_profile(profile, url):
"""Open up a url in a specific profile window"""
logging.info("profile %s url %s", profile, url)
subprocess.call([CHROME_BIN, url, "--profile-directory=%s" % profile])
subprocess.Popen(["open", "-a", "Google Chrome"]) # to get focus
def get_profile_dir_for_email_address(email):
"""A map from email address to profile directory name."""
for dirname in os.listdir(PROFILE_DIR):
prefs_file_name = os.path.join(PROFILE_DIR, dirname, "Preferences")
if os.path.exists(prefs_file_name):
prefs = json.load(open(prefs_file_name))
for acct in prefs.get("account_info") or []:
if acct.has_key("email") and acct["email"] == email:
return dirname
return None
def get_open_chrome_rc():
"""a simple json file with an object (see README.md)."""
return json.load(open(os.path.expanduser("~/.openchromerc")))
def open_url(url):
"""Choose a profile to open this url in."""
global PROFILE_DIR, CHROME_BIN
config = get_open_chrome_rc()
PROFILE_DIR = os.path.expanduser(config.get("profileDirectory", PROFILE_DIR))
CHROME_BIN = os.path.expanduser(config.get("chromeBinary", CHROME_BIN))
if config.has_key("rules"):
rules = config["rules"]
else:
logging.fatal(
"~/.openchromerc must have `rules`, see https://github.com/arkarkark/openChrome.app"
)
profile = None
rule = None
for rule in rules:
pattern = rule.get("pattern")
if not pattern or re.search(pattern, url, re.VERBOSE | re.IGNORECASE):
break
profile = None
if not rule:
profile = DEFAULT_PROFILE
elif rule.has_key("profile"):
profile = rule["profile"]
elif rule.has_key("email"):
profile = get_profile_dir_for_email_address(rule["email"])
elif rule.has_key("run"):
open_shell(rule["run"], url)
return
else:
logging.fatal(
".openchromerc entry must have either `profile` or `email` entry (rule: %r)",
rule,
)
if profile:
open_url_in_profile(profile, url)
else:
logging.fatal("Unable to find profile for: %r", rule)
def main():
"""Run."""
logging.basicConfig() # filename='/tmp/openChrome.log')
logging.getLogger().setLevel(logging.DEBUG)
try:
opts, args = getopt.getopt(sys.argv[1:], "h", "help".split(","))
except getopt.error as msg:
usage(1, msg)
if len(args) != 1:
usage(1)
url = args[0]
for opt, _ in opts:
if opt in ("-h", "--help"):
usage(0)
if not url:
usage(2, "you must provide a url")
open_url(url)
def open_shell(cmds, url):
"""Choose a profile to open this url in."""
cmds = [item % locals() for item in cmds]
logging.info("Opening: %r", cmds)
subprocess.Popen(cmds)
if __name__ == "__main__":
main()