Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

bollweevil

macrumors 6502
Original poster
Feb 1, 2008
410
1
Hello All,

I am trying to use some AppleScripts to do stuff in iTunes, but I am running into many problems. A lot of AppleScripts work so long as all the MP3 files are not locked, but if they are locked then the AppleScript throws an error. This is particularly frustrating because the AppleScripts are simply automating things that iTunes can do and does do to all MP3s regardless of whether they are locked or not.

Example code: (from http://dougscripts.com/)

Code:
tell application "iTunes"
	if player state is not stopped then
		try
			tell current track
				set played count to (get played count) + 1
				set played date to (get current date)
			end tell
		end try
		next track
	end if
end tell

When this script is invoked, it should skip to the next track and increment the play count of the song that it just skipped away from. This works if the current song is unlocked. This DOES NOT work if the current song is locked. It does skip to the next track, but it fails to change the play count. Now, if you actually let the song finish playing in iTunes, then iTunes has no problem incrementing the play count.

Why can iTunes do this "naturally", but the AppleScript cannot?

Another AppleScript reports "file permission error" (just like that, not capitalized or anything) when I try to set the play count to an arbitrary number. Again, this seems strange because iTunes itself can set the play count to zero without complaining.

A related question might be, where does iTunes store all this metadata? Does it like to store it on the files themselves, but it forced to store it in the ITDB file if the files are locked, but AppleScript doesn't know how to handle this contingency? That seems like a likely explanation to me. How can we make the AppleScripts comfortable with this, just like iTunes is by default?

Thanks.
 
By incrementing the play count, you are WRITING data, so if the file is locked by iTunes as it is playing it, then this is normal behavior.
 
By incrementing the play count, you are WRITING data, so if the file is locked by iTunes as it is playing it, then this is normal behavior.

You misunderstand, iTunes did not lock the file, I locked it. I actually went to Get Info and set the "Locked" flag on the file, also known as the uchg (for "unchangeable") flag.

This AppleScript does work if the file is not locked.
 
As far as I know play count isn't part of the MP3 file, which is why iTunes can increment it. I'd guess AppleScript is checks for the track's write-ablilty when it does certain operations (even if they can succeed on locked files) and gives an error. A workaround in your particular example would be to set the player's position to right at the very end of the track, which would then update the play count, last played and skip to the next track:

Code:
tell application "iTunes"
	if player state is not stopped then
		try
			tell current track
				set trackLength to (get duration)
			end tell
			
			set player position to trackLength
		end try
	end if
end tell
 
Thanks, that workaround works in this case. However, I also really desperately want to be able to set the play count to any arbitrary number. Any ideas how I could do that?
 
Question 1, is why did you lock the track in the first place?

Other than that, could you tell the Finder to unlock the file, tell iTunes to set the play count, then tell the Finder to relock the file?

mt
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.