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