-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract.py
executable file
·169 lines (154 loc) · 6.79 KB
/
extract.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env python3
# extract.py
# Extract user screen dimensions and title of Chrome & Terminal apps.
import subprocess, sys, os
import platform
# check platform
if platform.system() == 'Darwin':
from AppKit import NSScreen # get native resolution
# grab width and height for user
width = int(NSScreen.mainScreen().frame().size.width)
height = int(NSScreen.mainScreen().frame().size.height)
with open('applicationName.txt', 'r') as file:
# parse and stdout
def parse(applescript):
try:
args = [item for x in [("-e",l.strip()) for l in applescript.split('\n') if l.strip() != ''] for item in x]
proc = subprocess.Popen(["osascript"] + args , stdout=subprocess.PIPE)
progname = proc.stdout.read().strip()
sys.stdout.write(str(progname))
except Error as error:
print(f'error: {err}'.format(err=error))
# read open application name
applicationName = file.read().replace('\n', '')
# dual option selected through argument change
if str(sys.argv[1]) == "mono":
num_of_terminals = int(subprocess.check_output("bash openterminals.sh", shell=True))
if num_of_terminals == 1:
mono_applescript = '''\
tell application "Terminal" to activate
tell application "System Events" to keystroke "t" using command down
repeat while contents of selected tab of window 1 starts with linefeed
delay 0.01
end repeat
do script "" in front window
activate
set bounds of front window to {{0, 0, {W_terminal}, {H_terminal}}}
end tell\
'''.format(W_terminal=str(width), H_terminal=str(height))
# tell application "Terminal"
# activate
# tell window 1 of application
# do script " "
# activate
# set bounds of front window to {{0, 0, {W_terminal}, {H_terminal}}}
# windows where name contains "bash"
# if result is not {{}} then set index of item 1 of result to 1
# end tell\
else:
mono_applescript = '''\
tell application "Terminal"
activate
set bounds of front window to {{0, 0, {W_terminal}, {H_terminal}}}
end tell\
'''.format(W_terminal=str(width), H_terminal=str(height))
parse(mono_applescript)
elif str(sys.argv[1]) == "dual":
try:
num_of_terminals = int(subprocess.check_output("bash openterminals.sh", shell=True))
except ValueError:
num_of_terminals = 2
if num_of_terminals == 2:
dual_applescript = '''\
tell application "{application}"
set bounds of front window to {{0, 0, {W_chrome}, {H_chrome}}}
end tell
tell application "Terminal"
set bounds of front window to {{{leftPos_terminal}, 0, {W_terminal}, {H_terminal}}}
end tell\
'''.format(application=str(applicationName), W_chrome=str((width//2) - 1), H_chrome=str(height), leftPos_terminal=str(width//2), W_terminal=str(((width//2) - 1) + (width//2)), H_terminal=str(height))
elif num_of_terminals == 1:
dual_applescript = '''\
tell application "{application}"
set bounds of front window to {{0, 0, {W_chrome}, {H_chrome}}}
end tell
tell application "Terminal"
activate
do script " "
activate
set bounds of front window to {{{leftPos_terminal}, 0, {W_terminal}, {H_terminal}}}
end tell\
'''.format(application=str(applicationName), W_chrome=str((width//2) - 1), H_chrome=str(height), leftPos_terminal=str(width//2), W_terminal=str(((width//2) - 1) + (width//2)), H_terminal=str(height))
else:
dual_applescript = '''\
tell application "{application}"
set bounds of front window to {{0, 0, {W_chrome}, {H_chrome}}}
end tell
tell application "Terminal"
set bounds of front window to {{{leftPos_terminal}, 0, {W_terminal}, {H_terminal}}}
end tell\
'''.format(application=str(applicationName), W_chrome=str((width//2) - 1), H_chrome=str(height), leftPos_terminal=str(width//2), W_terminal=str(((width//2) - 1) + (width//2)), H_terminal=str(height))
parse(dual_applescript)
# tripend option selected through argument change
elif str(sys.argv[1]) == "trisnap":
try:
num_of_terminals = int(subprocess.check_output("bash openterminals.sh", shell=True))
except ValueError:
num_of_terminals = 2
num_of_terminals = 2
if num_of_terminals == 2:
trisnap_applescript = '''\
tell application "{application}"
set bounds of front window to {{0, 0, {W_chrome}, {H_chrome}}}
end tell
tell application "Terminal"
set bounds of front window to {{{leftPos_terminal}, 0, {W_terminal}, {H_terminal}}}
do script " "
activate
set bounds of front window to {{{leftPos_terminal}, {H_terminal}, {W_terminal}, {H_terminal2}}}
end tell\
'''.format(application=str(applicationName), W_chrome=str((width//2) - 1), H_chrome=str(height), leftPos_terminal=str(width//2), W_terminal=str(((width//2) - 1) + (width//2)), H_terminal=str(height//2), H_terminal2=str(height))
elif num_of_terminals == 1:
trisnap_applescript = '''\
tell application "{application}"
set bounds of front window to {{0, 0, {W_chrome}, {H_chrome}}}
end tell
tell application "Terminal"
activate
do script " "
activate
set bounds of front window to {{{leftPos_terminal}, 0, {W_terminal}, {H_terminal}}}
end tell
tell application "Terminal"
activate
do script " "
activate
set bounds of front window to {{{leftPos_terminal}, {H_terminal}, {W_terminal}, {H_terminal2}}}
end tell\
'''.format(application=str(applicationName), W_chrome=str((width//2) - 1), H_chrome=str(height), leftPos_terminal=str(width//2), W_terminal=str(((width//2) - 1) + (width//2)), H_terminal=str(height//2), H_terminal2=str(height))
else:
trisnap_applescript = '''\
tell application "{application}"
set bounds of front window to {{0, 0, {W_chrome}, {H_chrome}}}
end tell
tell application "System Events" to tell process "Terminal"
tell window 1
set position to {{{leftPos_terminal}, 0}}
set size to {{{W_terminal}, {H_terminal}}}
end tell
tell window 2
set position to {{{leftPos_terminal}, {H_terminal}}}
set size to {{{leftPos_terminal}, {H_terminal}}}
end tell
end tell\
'''.format(application=str(applicationName), W_chrome=str((width//2) - 1), H_chrome=str(height), leftPos_terminal=str(width//2), W_terminal=str(((width//2) - 1) + (width//2)), H_terminal=str(height//2))
parse(trisnap_applescript)
elif platform.system() == 'Windows':
import ctypes
ctypes.windll.user32.MessageBoxW(0, "Error", "Please use Mac for snapgrid. Do not attempt to run on Windows or Linux.", 0)
elif platform.system() == 'Linux':
from tkinter import messagebox as tkMessageBox
tkMessageBox.showerror('Error', 'Please use Mac for snapgrid. We are not cross-platform.. even for Linux.')
else:
from tkinter import messagebox as tkMessageBox
tkMessageBox.showerror('Error', 'Please use Mac for snapgrid. We are not cross-platform.')