This repository has been archived by the owner on Oct 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweetbot.py
46 lines (39 loc) · 1.47 KB
/
tweetbot.py
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
import os
from subprocess import Popen, PIPE
from urlparse import urlparse
# Set Default Browser
browser = "WebKit"
# browser = "Safari"
# Get url address from safari / webkit
cmd = "/usr/bin/osascript -e 'tell application \"%s\"' -e 'get URL of current tab of window 1' -e 'end tell'" % browser
pipe = Popen(cmd, shell=True, stdout=PIPE).stdout
url = pipe.readlines()[0].rstrip('\n')
# Get index of current tab
cmd = "/usr/bin/osascript -e 'tell application \"%s\"' -e 'get index of current tab of window 1' -e 'end tell'" % browser
pipe = Popen(cmd, shell=True, stdout=PIPE).stdout
tindex = pipe.readlines()[0].rstrip('\n')
# Parse URL
o = urlparse(url)
string = ''
if 'twitter' in o.netloc:
if o.path != '/':
if 'status' in o.path:
# Open specific tweet in tweetbot
string = "tweetbot:/%s" % o.path
else:
# Open user profile in tweetbot
string = "tweetbot:///user_profile/%s" % o.path
else:
print "Not a Twitter URL..."
# Open Twitter URL in Tweetbot
if string != '':
cmdList = []
# Send to tweetbot, close blank safari window, switch back to original tab
cmdList.append("""osascript -e 'tell application \"%s\" to open location \"%s\"'""" % (browser, string))
cmdList.append("""osascript -e 'tell application \"%s\" to close current tab of window 1'""" % browser)
cmdList.append("""osascript -e 'tell front window of application \"%s\" to set current tab to tab %s'""" % (browser, tindex))
# Execute Commands
for cmd in cmdList:
os.system(cmd)
else:
print "Exiting"