Two text item delimiters within one tell block.

I’ve written a script called “Newly Added Albums to iPod Notes”. Basically it gets album name and artist name of every album in a specified iTunes playlist (in my case a smart-playlist which lists albums which have been added in the last week and are in the playlist which my iPod is set to sync to.

I’m trying to improve the script by adding HTML links wich will allow the user to start the album playing directly from the “Newly Added Albums” note on the iPod.

In order to adapt my script, I think I need to change the text item delimiters from within a “TextEdit” tell block, and then back again. The reason is that for each link, I need to extract the artist and album name from a variable which looks like this:

playlist_items = {“artist name 1 - album name 1” , “artist name 2 - album name 2” , “artist name 3 - album name 3”}

the text item delims are set to " , " and I need to create a new variable from the above list using the " - " as the delimiter.

In the following segment of script I want to use the “repeat with artist_album in playlist_items” repeater to set the above information. But of course TextEdit doesn’t know how to set the text item deliimiters, so this doesn’t work:

repeat with _item in each_paragraph
			repeat with artist_album in playlist_items
				set text item delimiters to " - "
				set artist_name to item 1 of artist_album
				set album_name to item 2 of artist_album
				set text item delimiters to " , "
				
			end repeat
			
			write _item & return & "<a href=\"ipod:music?artis=" & artist_name & "&album=" & album_name & "\">Play</a>" & return & return to the open_target_file starting at eof
			
		end repeat

Can anyone help me with this, or perhaps suggest a better way?

tell application "TextEdit" to set AppleScript's text item delimiters to " - "

get text item delimiters
--> " - "

No. Text Item Delimiters are owned by AppleScript itself (I think in very ancient versions you could set TIDs for particular apps, though), and they will work in whatever part of whatever script. Its default value is a single-item-list, which is an empty string: {“”}. Comma is the separator for list items, and you can’t change it.

So, in your code, I would just modify:

set artist_name to item 1 of artist_album's text items
set album_name to item 2 of artist_album's text items
set text item delimiters to {""} --> its default value

I followed the advice of silvermeteors and it is now almost working… it properly sets the artist and album name variables and then places them into the text file. The problem now: the repeat block is somehow broken!

repeat with _item in each_paragraph
			tell application "TextEdit" to set AppleScript's text item delimiters to " - "
			set name_artist to text item 1 of _item
			set name_album to text item 2 of _item
			tell application "TextEdit" to set AppleScript's text item delimiters to " , "
			
			write _item & return & "<a href=\"ipod:music?artist=" & name_artist & "&album=" & name_album & "\">Play</a>" & return & return to the open_target_file starting at eof
		end repeat

The result of the above is this:

It’s properly formatted but it’s only writing the first item of each_paragraph!

When I run the older script (which excludes the html markup and changes to the T.I. delims) the result is this:

How could the above script have broken the repeat command? ARG!

Woops, I should have noticed that in your code. You need to to address the contents of your list item before parsing it.

 set name_artist to text item 1 of _item's contents
           set name_album to text item 2 of _item's contents

^ I’m still getting a result from only the first item of each_paragraph.

Hmmm… on second look your code seems to have changed quite a bit from version 1. Would you mind posting the whole script?

sure, here it is:

set text item delimiters to ", "

tell application "iTunes"
	set this_plist to name of playlist "Newly Added nano Music"
	
	set last_day to every track of playlist this_plist
	set playlist_items to {}
	repeat with _album in last_day
		set album_name to album of _album as string
		set artist_name to artist of _album as string
		
		set artist_album to artist_name & " - " & album_name
		if playlist_items does not contain artist_album & ", " then
			set the end of playlist_items to artist_album & ", "
		end if
		
	end repeat
	
end tell

tell application "TextEdit"
	
	set Filename to "Newly Added Albums" --Name the file 
	
	set this_data to playlist_items as string --Any text here
	--set target_file to "Macintosh HD:Users:tomwhite:Desktop:" & Filename
	set target_file to "iPod nano 4GB:Notes:" & Filename --You can eliminate "Filename" as a variable and type the filename as part of the path (if you wish) but you need a full path 
	set append_data to false --Set this to false if you want to overwrite, true if you want to add text to an existing file
	
	try --This part writes the file
		set the target_file to the target_file as text
		set the open_target_file to open for access file target_file with write permission
		if append_data is false then set eof of the open_target_file to 0
		
		set each_paragraph to every text item of this_data
		
		repeat with _item in each_paragraph
			tell application "TextEdit" to set AppleScript's text item delimiters to " - "
			set name_artist to text item 1 of _item's contents
			set name_album to text item 2 of _item's contents
			tell application "TextEdit" to set AppleScript's text item delimiters to " , "
			
			write _item & return & "<a href=\"ipod:music?artist=" & name_artist & "&album=" & name_album & "\">Play</a>" & return & return to the open_target_file starting at eof
		end repeat
		
		close access the open_target_file
	on error
		try
			close access file target_file
		end try
	end try
end tell

Okay, I’ve read through your script and I think I understand what you’re trying to do. AppleScript lacks good facilities for managing the database-like information your script requires, which makes clever approaches necessary. I thought the best prescription may be to re-write the script from the ground up, using a single repeat loop to cycle through pre-loaded album/artist information in a two-part variable, remove the duplicates, and then write to an output variable all in one motion:

tell application "iTunes"
	(*
	-- these are just test values I used for debugging:
	set my_playlist to playlist 7
	set my_file to ((path to desktop from user domain as string) & "test_note.txt")
	*)
	set my_playlist to playlist "Newly Added nano Music"
	set my_file to "iPod nano 4GB:Notes:Newly Added Albums"
	set replace to false
	set the_data to ""
	set album_history to {}
	
	tell every track of my_playlist to copy {artist, album} to {every_artist, every_album}
	
	repeat with x from 1 to (count every_artist)
		
		set an_artist to item x of every_artist
		set an_album to item x of every_album
		
		if an_album is not in album_history then
			copy an_album to end of album_history
			set the_data to the_data & an_artist & " - " & an_album & space & "<a href=\"ipod:music?artist=" & an_artist & "&album=" & an_album & "\">Play</a>" & return
		end if
		
	end repeat
end tell

set the_file to open for access file my_file with write permission
if replace then set eof of the_file to 0
write the_data to the_file starting at eof
close access the_file

Let me know if this works for you, and if I missed something from your original objective.

Edit: I re-wrote it again so that it’s a bit easier to read, the functionality is the same.

Wow, this is exactly what I was trying to do! I’ll have to give it a good studying

Thanks for all your help! Works fantastically!

Hmm…

For some reason the script wasn’t over-writing the file. No changes were being made if that file already existed at the target location. I added a Finder tell block at the beginning of it which just deletes the “Newly Added Albums” text file upon each launch, and now it’s working perfectly!

Basically this is a workaround to the fact that it’s impossible to ‘browse’ an iPod’s playlists by artist. One could link this script to any of their iPod’s playlists and then have the ability to browse it on the iPod’s Notes Reader.

So anyway, thanks again silvermeteors, this is exactly what I was trying to do. I’ll try to learn from your improvements to the code.