reporting the number of items in a pull-down menu?

I have created the following script to allow me to remotely go to a specific chapter when iTunes is playing a chapterized audio file.

So far, the applescript does the following: First it looks to see if there is a pulldown menu named “Chapters” present (because this menu is only present when a chapterized audio file is being played). If it finds a “Chapters” menu then it selects a specified chapter (in this case, chapter 2), and then uses the computers voice to announce the name of the chapter that was just selected. So far, so good.

Now, here’s where my inexperience and lack of basic coding skills presents a problem for me…
The number of chapters varies from audio file to audio file. If my audio file has 15 chapters, yet my script attempts to select chapter 16 I receive an error message. So, how do I get applescript to figure out how many chapters are available before attempting to select one?

I know it’s probably a very basic question but I’m really very new to this. Any help is greatly appreciated. Thanks, -Rob

-- Important, "enable access" must be selected in the Universal Access system preference for this script to work.
activate application "iTunes"
tell application "System Events"
	tell process "iTunes"
		get name of menu bar item 11 of menu bar 1
		if name of menu bar item 11 of menu bar 1 is "Chapters" then
			click menu item 2 of menu 1 of menu bar item 11 of menu bar 1
			set volume 3
			say "chapter 2" using "Vicki"
			set volume 6
			tell application "iTunes"
				play
			end tell
		else
			set volume 3
			say "file has no chapters" using "Vicki"
			set volume 6
		end if
	end tell
end tell

The simplest way to deal with this is with a try block, so you don’t have to know.

set myList to {1, 2, 3, 4}
set k to 5
try
	set thisVal to item k of myList
on error
	display dialog "Oops, no item " & k & " in the list"
end try

Count the menu items that you’re try to click.

set originalVolume to output volume of (get volume settings)
set volume output volume 43

activate application "iTunes"

tell application "System Events"
	tell process "iTunes"
		try
			tell menu 1 of menu bar item "Chapters" of menu bar 1
				set numItems to count menu items
				click menu item 2 of menu 1 of menu bar item "Chapters" of menu bar 1
			end tell
			
			say "Chapter 2" using "Vicki"
			tell application "iTunes" to play
		on error
			say "File has no chapters" using "Vicki"
		end try
	end tell
end tell

set volume output volume originalVolume

FYI, menu bar item 11 was not the index for my machine, so I referred to it by name instead (if it doesn’t exist, an error is raised).

Also, note that the direct parameter for set volume has been deprecated since Mac OS X v10.3.5 (AppleScript Release Notes).

Edit: On second thought:

set originalVolume to output volume of (get volume settings)
set volume output volume 43

activate application "iTunes"

tell application "System Events"
	tell process "iTunes"
		if not (exists menu bar item "Chapters" of menu bar 1) then
			say "File has no chapters" using "Vicki"
		else
			try
				click menu item 2 of menu 1 of menu bar item "Chapters" of menu bar 1
				
				say "Chapter 2" using "Vicki"
				tell application "iTunes" to play
			on error
				-- whatever
			end try
		end if
	end tell
end tell

set volume output volume originalVolume

Hi Bruce!
I tried the last script you posted but it doesn’t seem to work with my setup… I mean, it brings iTunes to the front but it doesn’t select a chapter and it doesn’t play and it doesn’t make any vocal announcements. Did it work on your system? Does my event log look ok? Sorry I don’t know any more about this than I do. I definitely appreciate your time.

Here is my event log…

tell current application
get volume settings
{output volume:86, input volume:0, alert volume:100, output muted:false}
set volume output volume 43
end tell
tell application “iTunes”
activate
end tell
tell application “System Events”
exists menu bar item “Chapters” of menu bar 1 of process “iTunes”
true
click menu item 2 of menu 1 of menu bar item “Chapters” of menu bar 1 of menu 1 of menu bar item “Chapters” of menu bar 1 of process “iTunes”
end tell
tell current application
set volume output volume 86
end tell

Hi,

the reference to the menu item isn’t correct and throws an error


set originalVolume to output volume of (get volume settings)
set volume output volume 43

activate application "iTunes"

tell application "System Events"
	tell process "iTunes"
		if not (exists menu bar item "Chapters" of menu bar 1) then
			say "File has no chapters" using "Vicki"
		else
			try
				click menu item 2 of menu 1 of menu bar item "Chapters" of menu bar 1
				say "Chapter 2" using "Vicki"
				tell application "iTunes" to play
			on error
				-- whatever
			end try
		end if
	end tell
end tell

set volume output volume originalVolume

Er, what Stefan said. (I fixed my earlier post.)

It works like a CHAMP! Thank you both soooo much!!! -Rob

Hey, I’m sorry but I can’t help but ask this question…
How could the above script be motified into two seperate scripts where one scripts doest this…
Advances to the next chapter and reports via voice the actual name of the chapter (i.e. “Chapter 2 (29:10)”)

and the other script …

goes back to the previous chapter and reports the name of that chapter?

I’m not sure how you would check the current chapter…