-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathopen_airplay_menu.applescript
161 lines (140 loc) · 5.58 KB
/
open_airplay_menu.applescript
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
-- A click event works normally if screen mirroring is already in main menu bar
-- but the icon may not be in the menu bar if it hasn't been placed there already.
-- So we check if the Screen Mirroring item is in the menu bar and click it there
-- if not we go through the Control Center drop down.
-- pass name of airplay device with commandline argument to autoselect that device.
-- if nothing is passed it'll just open the Screen Mirroring menu without selecting a device
use framework "Foundation"
use scripting additions
-- passing commandline argument
-- https://stackoverflow.com/questions/57690558/compile-applescript-into-application-with-parameters
set arguments to (current application's NSProcessInfo's processInfo's arguments) as list
if first item of arguments contains "osascript" then set arguments to rest of arguments -- skip osascript path
if (count arguments) is 1 then set end of arguments to "no arguments"
repeat with anItem in rest of arguments -- skip the main executable path
set airPlayDevice to anItem
end repeat
# osascript still returns the last result
tell application "System Events"
tell its application process "ControlCenter"
set osVersion to get system version of (system info)
-- log osVersion
set status to ""
-- click on Screen Mirroring dropdown if it's on the menu bar
if exists (first UI element of menu bar 1 whose value of attribute "AXIdentifier" contains "screen-mirroring") then
set screenMirroringDropDownButton to first UI element of menu bar 1 whose value of attribute "AXIdentifier" contains "screen-mirroring"
click screenMirroringDropDownButton
delay 1
set window_ to title of its window as string
-- click on the Control Center drop down on menu bar
else
set controlCenter to first UI element of menu bar 1 whose value of attribute "AXIdentifier" contains "controlcenter"
click controlCenter
delay 1
set window_ to title of its window as string
set status to controlCenterDropDown(osVersion, window_) of me as string
end if
if status is not "failed" then
getScreenMirroringDropDown(osVersion, airPlayDevice, window_) of me
end if
end tell
end tell
return
-- "click" on screen mirroring on the Control Center drop down
on controlCenterDropDown(osVersion, window_)
tell application "System Events"
tell its application process "ControlCenter"
tell its window window_
-- clicking doesn't actually work on the screen mirroring button(checkbox)
-- in the Control Center dropdown so it's click action is "perform action 1" or "perform action 2" depending on system version
-- we will also find the Screen Mirroring by using AXIdentifier or title
try
-- ventura
if osVersion ³ 13 then
set controlCenterElements to UI elements of group 1
set myattribute to "AXIdentifier"
set myaction to 1
-- Monterey
else if osVersion < 13 and osVersion ³ 12 then
set controlCenterElements to UI elements
set myattribute to "AXIdentifier"
set myaction to 2
-- big sur
else
set controlCenterElements to UI elements of group 1 of group 1
set myattribute to "AXTitle"
set myaction to 1
end if
on error
log "Error getting screen mirroring button"
return "failed"
end try
-- go through the UI elements of Control Center drop down and "click" on the screen mirroring button
repeat with anItem in controlCenterElements
try
if exists attribute myattribute of anItem then
if value of attribute myattribute of anItem contains "screen-mirroring" or value of attribute myattribute of anItem contains "Screen Mirroring" then
perform action myaction of anItem
exit repeat
end if
end if
on error
log "error clicking screen mirroring"
return "failed"
end try
end repeat
delay 1
end tell
end tell
end tell
return
end controlCenterDropDown
on getScreenMirroringDropDown(osVersion, airPlayDevice, window_)
tell application "System Events"
tell its application process "ControlCenter"
tell its window window_
set screenMirroringDropDown to ""
try
-- get ui elements of screen mirroring drop down
-- Ventura
if osVersion ³ 13 then
set x to UI elements --?? if in menu bar this is needed ??
set screenMirroringDropDown to UI elements of group 1
-- Monterey
else if osVersion < 13 and osVersion ³ 12 then
set x to UI elements --?? if in menu bar this is needed ??
set screenMirroringDropDown to UI elements
-- big sur
else
set screenMirroringDropDown to UI elements of group 1
end if
on error
log "Error getting UI elements of screen mirroring drop down."
return
end try
repeat with anItem in screenMirroringDropDown
try
set itemsOfScreenMirroringMenu to value of attribute "AXChildren" of anItem
-- find screen mirroring device and click
repeat with childItem in itemsOfScreenMirroringMenu
if (exists attribute "AXIdentifier" of childItem) then
set aScreenMirroringItem to value of attribute "AXIdentifier" of childItem
-- log aScreenMirroringItem
else
set aScreenMirroringItem to title of childItem
-- log aScreenMirroringItem
end if
if aScreenMirroringItem ends with airPlayDevice then
click childItem
exit repeat
end if
end repeat
on error
log "error clicking on or setting device in Screen Mirroring drop down "
end try
end repeat
end tell
end tell
end tell
return
end getScreenMirroringDropDown