'Invalid index' errors

I am using this script to set values in the iTunes View Options… menu:


tell application "iTunes"
	activate
end tell


tell application "System Events"
	tell process "iTunes"
		click menu item "View Options." of menu 1 of menu bar item "View" of menu bar 1 -- bring up the View Options Dialog
		delay 0.5 -- give the window a little time to open -- This is sometimes necessary.
	end tell
	
	set targetcheckbox to "Genre" -- Pick a checkbox to work with.	
	set initval to value of checkbox targetcheckbox of window "View Options" of application process "iTunes" -- get the value of a checkbox
	if (initval = 1) then
		click checkbox targetcheckbox of window "View Options" of application process "iTunes" -- reset 'Genre'
	end if
	
	
	click button "OK" of window "View Options" of application process "iTunes" of application "System Events" -- close the View Options Dialog
end tell

return

This runs fine when called from Script Debugger or the Applescript Editor. However, if I copy the script to the library and then try to run it from the iTunes scripts menu it brings the error (after, I believe, briefly opening the View options… window):

I have seen in this forum that others have had similar problems. Indeed there are pages and pages of reports of the trouble from as far back as 2005! However, I haven’t seen any explanation of what I have to do to get rid of the problem. Can anyone help? Please!

Model: Intel iMac, OS X 10.6.4
AppleScript: 2.1.2
Browser: Firefox 11.0
Operating System: Mac OS X (10.6)

Hi,

try this, it uses the pick command and a controlled delay


set targetcheckbox to "Genre" -- Pick a checkbox to work with.	

activate application "iTunes"
tell application "System Events"
	tell process "iTunes"
		pick menu item "View Options." of menu 1 of menu bar item "View" of menu bar 1 -- bring up the View Options Dialog
		repeat until exists window "View Options"
			delay 0.2
		end repeat
		tell window "View Options"
			set initval to value of checkbox targetcheckbox
			if (initval = 1) then
				click checkbox targetcheckbox
			end if
			click button "OK"
		end tell
	end tell
end tell

Thanks, that is interesting. I didn’t know the ‘pick’ command - is it described somewhere?

However, on my system your script has exactly the same result as my original one - it works fine when run from Script Debugger but displays the error message ‘Invalid index’ when run from the itunes scripts menu.

(1) View Options. is enabled only if you clicked one of these buttons :
LIBRARY > Music
LIBRARY > TV Shows
LIBRARY > Apps

(2) what need to trigger a menu item which is localization dependent when there is a predefined shortcut valid worldwide : cmd + J

(3) I don’t see a Scripts menu dedicated to iTunes.
I just see the System one in which we may have menu items dedicated to iTunes.

Yvan KOENIG (VALLAURIS, France) samedi 28 avril 2012 18:14:44

Salu Yvan,

the iTunes script menu is enabled, if there is a folder ~/Library/iTunes/Scripts and at least one script file in it.
Look also here

Merci Stephan

This day is a good one, I learnt something.

In fact, I don’t use iTunes too much (no iPod, no iPhone and no iPad). I just opened it for see and, as I saw no Script menu, I didn’t search more.
This time, I searched for the word « script » in iTunes Help and got no result.
So, I will bookmark the passed link :wink:

Yvan KOENIG (VALLAURIS, France) samedi 28 avril 2012 21:53:57

I don’t know why this happens (I didn’t check that it does error) but rather than fight it you could just use the keyboard shortcut command-j. So replace…

pick menu item "View Options." of menu 1 of menu bar item "View" of menu bar 1

with…

keystroke "j" using command down

Hello

If I understand well, the script issue an error when the View Options. menu item is disabled.
To get rid of that the script must enable it.
This piece of code select LIBRARY > Music to enable the View Options. item.


activate application "iTunes"
tell application "System Events" to tell application process "iTunes"
	tell window 1 to tell 2nd scroll area to tell outline 1 to tell row 2
		set {value of attribute "AXSelected", value of attribute "AXDisclosing"} to {true, true}
	end tell
end tell

Yvan KOENIG (VALLAURIS, France) dimanche 29 avril 2012 10:04:28

-- Run this from iTunes's own script menu.

tell application "System Events"
	tell application process "iTunes"
		set frontmost to true
		set c to (count menu bars)
	end tell
end tell

display dialog c --> 0

I get the same result running a similar script from PowerMail’s script menu. GUI scripting the app running the script is apparently problematic.

What if the script is saved as an application ?
Here it works.

Yvan KOENIG (VALLAURIS, France) dimanche 29 avril 2012 11:19:49

Yes. That seems to do the trick for iTunes (but not for PowerMail, whose menu shows the application package as a directory structure and only offers the compiled script at the heart of it).

Many thanks to all who have provided such useful help here. The final tip of saving the script as an application solves the problem when calling the script from within iTunes.

I have saved a current version of my script, which runs when called from within iTunes as well as from Script Debugger, etc., to complete the original thread where this script was discussed last August, at http://macscripter.net/viewtopic.php?pid=150721#p150721.

Yvan, iTunes can be great even without any extra devices. I use it mainly just to organise my music library.