Wednesday, August 27, 2014

#1 2012-04-27 02:58:44 pm

espri
Member
Registered: 2009-07-18
Posts: 12

'Invalid index' errors

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

Applescript:


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):

Can’t get «class mbar» 1 of «class prcs» "iTunes" of application "System Events". Invalid index.

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)


Filed under: iTunes, invalid index

Offline

 

#2 2012-04-27 03:13:48 pm

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 10488
Website

Re: 'Invalid index' errors

Hi,

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

Applescript:


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


regards

Stefan

Offline

 

#3 2012-04-28 05:41:08 am

espri
Member
Registered: 2009-07-18
Posts: 12

Re: 'Invalid index' errors

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.

Offline

 

#4 2012-04-28 11:16:06 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 1432

Re: 'Invalid index' errors

(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

Offline

 

#5 2012-04-28 11:22:44 am

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 10488
Website

Re: 'Invalid index' errors

Yvan Koenig wrote:

(3) I don't see a Scripts menu dedicated to iTunes.

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

Last edited by StefanK (2012-04-28 11:23:28 am)


regards

Stefan

Offline

 

#6 2012-04-28 02:54:04 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 1432

Re: 'Invalid index' errors

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

Last edited by Yvan Koenig (2012-04-28 03:01:31 pm)

Offline

 

#7 2012-04-28 05:58:00 pm

regulus6633
Member
From: Taulov, Denmark
Registered: 2006-11-01
Posts: 1695
Website

Re: 'Invalid index' errors

espri wrote:

it works fine when run from Script Debugger but displays the error message 'Invalid index' when run from the itunes scripts menu.

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...

Applescript:

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

with...

Applescript:

keystroke "j" using command down

Offline

 

#8 2012-04-29 03:04:38 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 1432

Re: 'Invalid index' errors

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.

Applescript:


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

Last edited by Yvan Koenig (2012-04-29 03:20:58 am)

Offline

 

#9 2012-04-29 04:06:21 am

Nigel Garvey
Moderator
From: Warwickshire, England
Registered: 2002-11-19
Posts: 3505

Re: 'Invalid index' errors

Applescript:

-- 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.


NG

Offline

 

#10 2012-04-29 04:19:54 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 1432

Re: 'Invalid index' errors

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

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

Offline

 

#11 2012-04-29 05:22:58 am

Nigel Garvey
Moderator
From: Warwickshire, England
Registered: 2002-11-19
Posts: 3505

Re: 'Invalid index' errors

Yvan Koenig wrote:

What if the script is saved as an application ?

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).


NG

Offline

 

#12 2012-04-30 03:54:53 pm

espri
Member
Registered: 2009-07-18
Posts: 12

Re: 'Invalid index' errors

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.

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.047 seconds, 10 queries executed ]

RSS (new topics) RSS (active topics)