Noobie needs help with script!

Hi. I’m brand new to AppleScript and have FINALLY been able to write some simple, unsophisticated scripts that actually work. I’m sure they are very inefficient and poorly done, but they’re a start.

I need help with a simple iTunes script, which I will include here. I want to be able to play samples (or entire songs) in my iTunes Library by Artist Name, Rating, and then give the user a choice whether to hear a shot sample or the entire song.

I can get the 10 second sample to play, but not the entire song. The sample has a “delay 10” statement, but the entire song has no delay statement at all. I can’t figure out what is wrong.

Can anybody help me mend my ways? Also, I’d love to know a more efficient way to get user input on a single Display Dialog, instead of doing it 3 times. Any help appreciated. BTW, I’m on 10.6.8. Script below.

Thanks, Lou Dina

(*
This script plays songs in iTunes, based on user input. It asks the user for the name of the artist they wish to hear and the minimum rating, then cycles through each song. Currently, it is set to play only a short sample instead of the entire song, like when scanning a radio station. This script does not alter the iTunes database, create playlists, etc.

written by Lou Dina - 6/30/2012 (my first AppleScript)
*)

tell application “iTunes”
activate

--get User input on the artist they would like to hear in iTunes
display dialog "Enter the name (or part of the name) of an artist to hear a short sample of each artist track in the music library rated 3-stars or above. Click Cancel to Abort.
		" default answer "" buttons {"Cancel", "Continue"} default button "Continue"
set user_Artist to the text returned of the result

--get User input on the rating for the above list
display dialog "Enter the \"minimum\" rating (1, 2, 3, 4, or 5) of the songs you would like to hear.  Click Cancel to Abort.
	" default answer "" buttons {"Cancel", "Continue"} default button "Continue"
set user_Rating to the text returned of the result

--get User input on whether they want to whole song played, or just a snippet
display dialog "Please choose to hear the Whole Song, a 10 second sample, or click Cancel to abort. " buttons {"Cancel", "Entire Song", "10-Second Sample"} default button "Entire Song"
set song_Length to the button returned of the result

--create song list for the chosen artist by unique track id instead of by name to make sure songs of the same name by other artists are not played
set song_list to id of every track of playlist 1 whose artist contains user_Artist and rating is greater than or equal to (user_Rating * 20)

--cycle through songs and play. Stop when end of list is reached.
repeat with i in song_list
	--	set track_to_play to contents of song_list
	if song_Length is equal to "10-Second Sample" then
		play track id i
		delay 10
	else if song_Length is equal to "Entire Song" then
		play track id i
		--play track id i -- this doesn't work. It quickly lists songs but doesn't play unless I add a "delay". Hmmmmm.
		--delay 10
	end if
	
end repeat




stop

end tell

Model: Mac Pro
AppleScript: 2.3
Browser: Firefox 13.0.1
Operating System: Mac OS X (10.6)

Hi, Lou, welcome.

I’ve never scripted iTunes, but a brief inspection of its dictionary (*) suggests that you should create a (temporary) playlist, and put the search result into that. A playlist can play tracks one after another, but you cannot tell iTunes to play tracks from a list in a script (by the looks of it).
iTunes also has no way (it seems) to play only part of a track. You are more or less on track (pun unintended) for a solution: when you have created a playlist you can use a repeat with “delay 10” and “next track” while playing the playlist.

There’s a great site with iTunes scripts: there’s bound to be a few to show you how to create and use a playlist.

(*) Access a dictionary:
In AppleScript Editor, select Window > Library.
A “palette” window appears, with a list of applications.
iTunes should be there, if not use the + button at the top to add it.
Doubleclick iTunes in the list, and its dictionary window opens.
It lists everything iTunes knows in terms of AppleScript.
It helps to have a bunch of scripts at hand to see all of it in action.

Don’t hesitate to return with more questions.

Hi. The stop is in the wrong position; it ends any entire song almost at the same moment it begins playing.


if song_Length is equal to "10-Second Sample" then
			play track id i
			delay 10
			stop
		else if song_Length is equal to "Entire Song" then
			play track id i with once
		end if

Alastor/Marc…Thanks for your replies.

Alastor:
I did create a temporary playlist in my revised script, and deleted the playlist when the script finished running. I liked that addition, but it alone did not solve the problem.

Marc:
I tried your suggestion and it ran through the list of songs without playing them as it did in my original. I had to do a bit of a “kludge” to get it to play the entire song (see the else if statement in the revised script below). Without the delay statement, I just couldn’t get the songs to play at all, so I had the script determine the duration of each song, set the delay to the duration, and it worked. I have no idea why.

What is strange is that if I create a short, separate script without an if statement, (as show below), it works fine. Inside the IF statement, and this same code does not work.

tell application “iTunes”
make playlist with properties {name:“Temp Playlist”}
duplicate (every track of playlist 1 whose artist contains “beat” and rating is greater than or equal to (4 * 20)) to playlist “Temp Playlist”
reveal playlist “Temp Playlist”
play

end tell

My script has changed a lot, so I will include the entire revised script as it now stands.

Thx, Lou

(*
This script plays songs in iTunes, based on user input. It asks the user for the name of the artist they wish to hear and the minimum rating, and whether they want a short preview or the Whole Song. It then creates a “Temporary Playlist” in iTunes and plays each song. When done, or if the User stops the script from Script Editor, the temporary playlist is deleted from the iTunes Library.

Note: This script does NOT add or remove any songs from the iTunes database.

written by Lou Dina - 6/30/2012 (my first AppleScript)
*)

tell application id “com.apple.iTunes”
activate

--get User input on the artist they would like to hear in iTunes
display dialog "Enter the name (or part of the name) of an artist you wish to hear. Click Cancel to Abort.

		" default answer "" buttons {"Cancel", "Continue"} default button "Continue"
set user_Artist to the text returned of the result

--get User input on the rating for the above list
display dialog "Enter the \"minimum\" rating (0, 1, 2, 3, 4, or 5) of the songs you would like to hear.  Click Cancel to Abort.
	" default answer "" buttons {"Cancel", "Continue"} default button "Continue"
set user_Rating to the text returned of the result

--get User input on whether they want to whole song played, or just a preview
display dialog "Please choose to hear the \"Whole Song\", a \"Preview\", or click \"Cancel\" to abort. " buttons {"Cancel", "Whole Song", "Preview"} default button "Preview"
set song_Length to the button returned of the result

--create song list for the chosen artist by unique track id instead of by name to make sure songs of the same name by other artists are not played
set song_list to id of every track of playlist 1 whose artist contains user_Artist and rating is greater than or equal to (user_Rating * 20)

--create a temporary playlist which will be deleted after songs are played or if user stops script
make playlist with properties {name:"Temp Playlist LBD"}
duplicate (every track of playlist 1 whose artist contains user_Artist and rating is greater than or equal to (user_Rating * 20)) to playlist "Temp Playlist LBD"
reveal playlist "Temp Playlist LBD"

--cycle through songs and play. Stop when end of list is reached.
try
	repeat with i from 1 to count (song_list)
		if song_Length is equal to "Preview" then
			reveal track i of playlist "Temp Playlist LBD"
			play track i of playlist "Temp Playlist LBD"
			delay 5
		else if song_Length is equal to "Whole Song" then
			reveal track i of playlist "Temp Playlist LBD"
			set track_duration to duration of track i of playlist "Temp Playlist LBD"
			play track i of playlist "Temp Playlist LBD" with once
			delay track_duration
		end if
	end repeat
on error
	delete playlist "Temp Playlist LBD"
	return
end try

delete playlist "Temp Playlist LBD"

end tell

Hi,

a new playlist is actually not necessary, try this


.
	--create song list for the chosen artist by unique track id instead of by name to make sure songs of the same name by other artists are not played
	set song_list to id of every track of playlist 1 whose artist contains user_Artist and rating is greater than or equal to (user_Rating * 20)
	
	--cycle through songs and play. Stop when end of list is reached.
	try
		repeat with aSong in song_list
			play aSong
			if song_Length is equal to "Preview" then
				delay 5
			else
				delay 1
				repeat while player state is playing
					delay 1
				end repeat
			end if
		end repeat
	end try
end tell

Thank you, Stefan.

I tried plugging your code into my script, replacing the portion you noted, but I can’t get it to work.

Were you able to get the entire script to run successfully with your suggested changes?

Thanks,

Lou

Ah…I figured it out.

play aSong needs to be …

play track id aSong
, then it works.

Lou