Use Apple Script to change MacBook view to external monitor

Hi Buddy,

Since there’s an urgent question of apple script to be dealt with, I come here and ask you for help.

I wanna use apple script to change view to external monitor under a certain application, in fact, pressing “command” and “F1” at the same time by manual under the application can achieve the target.

My script is as below.
Now the difficulty is I find the key code of F1 is “122” but it doesn’t work.

If anyone has good idea to use an language to implement the behavior of “press down/up F1”, could you please teach me?

tell application “System Preferences” to activate
tell application “System Events”
tell process “System Preferences”
click menu item “Displays” of menu “View” of menu bar 1
click radio button “Display” of tab group 1 of window “Color LCD”
delay 1
key down command
key code 122 – How to implement F1’s down/up?
key up command
click radio button “Arrangement” of tab group 1 of window “Color LCD”
if value of checkbox “Mirror Displays” of group 1 of tab group 1 of window “Color LCD” is 0 then
click checkbox “Mirror Displays” of group 1 of tab group 1 of window “Color LCD”
end if
delay 5
if value of checkbox “Mirror Displays” of group 1 of tab group 1 of window “AL1715” is 1 then
click checkbox “Mirror Displays” of group 1 of tab group 1 of window “AL1715”
end if
delay 5
end tell
end tell
tell application “System Preferences” to quit

Does anybody know it???

up,up,up.
could anyone help me?:slight_smile:

Hi
try this

tell application "Finder"
	activate
	tell application "System Events" to key code 122 using {command down}
end tell