Changing 'View->View Options...' in iTunes

I want to automate changing the columns shown in iTunes, i.e. what you would do with the ‘View->View Options…’ command by hand. I thought at first that I would write an Applescript but I can’t find the necessary data in the iTunes dictionary.

So now I rather suspect that I maybe need to use GUI scripting (which I’ve never done before). Can anyone comment? Any tips would be appreciated (also as to how you can change the size of columns once you have the ones you want). Are there any sample scripts around that do this sort of thing?

Thanks!

Model: iMac
Browser: Firefox 6.0
Operating System: Mac OS X (10.6)

This bit of script should get you started:

set targetcheckbox to "Beats Per Minute" -- Pick a checkbox to work with.

tell application "iTunes"
	activate
end tell

tell application "System Events"
	click menu item "View Options." of menu "View" of menu bar item "View" of menu bar 1 of application process "iTunes" -- bring up the View Options Dialog
	delay 0.5 -- give the window a little time to open -- This is sometimes necessary.
	
	set initval to value of checkbox targetcheckbox of window "View Options" of application process "iTunes" -- get the value of a checkbox
	
	click checkbox targetcheckbox of window "View Options" of application process "iTunes" -- click that checkox
	
	set finval to value of checkbox targetcheckbox of window "View Options" of application process "iTunes" -- and see how the value has changed
	
	--click button "OK" of window "View Options" of application process "iTunes" of application "System Events" close the View Options Dialog
end tell

activate -- Now display the result of our clicking
display dialog "Value before click: " & (initval as string) & "\nValue After click: " & finval as string with title targetcheckbox & ": Before and after clicking"
return

That was very useful - many thanks!

For information, here’s what I ended up with, disabling ‘Genre’ and enabling ‘Last Played’:

tell application "iTunes"
	activate
end tell

tell application "System Events"
	click menu item "View Options." of menu "View" of menu bar item "View" of menu bar 1 of application process "iTunes" -- bring up the View Options Dialog
	delay 0.5 -- give the window a little time to open -- This is sometimes necessary.
	
	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
	
	set targetcheckbox to "Last Played" -- 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 = 0) then
		click checkbox targetcheckbox of window "View Options" of application process "iTunes" -- set 'Last Played'
	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

Now I have to build that in a loop to go through my whole library. I also want to change the ordering of the iTunes columns and, perhaps, modify their width. Any ideas there?

Thanks again!

Here is a further development which also re-orders the columns displayed by iTunes. This script only changes the sequence of the last few columns (assuming that the columns on the left of the window are already in the desired sequence). To do that, I found that I had to call the ‘View Options…’ command twice, so that the columns are first removed and then are added on the right as wished.

tell application "iTunes"
	activate
end tell

tell application "System Events"
	click menu item "View Options." of menu "View" of menu bar item "View" of menu bar 1 of application process "iTunes" -- bring up the View Options Dialog
	delay 0.5 -- give the window a little time to open -- This is sometimes necessary.
	set resetlist to {"Genre", "Plays", "Last Played", "Date Added"}
	repeat with targetcheckbox in resetlist
		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"   -- ensure specified column is reset
		end if
	end repeat
	click button "OK" of window "View Options" of application process "iTunes" of application "System Events" -- close the View Options Dialog

	click menu item "View Options." of menu "View" of menu bar item "View" of menu bar 1 of application process "iTunes" -- bring up the View Options Dialog
	delay 0.5 -- give the window a little time to open -- This is sometimes necessary.
	set setlist to {"Composer", "Plays", "Last Played", "Date Added"}
	repeat with targetcheckbox in setlist
		set initval to value of checkbox targetcheckbox of window "View Options" of application process "iTunes" -- get the value of a checkbox
		if (initval = 0) then
			click checkbox targetcheckbox of window "View Options" of application process "iTunes"   -- ensure specified column is set
		end if
	end repeat
	
	click button "OK" of window "View Options" of application process "iTunes" of application "System Events" -- close the View Options Dialog
end tell

return

That seems to work quite well but there are still some problems I would like to solve. When I do something similar by hand, I do the ordering with drag and drop and I also change the width of the columns, usually with ‘ctrl-click’ and then ‘Auto Size All Columns’. Would there be any way of automating that?

When I came back to this script, with much help from others on the forum, I managed to implement a version which runs both from inside Script Debugger or Applescript Editor (which the previous versions also did) and also, after it has been saved to /Library/iTunes/Scripts as an application, when called directly from iTunes Scripts menu (where earlier versions brought an error). Here is this current version…


tell application "iTunes"
	activate
end tell

tell application "System Events"
	keystroke "j" using {command down} -- bring up the View Options Dialog
	delay 0.2 -- give the window a little time to open -- This is sometimes necessary.
	set resetlist to {"Genre", "Plays", "Last Played", "Date Added"}
	
	repeat with targetcheckbox in resetlist
		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" -- ensure specified column is reset
		end if
	end repeat
	click button "OK" of window "View Options" of application process "iTunes" of application "System Events"
	
	keystroke "j" using {command down} -- bring up the View Options Dialog again
	delay 0.2 -- give the window a little time to open -- This is sometimes necessary.
	set setlist to {"Composer", "Plays", "Last Played", "Date Added"}
	repeat with targetcheckbox in setlist
		set initval to value of checkbox targetcheckbox of window "View Options" of application process "iTunes" -- get the value of a checkbox
		if (initval = 0) then
			click checkbox targetcheckbox of window "View Options" of application process "iTunes" -- ensure specified column is set
		end if
	end repeat
	
	click button "OK" of window "View Options" of application process "iTunes" of application "System Events" -- close the View Options Dialog
end tell