I’m trying a script to delete some lines from iTunes lyrics. A widget adds lyrics but sometimes with a line (from http://thissite). Trying to remove that line, I want to place each line in a list, and create a new one excluding any matches.
I thought ASCII character 10 was supposed to be line feed. I tried 13 too but maybe the lyrics have some other format. Or something I’m not doing right.
Gary
on fixlyrics(tracklyrics)
set AppleScript's text item delimiters to ASCII character 10
set the itemlist to text items of tracklyrics
set templyrics to {}
repeat with currentline in itemlist
if currentline does not contain "www.metrolyrics" then
copy currentline to the end of templyrics
end if
end repeat
set newlyrics to templyrics as string
set AppleScript's text item delimiters to ""
return newlyrics
end fixlyrics
I found out I can separate the text using paragraphs. After that’s put into a list, I’m not sure how to re-create the paragraph. Tried a few things I thought should work.
I think this is the same thing as taking a paragraph of text and deleting unwanted lines from it. But I couldn’t find any other forum topics about it.
set AppleScript's text item delimiters to {"\n", "\r"}
AppleScript recognizes both carriage return and linefeed as line delimiters. some of your imported lyrics may contain one, the other or both.
Then again, if the text you want to remove is constant, say “this is the annoying text”, you can just set the text item delimiters to the problem text:
set t to "Here is some text. this is the annoying text. Here is some more text"
set AppleScript's text item delimiters to "this is the annoying text. "
set z to every text item of t as list
set AppleScript's text item delimiters to ""
set res to every text item of z as text
When you’re having the option ‘place excapses before tabs, line ends and special characters’ turned off in script editor. you could replace {“\n”, “\r”} for {character id 10, character id 13} but don’t use ascii character anymore, this is deprecated and works with single byte characters and not with multibyte characters.
paragraphs instead of text item delimiters can handle
all combinations of new line characters
on fixlyrics(tracklyrics)
set itemlist to paragraphs of tracklyrics
set templyrics to {}
repeat with currentline in itemlist
if currentline does not contain "www.metrolyrics" then
copy currentline to the end of templyrics
end if
end repeat
set {TID, text item delimiters} to {text item delimiters, return}
set newlyrics to templyrics as text
set text item delimiters to TID
return newlyrics
end fixlyrics
I think Stefan’s idea works well. I had tried paragraphs in the last few days and I was able to separate items into a list. The strange thing is when I reassemble the list (with ‘returns’) and compare, they’re the same.
The idea of using returns as TIDs should help. I wasn’t sure how to reassemble the list for a while and then I tried ‘set fixedlyrics to currentline & return’ but the other way seems better.
Testing for conditions like double returns and an empty line at the end (not sure how to do that without interating thru each line) is really making the code messy. Maybe there’s another way but it works for now.