I am trying to use AppleScript to navigate iTunes. This is working thus far, but I am having trouble clicking the "See All" link/text (image below).
You may note that there are two "See All" links, one for iPhone and one for iPad. I am trying to click the one for iPhone.
Any guidance would be greatly appreciated!
run this code in Script Editor:
tell application "System Events" to tell application process "iTunes"
get static texts of UI elements of UI elements of UI element 1 of scroll area 1 of splitter group 1 of window 1 whose name is "See All"
end tell
This should get you all the static texts named "See All". Use trial and error till you get the right one.
The trick I used was to use "static texts of UI elements of UI elements" etc, until I found the static text I was looking for. You should be able to do this with anything. Looking for buttons? Replace static texts with buttons, etc.
Related
I have a simple script that automates the command "Export Library" in Music. It does the following:
tell application "System Events"
tell process "Music"
set libmenu to menu item "Library" of menu "File" of menu bar 1
click menu item "Export Library…" of menu of libmenu
tell window 1
click button "Save"
tell sheet 1 to click button "Replace"
end tell
end tell
end tell
(So basically it opens the menu and clicks the obvious buttons, saving me a few clicks). However, after upgrading to Ventura (macOS 13.0), this stopped working. The command click button "Save" is failing with:
Can’t get button \"Save\" of window 1 of process \"Music\"."
I tried to say click button 1 or click button 2 instead, but that doesn't work. I then said name of every button and it printed
{missing value, missing value, missing value}
I couldn't find a good manual for AppleScript, but it does look like something changed in Ventura. Any hints will be appreciated!
Thanks,
OK, I replaced click button "Save" with:
repeat with b in (get buttons)
if name of b is "Save" then
click b
end if
end repeat
Interestingly, I can still click the "Replace" button directly without resorting to a loop.
Thanks!
OK! I found a hint for the answer here: Just add a short delay before trying to click on "Save"!
delay 0.5
click button "Save"
Without the delay, the the dialog doesn't seem to be ready and indeed if you do get buttons you will only see the 3 from the top left corner (close, minimize, zoom). With the delay, get buttons returns 6 buttons, including the "Save".
I've recently found Applescript after desperate hours trying to figure out a way to do the trick since I use Preview A LOT. The button to open highlighting colors doesn't have any menu item or keyboard shortcut (only for general highlight that is yellow, my plan was to create shortcuts for the rest of the colors available), it's only located in the toolbar. The code I've encountered for similar problems didn't work (probably due to my serious lack of knowledge in the area...), so if anyone could come up with something I would really appreciate it!
First I'd like to make note that Preview does not contain an AppleScript dictionary file, e.g. Preview.sdef, and as such, sans a minimal set of default commands, is not easily scriptable beyond the default commands and requires UI Scripting with System Events to achieve the goal.
Note that UI Scripting can be kludgy and has its own issues and requires granting privileges in System Preferences > Security & Privacy > Privacy > Accessibility, which one should be prompted for if necessary, however I present two options for your consideration.
Option 1
This first option simply triggers the Highlight menu where you can then press the key of the first character of a color, after which you press the enter to set the chosen color. For the color Purple, since there is also Pink, pressing P will of course highlight Pink, so pressing Pu will select Purple. You can also use the Up/Down arrow keys to navigate the menu.
This would allow you to actuate the menu with one assigned keyboard shortcut and not have to explicitly assign a keyboard shortcut for each individual color.
Example AppleScript code:
tell application "Preview" to activate
delay 0.1
tell application "System Events"
tell group 2 of ¬
toolbar 1 of ¬
window 1 of ¬
application process "Preview"
try
click menu button 1 of group 1
on error
click menu button 1 of radio group 1
end try
end tell
end tell
Note: In the example AppleScript code shown above make note that there is an try statement with error handling to click the target menu. This is because the hierarchical UI element structure changes based on whether or not highlighting has already been used in the current document. This also allows for the script to silently fail if the keyboard shortcut to trigger it is pressed on a document that highlighting cannot be preformed on.
Option 2
This next option presents a list to choose from and then sets the highlight color based on the selection.
Example AppleScript code:
set colorList to ¬
{"Yellow", "Green", "Blue", "Pink", "Purple"}
set chosenHighlightColor to ¬
(choose from list colorList ¬
with title "Select Highlight Color") ¬
as string
if chosenHighlightColor is "false" then return
tell application "Preview" to activate
tell application "System Events"
tell group 2 of ¬
toolbar 1 of ¬
window 1 of ¬
application process "Preview"
try
click menu button 1 of group 1
on error
click menu button 1 of radio group 1
end try
delay 0.05
try
click menu item chosenHighlightColor of menu 1
end try
end tell
end tell
Note: In the example AppleScript code shown above make note that there is an try statement with error handling to click the target menu. This is because the hierarchical UI element structure changes based on whether or not highlighting has already been used in the current document. This also allows for the script to silently fail if the keyboard shortcut to trigger it is pressed on a document that highlighting cannot be preformed on.
Automator Service/Quick Action and Keyboard Shortcut
To use either of these examples as a keyboard shortcut, create an Automator Service/Quick Action with a Run Shell Script action replacing the default code with the example AppleScript code.
Then in System Preferences > Keyboard > Shortcuts > Services assign it a keyboard shortcut. I used ⇧⌘H as it was not already assigned to anything in Preview.
Notes:
This was tested and worked for me as is under macOS Catalina and macOS Big Sur with Language & Region setting in System Preferences set to English US.
Does not work if the document is in Full Screen view.
Select Highlight Color
Automator Service/Quick Action
Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.
The following script will open a track in iTunes
use application "iTunes"
property trackURL : "itmss://itunes.apple.com/us/album/brahms-violin-concerto-in-d-major-op-77-iii-allegro/145533236?i=145533044&uo=4"
open location trackURL
Now, asking "iTunes" to play it does not work because the track is highlighted but not properly selected, i.e., it requires a manual mouse click to select it and play it.
How can I select the highlighted track? Or how could I ask "iTunes" to play the song?! Alternatively, is there a way to add a music to my library from an URL directly?
Disclaimer: I don't have the Apple Music subscription, so the UI on my end may not be exactly the same as yours. However, if I click the "Play" button, I get the little advertisement asking me to sign up for the service, which I assume would just play the music if you had the service. So, these are the steps I've been able to follow to get that box to pop up:
The first, and most convenient from AppleScript, thing to try is just to hit the space bar to start the music playing. This actually works great if I've selected the item manually by clicking on it. However, after open location, it doesn't work, and this appears to be because even though the row is highlighted in the viewer, the actual keyboard focus seems to be on the page itself (the iTunes Store and Apple Music appear to have their entire UI presented as web pages rendered by WebKit). You can verify this by tapping the up and down arrow keys on the keyboard; the page scrolls up and down instead of you switching to adjacent tracks.
My opinion is that this is actually a bug in iTunes; I'd consider the true solution to the problem to be to report this to Apple via the bug reporter. Using open location really should set the keyboard focus to the track you navigated to.
With that said, we can work around it in the short term by simulating a click on the "Play" button. Note that you'll probably need to add your app in System Preferences > Security and Privacy > Accessibility. Note also that this is incredibly fragile, and if Apple ever changes anything in the layout of the web pages they're serving, this whole thing will break. Finally, please note that this code is extremely ugly; the whole thing gives me hives just by looking at it, but it's the only thing I was able to get to work. Side effects of reading this code may include nausea, headaches, and suicidal thoughts. Do not read this code immediately after eating. Consult your doctor before reading this code if you have a history of depression or obsessive-compulsive disorder.
property trackURL : "itmss://itunes.apple.com/us/album/brahms-violin-concerto-in-d-major-op-77-iii-allegro/145533236?i=145533044&uo=4"
property trackTitle : "III. Allegro giocoso, ma non troppo vivace"
tell application "iTunes"
activate
open location trackURL
delay 1 -- give the page a second to load
end tell
tell application "System Events"
tell process "iTunes"
set theRows to the rows of table 1 of UI element 1 of scroll area 1 of group 1 of group 1 of front window
-- "repeat with eachRow in theRows" isn't working. I don't know why. Freaking AppleScript
repeat with i from 1 to the number of theRows
set eachRow to item i of theRows
if exists group 2 of UI element 2 of eachRow then
if value of static text 1 of group 1 of group 2 of UI element 2 of eachRow is trackTitle then
tell group 1 of UI element 2 of eachRow to click
end if
end if
end repeat
end tell
end tell
If Apple ever fixes the bug, of course, we should be able to just:
tell application "iTunes"
activate
open location trackURL
delay 1 -- give the page a second to load
end tell
tell application "System Events" to keystroke space
I'm trying to select OK with the popup for scaling the screen with Apple script.
Does anyone know what I can add to this script to allow me to click OK?
Apple Script:
tell application "System Preferences"
reveal anchor "displaysDisplayTab" of pane "com.apple.preference.displays"
end tell
tell application "System Events" to tell process "System Preferences" to tell window "Built-in Retina Display"
click radio button "Scaled" of radio group 1 of tab group 1
click radio button 1 of radio group 1 of group 1 of tab group 1
(* Trying to click the ok button here *)
delay 1
click ((pop up buttons of sheet 1 of window 1) whose description is "OK")
end tell
quit application "System Preferences"
Or also rather does anyone know a trusted application that can graphically print out the labels of the UI when I visit them on my screen so I will know what to use when I say: "tell UIName"?
Thanks.
Solution 1: Press Return
Since you're using System Events anyway, and the button in question is selected by default (indicated by being highlighted in blue), just make sure that the preference pane is in focus and use System Events to hit the return key:
tell application "System Events" to keystroke return
It's quick, simple, and saves the hassle of identifying UI elements within the hierarchy. The drawback is that the preference pane has tone in focus, and if it loses focus before it receives the keystroke, the rest of the script will fail.
Solution 2: The One You Asked For
Regarding identification of UI elements within the GUI objects hierarchy, you got the class of the button in question wrong as well as its description.
The "OK" button is referenced like so:
tell application "System Events" to tell process "System Preferences" ¬
to get button "OK" of sheet 1 of window "Built-in Retina Display"
(window 1 is also fine). You can use the whose filter to target it instead, which would be done like this (via System Events and the System Preferences process):
get buttons of sheet 1 of window 1 whose name is "OK"
but all this does is ask AppleScript to search for a button whose name we know, then annoyingly give the result back to us as a list (the list structure can be flattened by requesting the first button of sheet 1...).
However, we do know its name and that there's only one of it, so we can reference it by name directly.
As a side-bar, if you quickly run this command:
get the properties of button "OK" of sheet 1 of window "Built-in Retina Display"
you'll be able to see that its description is merely "button", which isn't what you had hoped. Now run this command:
get the actions of button "OK" of sheet 1 of window "Built-in Retina Display"
which reveals it has one action available, AXPress (this is equivalent to a mouse click).
Therefore, finally, the way to click the button in a more satisfactory way than hitting the return key looks like this:
tell application "System Events" to tell process "System Preferences" ¬
to tell button "OK" of sheet 1 of window "Built-in Retina Display" ¬
to perform action "AXPress"
For exploring the GUI objects on the screen, I occasionally use the Accessibility Inspector that comes with Apple's XCode. It's vaguely useful, although inconveniently annoying with some of the discrepancies between the names by which it references objects and the names by which AppleScript does (they're also really subtle discrepancies, but enough to stop your script from working and send you into a frenzy for weeks).
So, actually, I just explore it programmatically myself in Script Editor.
What I did to solve your issue was to bring up the pane and the dialog box in question, then systematically tell the System Preferences process to get UI elements; then get buttons of UI elements; then get buttons of UI elements of UI elements, and stop when I saw a pair of buttons returned called "OK" and "Cancel". It can be pain-staking, but it'll get you the correct reference.
There are other ways too, but I would be moving beyond the scope of this question.
I want to write a script which saves the images in few formats. the thing is the formats are shown based on some condition. I means some times there will be 5 formats and sometimes 8. I want to completely automate the working of these saving things. So i decided to write an applescript. Got UI browser and using that I can access each and every pop-up menus. I'm using loops to perform save operation. The thing is I am not getting where to end. So i came up with a thought that if i can get the number of items in the pop-up menu then it will be easy for me to carry out the task.
Can anyone help me out?
Well this is possible but you can't count the menu items directly. Communication is at the GUI-end and not directly to the application which means that the menu needs to appear before you can count it.
tell application "System Events"
tell process "Your application"
--we need to menu to appear first
click pop up button 1 of window 1
--now the menu appeared we can count the items in it
count menu items of menu 1 of pop up button 1 of window 1
--now hide the menu again by pressing escape
key code 53
end tell
end tell
Well counting is one way to check the menu but the other way is get all the values in it and then click the right menu item by it's name. This is, maybe not in your case, in general the best solution.
set menuItemToSelect to "Title of menu item I prefer to check"
tell application "System Events"
tell process "Your Application"
tell pop up button 1 of window 1
--Only continue if the menu item isn't already selected
if value of it is not equal to menuItemToSelect then
--we need to menu to appear first
click it
--now the menu appeared we can get the items
set menuItemTitles to name of menu items of menu 1
--check if the menu item exists
if menuItemToSelect is in menuItemTitles then
--menu item exists; click on it
click menu item menuItemToSelect of menu 1
else
--the menu item to select doesn't exist; hide the menu
key code 53
end if
end if
end tell
end tell
end tell