-
Notifications
You must be signed in to change notification settings - Fork 19
/
div.scpt
187 lines (156 loc) · 6.7 KB
/
div.scpt
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
-- check if active application is in full screen mode
on isItFullScreen()
tell application "System Events"
set activeApp to item 1 of (get name of processes whose frontmost is true)
tell process activeApp
get value of attribute "AXFullScreen" of window 1
end tell
end tell
return result as boolean
end isItFullScreen
on getActiveSceenIndex(divObjC)
set screensCount to divObjC's getScreensCount()
set screensSizes to divObjC's getScreensSizes()
set screensOrigins to divObjC's getScreensOrigins()
tell application "System Events"
set activeApp to name of first application process whose frontmost is true
tell process activeApp
if subrole of window 1 is "AXUnknown" then
set activeWindow to 2
else
set activeWindow to 1
end if
set positionOfCurrentApp to position of window activeWindow
set sizeOfCurrentApp to size of window activeWindow
set centerOfCurrentApp to (item 1 of positionOfCurrentApp) + (item 1 of sizeOfCurrentApp / 2)
set activeScreenIndex to 1
set distanceFromDisplayCenter to 9999
repeat with screenIndex from 1 to screensCount
set currentScreenXorigin to item 1 of item screenIndex of screensOrigins
set currentScreenXsize to item 1 of item screenIndex of screensSizes
set currentScreenCenter to currentScreenXorigin + (currentScreenXsize / 2)
set currentScreenCenterDistanceToAppCenter to centerOfCurrentApp - currentScreenCenter
if currentScreenCenterDistanceToAppCenter < 0 then set currentScreenCenterDistanceToAppCenter to -currentScreenCenterDistanceToAppCenter
if currentScreenCenterDistanceToAppCenter < distanceFromDisplayCenter
set activeScreenIndex to screenIndex
set distanceFromDisplayCenter to currentScreenCenterDistanceToAppCenter
end if
end repeat
end tell
end tell
return activeScreenIndex
end getActiveSceenIndex
-- return active screens size
on getScreenSize(divObjC)
set screensCount to divObjC's getScreensCount()
set screensSizes to divObjC's getScreensSizes()
if screensCount is 1
set _width to item 1 of item 1 of screensSizes
set _height to item 2 of item 1 of screensSizes
else
set activeSceenIndex to getActiveSceenIndex(divObjC)
set _width to item 1 of item activeSceenIndex of screensSizes
set _height to item 2 of item activeSceenIndex of screensSizes
end if
return {_width, _height}
end getScreenSize
-- return active screens size
on getScreenOrigin(divObjC)
set screensCount to divObjC's getScreensCount()
set screensSizes to divObjC's getScreensSizes()
set screensOrigins to divObjC's getScreensOrigins()
if screensCount is 1
set _x to item 1 of item 1 of screensOrigins
set _y to item 2 of item 1 of screensOrigins
else
set activeSceenIndex to getActiveSceenIndex(divObjC)
set activeSceenHeight to item 2 of item activeSceenIndex of screensSizes
set mainSceenHeight to item 2 of item 1 of screensSizes
set screensHeightDiff to mainSceenHeight - activeSceenHeight
set _x to item 1 of item activeSceenIndex of screensOrigins
set _y to item 2 of item activeSceenIndex of screensOrigins - screensHeightDiff
end if
return {_x, _y}
end getScreenOrigin
-- convert arguments to list
on converttoList(delimiter, input)
local delimiter, input, ASTID
set ASTID to AppleScript's text item delimiters
try
set AppleScript's text item delimiters to delimiter
set input to text items of input
set AppleScript's text item delimiters to ASTID
return input --> list
on error eMsg number eNum
set AppleScript's text item delimiters to ASTID
error "Can't convert: " & eMsg number eNum
end try
end converttoList
-- resize app window
on resizeApp(positionX, positionY, sizeX, sizeY)
tell application "System Events"
set activeApp to name of first application process whose frontmost is true
tell process activeApp
if subrole of window 1 is "AXUnknown" then
set activeWindow to 2
else
set activeWindow to 1
end if
set position of window activeWindow to {positionX, positionY}
set size of window activeWindow to {sizeX, sizeY}
end tell
end tell
end resizeApp
-- display notification
on displayNotification(_notification, _subtitle)
display notification _notification with title "Div" subtitle _subtitle sound name "Basso"
end displayNotification
-- on script invocation read the passed argumens and assign to userQuery
on run userQuery
-- Load Objective-C script
tell application "Finder"
set myPath to container of (path to me) as text
end tell
set divObjC to load script file (myPath & "divObjC.scptd")
-- set some variables
set screenSize to getScreenSize(divObjC)
set screensOrigin to getScreenOrigin(divObjC)
set args to converttoList(" ", userQuery)
set argsSize to count of args
set isCurrentAppInFullScreenMode to isItFullScreen()
-- warn user that the Div doesn't work in full screen mode
if isCurrentAppInFullScreenMode is true then
set _notification to "Div doesn't work in full screen mode!"
set _subtitle to "Sorry dude :-("
displayNotification(_notification, _subtitle)
return
end if
-- if user provided 4 arguments, resize to custom bounds
if argsSize is 4 then
set positionX to ((item 1 of args / 100) * item 1 of screenSize) + item 1 of screensOrigin
set positionY to ((item 2 of args / 100) * item 2 of screenSize) - item 2 of screensOrigin
set sizeX to ((item 3 of args / 100) - (item 1 of args / 100)) * item 1 of screenSize
set sizeY to ((item 4 of args / 100) - (item 2 of args / 100)) * item 2 of screenSize
resizeApp(positionX, positionY, sizeX, sizeY)
-- if user provided 2 arguments, resize to absolute size on the center of a window
else if argsSize is 2 then
-- if monitor is not big enough to resize
if item 1 of args as number > item 1 of screenSize as number or item 2 of args as number > item 2 of screenSize as number then
set _notification to "Buy a bigger one dude"
set _subtitle to "Screen not big enough :-("
displayNotification(_notification, _subtitle)
-- otherwise resize to desired size
else
set positionX to ((item 1 of screenSize - item 1 of args) / 2) + item 1 of screensOrigin
set positionY to ((item 2 of screenSize - item 2 of args) / 2) - item 2 of screensOrigin
set sizeX to item 1 of args
set sizeY to item 2 of args
resizeApp(positionX, positionY, sizeX, sizeY)
end if
-- Otherwise remmind your user how many arguments is required
else
set _notification to "Two or four arguments (space separated) only!"
set _subtitle to "Sorry dude :-("
displayNotification(_notification, _subtitle)
end if
end run