Rotating displays with AppleScript (Monterey) - applescript

Script and the setting window
Error
How can I click the "Roatation:" button?

tell application id "com.apple.systempreferences"
reveal pane id "com.apple.preference.displays"
activate -- This used to not be necessary
end tell
tell application id "com.apple.systemevents" to tell ¬
process "System Preferences" to tell window 1
tell (a reference to pop up button -1 of group 1)
my (wait for it)
perform action "AXShowMenu"
tell menu 1
my (wait for it)
pick menu item 1
end tell
end tell
end tell
tell application id "com.apple.systempreferences" to quit
to wait for UIElementRef
tell application id "com.apple.systemevents" to repeat 10 times
if the UIElementRef exists then return
delay 0.5
end repeat
error from UIElementRef
end wait
You can change the following line:
pick menu item 1
by replacing the index number 1 with whichever menu item number you require.

Related

How do I double click on an image/or link to initiate installation? APPLESCRIPT [Please view image]

I am trying to automate the installation of slack. I run the .dmg then must double click the "Slack" icon to start the install. using the '''click''' function on images is not really doable im guessing.. I see that the URL this image calls on is file:///Volumes/Slack.app/Slack.app/ Do I need to call on this link perhaps ? How can I get the same results of double clicking on the image.
Accessibility Inspector Screenshot
tell application "Finder"
set myFolder to container of (path to me) as alias
end tell
tell application "Finder"
open document file "Slack-4.3.3-macOS.dmg" of myFolder
end tell
tell application "System Events"
tell process "Finder"
click image "Slack" of group 1 of list 1 of list 1 of scroll area 1 of splitter group 1 of splitter group 1 of window "Slack"
end tell
end tell
Accessibility Inspector Screenshot
If you are still interested in doing this job by clicking the image in AppleScript, you can try code below:
tell application "System Events"
tell its application process "Finder"
tell image "Slack.app" of group 1 of list 1 of list 1 of scroll area 1 of splitter group 1 of splitter group 1 of window "Slack.app"
ignoring application responses
perform action "AXShowMenu"
end ignoring
end tell
end tell
end tell
delay 0.3
do shell script "killall System\\ Events"
delay 0.1
tell application "System Events"
tell its application process "Finder"
tell image "Slack.app" of group 1 of list 1 of list 1 of scroll area 1 of splitter group 1 of splitter group 1 of window "Slack.app"
--key code 125 --also can try use arrow key to select item in context menu
keystroke "Open"
delay 0.2
keystroke return
end tell
end tell
end tell
I test this code on my MacOS 10.12.6, it works OK. Even though it's easy to use terminal command to do this job, this method involves some very useful AS knowledge point.
1st : perform action "AXOpen" and "AXShowMenu"
2nd : show context menu and select menu item
3rd : deal with system delay
If it doesn't have delay issue, you can try the code below, which is more simpler,(also works on my machine):
tell application "System Events"
tell its application process "Finder"
tell image "Slack.app" of group 1 of list 1 of list 1 of scroll area 1 of splitter group 1 of splitter group 1 of window "Slack.app"
perform action "AXShowMenu"
delay 0.3
keystroke "Open"
delay 0.2
keystroke return
end tell
end tell
end tell

Can't get a menu (or menu item) of a pop up button in Logic Pro X using AppleScript

so basically i want to write an AppleScript that launches a plugin in Logic Pro X, but even after clicking the pop up button, it can't reach the menu (therefore none of the menu items (plug-ins))
this is the menu
here is what i have tried, i found where the "Audio FX" pop up button is and i can click it, but i want to know if there is any way to reach menu 1 of it? according to automator's watch-me-do it should be there but for some reason i can't get into the menu
tell application "Logic Pro X" to activate
tell application "System Events"
tell process "Logic Pro X"
set frontmost to true
tell (first window whose subrole is "AXStandardWindow")
tell group "Audio FX" of group 1 of group 1 of list 1 of list 1 of group 2
tell pop up button 1
click
click menu item 3 of menu 1
end tell
end tell
end tell
end tell
end tell
so the error that i get is this:
error "System Events got an error: Can’t get menu 1 of pop up button 1 of group \"Audio FX\" of group 1 of group 1 of list 1 of list 1 of group 2 of window 1 of process \"Logic Pro X\" whose subrole = \"AXStandardWindow\". Invalid index." number -1719
I agree with #Ted Wrigley 's comment about it may be an issue of race condition. Something like this may work for you.
tell application "Logic Pro X" to activate
tell application "System Events"
tell process "Logic Pro X"
set frontmost to true
tell (first window whose subrole is "AXStandardWindow")
tell group "Audio FX" of group 1 of group 1 of list 1 of list 1 of group 2
repeat while not (exists of pop up button 1)
delay 0.1
end repeat
tell pop up button 1
click
repeat while not (exists of menu item 3 of menu 1)
delay 0.1
end repeat
click menu item 3 of menu 1
end tell
end tell
end tell
end tell
end tell

Apple Script to access elements of the Context Menu for a file

I was able to get the context menu up with applescript
tell perform action "AXShowMenu"
tell application "System Events" to tell process "Finder" tell window {window name} tell splitter group 1 tell splitter group 1 UI elements tell scroll area 1 UI elements tell group 1 UI elements tell image {filename} perform action "AXShowMenu" end tell end tell end tell end tell end tell end tell
This got the menu up. But I am not able to access any of the menu items. How can I access menu items for a file in finder?
You can use keystroke to select the menu item:
set filename to "test.jpg"
tell application "Finder" to activate
delay 0.2
tell application "System Events"
tell process "Finder"
-- set theTarget to value of attribute "AXFocusedUIElement"
set theTarget to group 1 of scroll area 1 of splitter group 1 of splitter group 1 of window 1
tell theTarget
tell image filename to perform action "AXShowMenu"
delay 0.2
keystroke "open"
keystroke return
end tell
end tell
end tell

How to download a new voice for say's function with AppleScript?

I'm searching a method to install different voices (2 to be exact) on the system preferences.
The voices are "Alex" for an english voice and "Thomas" for a french voice.
I've tried directly by console but didn't succeed, that's why I've turned to the AppleScript language, but I never used this language.
The code I've for the moment is
set osver to system version of (system info)
if osver is equal to "10.6.8" then
display dialog ("Downloading voices is only available in OS X Lion and higher")
else
tell application "System Preferences"
activate
reveal (pane id "com.apple.preference.speech")
end tell
try
tell application "System Events"
click radio button 2 of tab group 1 of window 1 of process "System Preferences"
repeat until (exists pop up button of tab group 1 of window 1 of process "System Preferences")
delay 2
end repeat
delay 2
click pop up button 1 of tab group 1 of window 1 of process "System Preferences"
delay 2
click menu item -1 of menu 1 of pop up button of tab group 1 of window 1 of process "System Preferences"
delay 2
end tell
on error
display dialog ("An error happend")
end try
end if
This program is opening the voice window but the display dialog appears every time whatever the index I put.
If you have another idea to download the voices, or if you can help me to understand what is not working, I will be grateful.
This worked for me in 10.9:
tell application "System Preferences"
reveal anchor "TTS" of pane id "com.apple.preference.speech"
activate
end tell
tell application "System Events" to tell window 1 of process "System Preferences"
tell pop up button 1 of tab group 1
click
click menu item "Customize..." of menu 1
end tell
delay 1
repeat with r in UI element 1 of rows of table 1 of scroll area 1 of sheet 1
if exists static text 1 of r then
if {"Alex", "Thomas"} contains value of static text 1 of r then
if value of checkbox 1 of r is 0 then click checkbox 1 of r
end if
end if
end repeat
click button "OK" of sheet 1
end tell
It took multiple seconds to run the script though.

Applescript delay issue

I am testing applescripts that I will use later in my OSX app.
I'm getting a 6 sec delay after the click button command below.
After some research it seems that this is a known issue.
What I find interesting is, if i use the commercial app QuicKeys to perform the same
button click there is no delay, so I assume they found a work around.
Anybody have any ideas?
tell application "System Events"
tell process "Pro Tools"
set frontmost to 1
click button "Track List pop-up" of window 1
-- 6 seconds delay before next command is sent
key code 36 -- return key stroke
end tell
end tell
Was having the same problem and resolved it by enclosing the click causing delay in the ignoring application responses block. Here is a quick summary:
OLD CODE (Causes 6 sec delay)
tell application "System Events" to tell process "SystemUIServer"
set bt to (first menu bar item whose description is "bluetooth") of menu bar 1
click bt
tell (first menu item whose title is "SBH80") of menu of bt
click
tell menu 1
if exists menu item "Disconnect" then
click menu item "Disconnect"
else
click menu item "Connect"
end if
end tell
end tell
end tell
NEW CODE (No delay)
tell application "System Events" to tell process "SystemUIServer"
set bt to (first menu bar item whose description is "bluetooth") of menu bar 1
ignoring application responses
click bt
end ignoring
end tell
do shell script "killall System\\ Events"
delay 0.1
tell application "System Events" to tell process "SystemUIServer"
tell (first menu item whose title is "SBH80") of menu of bt
click
tell menu 1
if exists menu item "Disconnect" then
click menu item "Disconnect"
else
click menu item "Connect"
end if
end tell
end tell
end tell
Please check detailed answer in the thread listed below.
Speed up AppleScript UI scripting?
Hope this helps.
It seems click or axpress causes a big delay.
Instead - get position and use a third party shell script to do the clicking. Much Much faster.
using clicclik : https://www.bluem.net/en/mac/cliclick/
put in user library/application support/Click
set clickCommandPath to ((path to application support from user domain) as string) & "Click:cliclick"
set clickCommandPosix to POSIX path of clickCommandPath
tell application "System Events"
tell process "Pro Tools"
set frontmost to 1
tell button "Track List pop-up" of window 1
set {xPosition, yPosition} to position
set x to xPosition
set y to yPosition
end tell
do shell script quoted form of clickCommandPosix & " c:" & xPosition & "," & yPosition
key code 36 -- return key stroke
end tell
end tell

Resources