Update: I have a script that works, just not inside Keyboard Maestro.
After reading this post, I have a bare bones script.
It works when I run it from Script Editor.
It doesn’t work when I run it from Keyboard Maestro. It opens up the menu, but doesn’t click anything.
Script:
set deviceName to "NAME OF iPad"
tell application "System Events" to tell process "QuickTime Player"
set frontmost to true
end tell
ignoring application responses
tell application "System Events" to tell process "QuickTime Player"
click button 2 of window 1
delay 1
end tell
end ignoring
do shell script "killall 'System Events'"
tell application "System Events" to tell process "QuickTime Player"
set inputMenu to menu 1 of button 2 of window 1
set inputList to name of every menu item of inputMenu
repeat with menuItemPosition from 1 to length of inputList
if item menuItemPosition of inputList is deviceName then
ignoring application responses
click menu item menuItemPosition of inputMenu
exit repeat
end ignoring
end if
end repeat
end tell
I think the ignorings, the shell killall, and repeated tell blocks are causing timing issues. Try this:
set deviceName to "NAME OF iPad"
tell application "System Events" to tell process "QuickTime Player"
set frontmost to true
click button 2 of window 1
delay 1
set inputMenu to menu 1 of button 2 of window 1
set inputList to name of every menu item of inputMenu
repeat with menuItemPosition from 1 to length of inputList
if item menuItemPosition of inputList is deviceName then
click menu item menuItemPosition of inputMenu
exit repeat
end if
end repeat
end tell
For reference, I asked Gemini to tell me why @Nige_S’s version works inside Keyboard Maestro:
The first script functioned in Script Editor because that app runs in a dedicated foreground environment that can often re-establish a connection after a crash; however, it failed in Keyboard Maestro because KM executes scripts as a background process that relies on a continuous, stable link to the System Events bridge. When the first script invoked killall 'System Events', it effectively "pulled the rug out" from under Keyboard Maestro’s runner, severing the macro's ability to communicate with the UI before the second half of the script could execute. The second script works by maintaining a single, uninterrupted communication channel, ensuring that Keyboard Maestro never loses its handle on the QuickTime menu it is trying to control.
That's not the full story, at least for me -- if I comment out the do shell script line the script still fails in KM, even though System Events isn't being killed. I'm finding that the ignoring application responses around the click menu item... line also cause it to fail.
Ignoring responses can speed a script up -- you're basically saying "Don't wait for the target to say it's done this -- go to the next step immediately". But when you're scripting the UI like this you often need to know the previous step has completed -- you can't click a menu item until the menu item is there!