tell application "iTunes" to activate
tell application "System Events"
tell process "iTunes"
tell pop up button 1 of window "iTunes"
click
delay 0.5
set listofItems to get name of every menu item
tell menu 1
click menu item "TV Shows"
end tell
--keystroke "t"
--delay 0.5
--keystroke return
end tell
end tell
end tell
The pop up button is correctly clicked and the list of choices pops up, but trying to click the relevant menu item throws this error:
listofitems returns 0 items.
What is going on here? How do I click/select the required item?
BTW, the grayed out code does not work either. It selects the right sub item in the pop up menu, but for some inexplicable reason keystroke return does not do anything!
tell application "iTunes" to activate
tell application "System Events"
tell application process "iTunes"
tell window 1
click (first button whose name is not missing value and name is not "iTunes Store")
-- The click produces a new window 1 whose scroll area 1 contains an outline 1. Obviously the 'window 1' reference now refers to this new window.
tell outline 1 of scroll area 1
repeat until it exists
delay 0.2
end repeat
click UI element 1 of (first row whose value of static text 1 of UI element 1 contains "TV")
end tell
end tell
end tell
end tell
“TV shows” is “TV Programmes” on my machine, probably an adaptation to my en_GB language preference.
My best guess is that you’re using a different OS version from mine and the GUI set-up’s different for the two systems.
On my Snow Leopard system, what looks like a pop-up button is in fact just a ‘button’ to System Events. Clicking it produces not a ‘menu’ but a menu-like sheet which is classed as a ‘window’. This new window contains only a ‘scroll area’, which contains an ‘outline’, which contains five ‘rows’. The first row’s a header and the other four are what would be menu items if the window was a ‘menu’. However, it’s appears to be necessary to click the first (and only) ‘UI element’ of the desired row, not the row itself.
I suppose that even if the button’s is a ‘pop up button’ on your system, the ‘menu’ could still be another ‘window’ as on mine.
tell application "System Events"
tell application process "iTunes"
tell window 1
click pop up button 1 --(first button whose name is not missing value and name is not "iTunes Store")
-- The click produces a new window 1 whose scroll area 1 contains an outline 1. Obviously the 'window 1' reference now refers to this new window.
tell UI element 1
tell scroll area 1
tell outline 1
--repeat until it exists
delay 0.5
--end repeat
click UI element 1 of (first row whose value of static text 1 of UI element 1 contains "TV")
end tell
end tell
end tell
end tell
end tell
end tell
Aha! The GUI Scripting approach also depends on whether or not the “sidebar” (Option-Command-s) is showing. Yvan’s script works if it is. My GUI script works (or not) if it isn’t.
But the direct (iTunes dictionary) alternatives a couple of posts up are preferable either way.
But I have one further question. I have a HomeShare iTunes library which appears in the drop down list. But the direct route above does not work to reveal the home share library.
I suspect this is because home share is not a playlist.
What would be the correct terminology to reveal the homeshare library?
I have tried to reveal “user playlist” and “library playlist” but these have not worked…
I had cobbled together the following script, built around your script, which works:
tell application "iTunes" to activate
tell application "System Events"
tell application process "iTunes"
try
click menu item "Show Sidebar" of menu 1 of menu bar item "View" of menu bar 1
delay 1
end try
tell window 1
try
click (first button whose name is not missing value and name is not "iTunes Store")
end try
end tell # window 1
# Now, speak to the new window 1
tell window 1 to tell outline 1 of scroll area 1 of splitter group 1
repeat until it exists
delay 0.2
end repeat
repeat with n from 1 to 10
try
(first row whose value of (static text 1) contains "Server")
set value of attribute "AXSelected" of result to true
delay 1
exit repeat
end try
delay 1
end repeat
end tell # window 1
try
click menu item "Hide Sidebar" of menu 1 of menu bar item "View" of menu bar 1
end try
--delay 10
end tell # process
end tell # System Events
I have it autorun, using DSW, when iTunes launches and all seems to work well, irrespective of how iTunes is set up.
Of course, if there is an iTunes dictionary solution, that would be much better. But in the meantime, we have a solution.
I recommend to control showing and hiding the sidebar rather than using try blocks
activate application "iTunes"
tell application "System Events"
tell application process "iTunes"
set sidebarIsVisible to exists splitter group 1 of window 1
if not sidebarIsVisible then
keystroke "s" using {command down, option down} -- show sidebar
repeat until exists splitter group 1 of window 1
delay 0.2
end repeat
end if
-- .
if not sidebarIsVisible then
keystroke "s" using {command down, option down} -- hide sidebar
end if
end tell # process
end tell # System Events