Numbering items of a text list

Hi everyone

I’m trying to make a script that will copy a songs name/artist to a text file. Here is what I have so far:


--  let user select starting number
set _count to text returned of (display dialog "" buttons {"OK", "Cancel"} default button 1 default answer "Enter starting point here.") as integer
copy {} to _sList
tell application "iTunes"
	--  activate
	if selection is {} then
		--  retrieve all songs from the currently selected playlist
		copy view of front window to _selectedLibrary
		copy every track of _selectedLibrary to _selectedSongs
	else
		--  copy selected songs only
		copy selection to _selectedSongs
	end if
	
	repeat with _songInfo in _selectedSongs
		--  Loop thru each track and arange in list (EX: 1. Here With Me by ATB)
		copy {name, artist} of _songInfo to {_sName, _sArtist}
		set the end of _sList to (_count & ".  " & _sName & " by " & _sArtist)
		--  increment numbering for next track
		set _count to _count + 1
	end repeat
end tell --  iTunes

if _sList is not {} then
	set text item delimiters to return
	try
		--  create new txt file @ desktop
		set _infoFile to (((path to desktop) as string) & "text.txt")
		set _txtFile to open for access _infoFile with write permission
		
		write (_sList as text) to _txtFile starting at eof
		close access _txtFile
	on error errmess
		close access _txtFile
		display dialog errmess buttons "Cancel" default button 1
	end try
end if

It works fine except that the indetations are wrong. The text file “list” looks like this:
1
.
Public Pervert (Carlos D Remix)
by
Interpol
2
.
Bad Romance
by
LGG
3
.
Sex is on Fire
by
Kings of Leon

I have a feeling it has to do with the text item delimiters but I’m not sure what I can do to fix it.

Browser: Safari 531.22.7
Operating System: Mac OS X (10.4)

It has everything to do with setting the text item delimiters to return and never setting them back to their default: “” – a big mistake because AppleScript remembers them from run to run. I’m not sure what you want the list to look like though. Give an example.

set tid to AppleScript's text item delimiters -- grab what they currently are
set applescript's text item delimiters to whatEver
set tParts to text items of someText
do stuff to the parts
set AppleScript's text item delimiters to tid -- set them back as they were

Hello Adam.

I have a little off topic question for you, as it was the text item delimiters that triggered it.

If a handler is called within an applications terminology scope, and you only set the text item delimiters, and not AppleScript’s, you would only affect the text item delimiters within that terminology scope wouldn’t you?

The reason for this quest was that I had used “days” in a handler that I called up from within an Excel tell block,
and the result wasn’t as meagre as it should have been, that lead to an error message, from which I deduce, that the
callers terminology scope is visible from within a handler.

What I then basically wonder about, is if there is “local” text item delimiters per applications tell block.

Thanks. :slight_smile:

Darn! This is what I get for getting ahead of myself. Well, if AS remembers TID from run to run, How do I set them back to their original default? As for, “What should the list look like?”… well, I’m trying to aim for something like this

  1. Here With Me by ATB
  2. Crazy by Gnarles Barkley
  3. Highland’s Apparition by Chevelle
    … etc.

If you don’t know what the were, just set them to nothing


set AppleScript's text item delimiters to ""

That is the value they IMHO should have whenever a script or application has executed anyway.

Remember once, he he he, I ran a file rename script, and it turned out that the text item delimiter of the day was a comma, he he he.

So I think it is wise practice to never believe anything about text item delimiters, always set them in order to avoid peculiar side-effects.

What has happened to your script is that you got a return after each text item, since you haven’t set back the text item delimiter to “” . As said it is wise to save the Original Value before running your script, then set them to a value you can live with, and then set them back to the original value when you are done.

Well thanks for the help guys. Seems like this:


set AppleScript's text item delimiters to ""

did restored the defualt tid, but the list still didn’t look right.

After thinking about, I came to the odvious realization that this (note the “& return”):


set the end of _sList to (_count & ".  " & _sName & " by " & _sArtist & return)

works just fine haha!

always the little things…