delimiter problem??

This is a section of code that lives in an automator workflow. The only automator steps are a “Find Item” and this “Run Applescript” section. I had it working all peachy but now I’ve gummed it all up trying to do one additional step. I have commented the area that I think is causing me trouble. What I’m trying to add is the separation of one of my lines of text. Other parts of the script are already doing a similar step so I’m stumped as to what’s going on. The error I’m getting says that it “can’t find folder:” and then shows a path to the “Montage Project Report” except for the section on the date seems to be funky. All I really need to do in the area marked is write the list items to a file, and when it comes to this line(or one like it) “Song File Names Unsung:Black Rain.mp3:Guns of Navarone.wav:America.wav”; I’d like to add a line return in place of the colon delimiters between the song names. My hunch is that I’m not setting the text delimiters correctly and it’s causing the filename to mess up. Any help would be appreciated! Thanks.

on run {input, parameters}
	set paragraphList to {}
	set trimList to {}
	set jobRecord to {}
	repeat with curItem in input
		tell application "Finder"
			if (label index of item curItem is 2) then
				-- If this text file's label index is 2, then skip
			else
				-- append its paragraphs to those already obtained.
				set paragraphList to paragraphList & paragraphs of (read curItem)
				set trimList to paragraphs of (read curItem)
				set newTrimList to {}
				set end of newTrimList to item 1 of trimList
				set end of newTrimList to item 2 of trimList
				set end of newTrimList to item 31 of trimList
				set end of newTrimList to item 32 of trimList

				set label index of item curItem to 2

				set end of jobRecord to newTrimList
			end if
		end tell
	end repeat

	set thisDay to day of (current date)
	set thisMonth to (current date)'s month as number
	set thisYear to year of (current date)
	set fileDate to thisMonth & "-" & thisDay & "-" & thisYear
	set Mfilename to "Montage Report " & fileDate & ".txt"
	set the_file to (((path to desktop) as string) & Mfilename) as file specification
	try
		
		script j
			property p : jobRecord
		end script
		open for access the_file with write permission
		set eof of the_file to 0
		write "Report created on  " & (current date) & return to the_file starting at eof
--P R O B L E M      A R E A		
                set astid to AppleScript's text item delimiters
		set AppleScript's text item delimiters to ":"
		repeat with curJob in jobRecord
			repeat with curInfo in curJob
				if (curInfo begins with "Song File Names") then
					-- If this paragraph begins with "Song File Names",
					-- get its text from the fourth word on.
					set thisInfo to text from word 4 to -1 of curInfo
					-- . and replace it in the list with a list of its colon-delimited text items.
					set splitSongs to thisInfo's text items
					display alert splitSongs as text
					repeat with oSong in splitSongs
						write oSong to the_file starting at eof
						write return to the_file starting at eof
					end repeat
				else
					write (curInfo as text) to the_file starting at eof
				end if
				

-- E N D     P R O B L E M      A R E A				
				write return to the_file starting at eof
				
			end repeat
			write return to the_file starting at eof
			write return to the_file starting at eof
		end repeat
		--write (jobRecord as text) to the_file starting at eof as list
		close access the_file
		set AppleScript's text item delimiters to astid
	on error
		try
			close access the_file
		end try
	end try
	
	-- (A script object for speed in dealing with the paragraph list.)
	script o
		property p : paragraphList
	end script
	
	set astid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to ":"
	
	-- Filter the paragraph list.
	repeat with i from 1 to (count paragraphList)
		set thisPara to item i of o's p
		if (thisPara begins with "Song File Names") then
			-- If this paragraph begins with "Song File Names",
			-- get its text from the fourth word on.
			set thisPara to text from word 4 to -1 of thisPara
			-- . and replace it in the list with a list of its colon-delimited text items.
			set item i of o's p to thisPara's text items

		else
			-- Otherwise replace the paragraph with a 'missing value'
			set item i of o's p to missing value
		end if
	end repeat
	
	set AppleScript's text item delimiters to astid
	-- Return the lists from the "paragraph" list.
	--return paragraphList's lists
	-- Get the lists from the "paragraph" list.
	set listOfLists to paragraphList's lists
	
	-- Concatenate them together to get a flat list of all their contents.
	set songFileList to {}
	repeat with i from 1 to (count listOfLists)
		set songFileList to songFileList & item i of listOfLists
	end repeat
	
	set songList to songFileList
	set outText to {}
	set outCheck to {}
	set i to 1
	repeat with i from 1 to (count of songList)
		set curItem to item i of songList
		set x to i
		set c to 1
		--compare the current item to the rest of the list
		repeat with x from i + 1 to (count of songList)
			if curItem is equal to item x of songList then
				set c to c + 1
			end if
			set x to x + 1
		end repeat
		--display alert "Item # " & i & " : " & text item i of songList & " appears " & c & " times"
		if curItem is in outCheck then
			--display alert "dupe!"
		else
			set end of outText to curItem & "	" & c
			set end of outCheck to curItem
		end if
		set i to i + 1
	end repeat
	
	set beginning of outText to "Report created on  " & (current date)
	set Afilename to "Song Usage Report " & fileDate & ".txt"
	set a_file to (((path to desktop) as string) & Afilename) as file specification
	try
		open for access a_file with write permission
		set eof of a_file to 0
		repeat with curItem in outText
			write (curItem as text) to a_file starting at eof
			write return to a_file starting at eof
			
		end repeat
		--write return to a_file starting at eof
		
		close access a_file
	on error
		try
			close access a_file
		end try
	end try
	
	return outText
	
end run

Side note: Two possible alternatives:

tell (current date) to tell ((100000000 + ¬
	(its year) * 10000 + ¬
	(its month) as integer) * 100 + ¬
	(its day)) as Unicode text to ¬
	set fileDate to text 9 thru 10 & "-" & text 11 thru 12 & "-" & text 3 thru 6

tell ((current date) as «class isot» as string) to set theDate to text 6 thru 10 & "-" & text 1 thru 4

Nitpick: Getting an alias from path to and then coercing it to something else is slightly less efficient than using the as parameter of path to directly:

set the_file to ((path to desktop as Unicode text) & Mfilename) as file specification

A perplexing problem, since there’s no mention of a folder anywhere in the script and the entire mention of the “Montage Project Report” file takes place within a ‘try’ block with no provision made to show any errors. :confused:

One error that’s definitely happening in the PROBLEM AREA is the ‘display alert’ line, which needs parentheses round the coercion of its parameter:

display alert (splitSongs as text)

Without the parentheses, ‘display alert’ is trying to display a list. The error will cause a jump strait to the ‘on error’ section, where the file will be closed without anything being written after the “Report created on .” line. Although you’ve arranged for the file to be closed in the event of an error, you haven’t arranged for AppleScript’s text item delimiters to be restored too, so setting astid to AppleScript’s text item delimiters again a couple of lines later will corrupt its value. However, I can’t see that that would cause any problems later in the script.

A faster and safer way to access a file that’s been opened for access is to use the reference number returned by ‘open for access’ rather than the file specifier:

set fRef to (open for access the_file with write permission)
set eof fRef to 0
write "Report created on " & (current date) & return to fRef

-- blah blah

write oSong & return to fRef

-- blah blah

close access fRef

There’s no need to use ‘starting at eof’ here, since the first write after opening for access automatically starts at the beginning of the file and subsequent writes continue from where previous ones leave off.

Sorry, though. I can’t see anything in the script that would give the error message you’re getting. :frowning:

Later edit:: Got it! I think.

set thisDay to day of (current date)
set thisMonth to (current date)'s month as number
set thisYear to year of (current date)
-- The above results are all integers.

set fileDate to thisMonth & "-" & thisDay & "-" & thisYear
-- The first item in this concatenation is an integer, so the result is a list:
-- eg. {9, "-", 25, "-", 2007}

set Mfilename to "Montage Report " & fileDate & ".txt"
-- The first item in this concatentaion is a string, so the list is coerced to string
-- using the current value of AppleScript's text item delimiters as the delimiter.
-- Owing to the failure to reset the TIDs after the 'display alert' error later in the script, the delimiter
-- for the application running the script (Automator?) remains a colon until the app quits.
-- If the script's run again before the application quits, it'll start with a delimiter of ":",
-- so the result of the list-to-string coercion will be "Montage report 9:-:25:-:2007.txt".

set the_file to (((path to desktop) as string) & Mfilename) as file specification
-- This is the line reporting the error. It's looking for a folder called "Montage Report 9"
-- containing a folder called "-" containing a folder called "25" containing a folder called "-".

The cure is to start the “integer” concatenations with a string, so that the result is a string rather than a list. (Or use Bruce’s alternatives, of course.) It wouldn’t hurt to reset the TIDs properly, either. :wink:

set thisDay to day of (current date)
set thisMonth to (current date)'s month as number
set thisYear to year of (current date)

set fileDate to (thisMonth as string) & "-" & thisDay & "-" & thisYear
--> "9-25-2007"

I didn’t notice that last night. :rolleyes:

Why not use text item delimiters to join the list items?

Try something like this (considering Nigel’s statements about write):

set astid to AppleScript's text item delimiters
try
	script j
		property p : jobRecord
	end script
	
	set fRef to (open for access the_file with write permission)
	set eof fRef to 0
	
	repeat with curJob in jobRecord
		repeat with curInfo in curJob
			if (curInfo begins with "Song File Names") then
				-- If this paragraph begins with "Song File Names",
				-- get its text from the fourth word on.
				set thisInfo to text from word 4 to -1 of curInfo
				
				-- . and replace it in the list with a list of its colon-delimited text items.
				set AppleScript's text item delimiters to ":"
				set splitSongs to text items of thisInfo
				
				-- display alert (splitSongs as text)
				
				set AppleScript's text item delimiters to return
				set splitSongs to "" & splitSongs
				
				write splitSongs & return to fRef
			else
				write curInfo & return to fRef
			end if
		end repeat
		
		write return & return to fRef
	end repeat
	
	--write (jobRecord as text) to fRef as list
	close access fRef
on error
	try
		close access fRef
	end try
end try
set AppleScript's text item delimiters to astid

Thanks guys, some of that went over my head but I was able to get it working. The added parenthesis on the date seemed to help for the file creation. Also fixing that bum display alert line fixed the whole loop somehow. Once I corrected that line or removed it, the script worked. Go figure…