Reputation: 35
I'm writing a script in AppleScript that ends with a button press in an application. The issue, however, is that the button has no Attribute Title. I've run Accessibility Inspector and UI Browser over it and the AXTitle is <empty string>.
I can call the button by its number, but everyone on my team has this application set up slightly differently based on their own workflow and the stupid button has a different identifying number depending on where the window is. The window can be a detached pop-up, or attached to the main application window on either the left or the right. All three options yield a different index address.
Button Index when attached to the left:
tell application "System Events" to tell process "Application Name"
click button 8 of splitter group 1 of front window of application
process "Application Name" of application "System Events"
end tell
Button Index when attached to the right:
tell application "System Events" to tell process "Application Name"
click button 9 of splitter group 1 of front window of application
process "Application Name" of application "System Events"
end tell
Button Index when window is detached and floating:
tell application "System Events" to tell process "Application Name"
click button 8 of front window of application process "Application
Name" of application "System Events"
end tell
The constant between the locations is the button label and the button identifier, but I'm having trouble getting script editor to recognize the button based on the Identifier or Prepared Response. I don't know if it's just not possible or if I'm simply writing the code wrong.
When I try to use either the Label or Identifier I get the errors
click button "Prepared Response" of front window
Script Error System Events got an error: Can’t get button "Prepared Response" of window 1 of process "Application Name".
click button "_NS:667" of front window
Script Error System Events got an error: Can’t get button "_NS:667" of window 1 of process "Application Name".
click button with "Prepared Response" of front window
Syntax Error Expected “into”, variable name, class name, other parameter name or property but found “"”.
click button with label "Prepared Response" of front window
Syntax Error Expected “given”, “with”, “without”, other parameter name, etc. but found “"”.
The only two restrictions are that the code has to be AppleScript - not Javascript - and that I can't require my team members to install an application
Upvotes: 2
Views: 5020
Reputation: 33359
I find get properties
useful for debugging:
tell application "System Events" to tell process "Application Name"
get properties of every UI element of front window
end tell
You'll get a bunch of junk, and nearly all of it might be (missing value)
for the app you're trying to script. But some of them will be filled out, such as description
. Once you've figured that out, you can cut the all the noise out by doing:
tell application "System Events" to tell process "Application Name"
get description of every UI element of front window
end tell
However, UI elements are nested. For example you won't get the toolbar buttons, you might just get "toolbar"
as the description of the second element. Not very useful...
To get the toolbar buttons you can do:
tell application "System Events" to tell process "Application Name"
get description of every UI element of toolbar 1 of front window
end tell
From there, you should easily be able to find the toolbar button you want. Then it's a simple matter of doing:
tell application "System Events" to tell process "Application Name"
repeat with uiElement in UI elements of toolbar 1 of front window
if description of uiElement is "Do The Thing" then
click uiElement
end if
end repeat
end tell
Upvotes: 1
Reputation: 3142
Here is an approach that may work for you.
AppleScript has the ability to change property
values and save those new property
values on every run of the script. However, if the script gets recompiled at any time, those new property
values that have been changed will be lost and set back to their original values.
In this following code I set the variable property launchCount : 0
and a few lines down I set another variable set launchCount to launchCount + 1
. So every time the script runs, the property launchCount : 0
will change from 0 to 1 then 2 then 3 etc...
The purpose of this approach is is that with each individual computer that runs the script for the first time, A choose from list
dialog appears, asking the user to select their window location. That choice will then be stored in another variable property windowIsLocated
. Next was to set up a handler with a conditional clause... ex: if windowIsLocated is this then do that else if windowIsLocated is that then do this
property launchCount : 0
property windowIsLocated : missing value
set launchCount to launchCount + 1
set theList to {"Window Attached To Left", "Window Attached To Right", "Window Is Floating"}
if launchCount is 1 then
set windowLocation to choose from list theList ¬
with title "Window Location" with prompt ¬
"Please Choose The Location Of Your Window" OK button name ¬
"OK" cancel button name ¬
"Cancel" multiple selections allowed false ¬
without empty selection allowed
set windowIsLocated to item 1 of windowLocation
end if
clickTheButton() -- use this anywhere in your script to click the button
to clickTheButton()
if windowIsLocated is "Window Attached To Left" then
tell application "Application Name"
activate
repeat until frontmost
delay 0.1
end repeat
end tell
tell application "System Events" to tell process "Application Name"
click button 8 of splitter group 1 of front window
end tell
else if windowIsLocated is "Window Attached To Right" then
tell application "Application Name"
activate
repeat until frontmost
delay 0.1
end repeat
end tell
tell application "System Events" to tell process "Application Name"
click button 9 of splitter group 1 of front window
end tell
else if windowIsLocated is "Window Is Floating" then
tell application "Application Name"
activate
repeat until frontmost
delay 0.1
end repeat
end tell
tell application "System Events" to tell process "Application Name"
click button 8 of front window
end tell
end if
end clickTheButton
Reading all of the comments to your post, I decided to add a .gif which demonstrates using Automator.app's "Watch Me Do" to record me clicking on some buttons in System Preferences, then copying those actions to Script Editor and using elements of that code to identify buttons and windows etc.
Upvotes: 3