Clean up the result?

Try running this script (you can change the artist if you want):


set artist to "korn"

set artist to my clean_seek_string(artist)
do shell script "curl [url=http://www.seeklyrics.com/lyrics/]http://www.seeklyrics.com/lyrics/"[/url] & artist & "/"
set seekcurled to every paragraph of result
if item 140 of seekcurled is not "                      <p>Data cannot be found.</p>" then
	repeat until item 1 of seekcurled contains "!-- SONGS START --"
		set seekcurled to rest of seekcurled
	end repeat
	set seekcurled to rest of rest of rest of rest of seekcurled
	set seekcurled to reverse of seekcurled
	repeat until item 1 of seekcurled contains "click here to view all lyrics"
		set seekcurled to rest of seekcurled
	end repeat
	set seekcurled to rest of rest of rest of seekcurled
	set seekcurled to reverse of seekcurled
	set seekcurled to my list_to_string(seekcurled, return)
	set seekcurled to my snr(seekcurled, "tr", "")
	set seekcurled to my snr(seekcurled, "TR", "")
       set seekcurled to my snr(seekcurled, "</a>", "")
	set seekcurled to my snr(seekcurled, "<", "")
	set seekcurled to my snr(seekcurled, ">", "")
	set seekcurled to my snr(seekcurled, "=", "")
	set seekcurled to my snr(seekcurled, "a href", "")
	set seekcurled to my snr(seekcurled, artist, "")
	set seekcurled to my snr(seekcurled, "\"", "")
	set seekcurled to my snr(seekcurled, "classtext bgcolorwhite -", "")
	set seekcurled to my snr(seekcurled, "lyrics", "")
	set seekcurled to my snr(seekcurled, ".html", "")
	set seekcurled to my snr(seekcurled, "/", "")
	set seekcurled to my snr(seekcurled, "classtlink", "")
	set seekcurled to my snr(seekcurled, "TD", "")
	set seekcurled to my snr(seekcurled, "td", "")
	set seekcurled to my snr(seekcurled, "title", "")
	set seekcurled to my snr(seekcurled, "classtext", "")
	set seekcurled to my snr(seekcurled, "TH  bgcolor#aabbbb colspan2 alignleftb stylecolor:whiteTop 30   bTH", "")
	return seekcurled
else
	return "error"
end if

on list_to_string(the_list, the_delim)
	tell (a reference to my text item delimiters)
		set {old_tid, contents} to {contents, the_delim}
		set {the_list, contents} to {"" & the_list, old_tid}
	end tell
end list_to_string

on snr(the_string, search_string, replace_string)
	tell (a reference to my text item delimiters)
		set {old_tid, contents} to {contents, search_string}
		set {the_string, contents} to {the_string's text items, replace_string}
		set {the_string, contents} to {"" & the_string, old_tid}
	end tell
	return the_string as Unicode text
end snr

on clean_seek_string(the_string)
	set search_strings to (" '/.()&-")'s characters
	repeat with i from 1 to (count search_strings)
		set the_string to my snr(the_string, item i of search_strings, "-")
	end repeat
	return the_string
end clean_seek_string


How do I remove the 1st and 3rd copies of each song name and make it into a list (ex: {song1,song2,song3…})?

And if possible - assign them to a combo box?

Hmmm… for some reason, curl wouldn’t work for me on that site - so I’ve used URL Access Scripting instead. (You can easily change it back if required.) Unfortunately, since your script also kept crashing my copy of Script Editor, I didn’t get much of a chance to analyse the exact nature of the problem. Oh well… :confused:

I notice that apostrophes come through as ascii character 146 (rather than 39). However, since I don’t know exactly how you intend to use the results, I haven’t tried to fix that. (It’s a trivial matter to remedy, anyway.)

As for parsing the list of song titles itself, I think I’d take a slightly different approach. This has worked consistently for every artist I’ve tried so far:

to switchText from t to r instead of s
	set text item delimiters to s
	set t to t's text items
	set text item delimiters to r
	tell t to item 1 & ({""} & rest)
end switchText

to cleanText from t
	repeat with c in " '/.()&"
		set t to switchText from t to "-" instead of c
	end repeat
end cleanText

to getText from urlRef
	set fileRef to ((path to "temp" as Unicode text) & "url")
	using terms from application "URL Access Scripting"
		tell application ((path to "Æ’scr" as Unicode text) & "URL Access Scripting.app:") to download urlRef to fileRef replacing yes
	end using terms from
	read file fileRef
end getText

to listTitles for artist
	set c to ASCII character 10
	set d to text item delimiters
	try
		set artist to cleanText from artist
		set t to getText from "http://www.seeklyrics.com/lyrics/" & artist & "/"
		if "<p>Data cannot be found.</p>" is in t then error "Could not find any lyrics for " & artist & "."
		set l to {}
		set text item delimiters to "<!-- SONGS START -->"
		set t to t's text item 2
		set text item delimiters to "<b>Click Here to View All Lyrics</b>"
		tell t's text item 1 to set t to text from paragraph 3 to -1
		set text item delimiters to "<"
		set t to rest of t's text items
		set text item delimiters to ">"
		repeat with k in t
			set l's end to rest of k's text items
		end repeat
		set text item delimiters to ""
		set t to l as string
		set text item delimiters to c & c
		set t to t's text item 1
		set text item delimiters to c & " - "
		set t to rest of t's text items
		set text item delimiters to return
		set t to (t as string)'s paragraphs
		set text item delimiters to d
		t
	on error e
		set text item delimiters to d
		display dialog e buttons {"Cancel"} default button 1 with icon 0
	end try
end listTitles

listTitles for "Jim Croce"

--> {"Time in a Bottle Lyrics", "Iíll Have to Say I Love You in a Song Lyrics", "Operator Lyrics", "Bad, Bad Leroy Brown Lyrics", etc...}

Thanks but how do I remove Lyrics after every song? (I don’t understand the script now that it’s been changed :/)

Can you make that into on __ () 's because I need to call that from on clicked

I appreciate how daunting it can be to wade through someone else’s code, especially if it’s a bit on the lengthy side. But take it a step at a time, and I’m sure you’ll pick up most of what’s going on at each stage. After that, if there are still parts that you don’t understand, just holler. :slight_smile:

While I’ve made a few other subtle changes here and there, the most significant amendments are the 2 lines inserted about halfway down the ‘listTitles’ handler:

	[b]set[/b] t [b]to[/b] switchText [b]of[/b] t [b]from[/b] " Lyrics" & c [b]to[/b] c
	[b]set[/b] t [b]to[/b] switchText [b]of[/b] t [b]from[/b] "í" [b]to[/b] "'"

The first statement removes the word “Lyrics” at line ends, while the second fixes the apostrophe problem that I mentioned earlier.

Here’s the complete revision:

to switchText of t from s to r
	set text item delimiters to s
	set t to t's text items
	set text item delimiters to r
	tell t to beginning & ({""} & rest)
end switchText

to cleanText from t
	repeat with c in " '/.()&"
		set t to switchText of t from c to "-"
	end repeat
end cleanText

to getText from urlRef
	set fileRef to ((path to "temp" as Unicode text) & "url")
	using terms from application "URL Access Scripting"
		tell application ((path to "Æ’scr" as Unicode text) & ¬
			"URL Access Scripting.app:") to download urlRef to fileRef replacing yes
	end using terms from
	read file fileRef
end getText

to listTitles for artist
	set c to ASCII character 10
	set d to text item delimiters
	try
		set artist to cleanText from artist
		set t to getText from "http://www.seeklyrics.com/lyrics/" & artist & "/"
		if "<p>Data cannot be found.</p>" is in t then error "Could not find any lyrics for " & artist & "."
		set l to {}
		set text item delimiters to "<!-- SONGS START -->"
		set t to t's text item 2
		set text item delimiters to "<b>Click Here to View All Lyrics</b>"
		tell t's text item 1 to set t to text from paragraph 3 to -1
		set text item delimiters to "<"
		set t to rest of t's text items
		set text item delimiters to ">"
		repeat with k in t
			set l's end to rest of k's text items
		end repeat
		set text item delimiters to ""
		set t to l as string
		set t to switchText of t from " Lyrics" & c to c
		set t to switchText of t from "í" to "'"
		set text item delimiters to c & c
		set t to t's text item 1
		set text item delimiters to c & " - "
		set t to rest of t's text items
		set text item delimiters to return
		set t to (t as string)'s paragraphs
		set text item delimiters to d
		t
	on error e
		set text item delimiters to d
		display dialog e buttons {"Cancel"} default button 1 with icon 0
	end try
end listTitles

listTitles for "Mary Black"

--> {"Fields of Gold", "Song for Ireland", "No Frontiers", "Katie", "Bright Blue Rose", "The Water is Wide", "Past The Point of Rescue", etc...}

o i get it. it would be

on clicked theObject
if the name of theObject is "circle" then
listTitles for "Mary Black"
end if
end clicked

and the rest would be at the bottom of my script with the other functions

That’s right. The user’s button click is passed to the ‘clicked’ handler, which then calls the ‘listTitles’ handler, which in turn calls the other handlers… :slight_smile:

ok i’m just gonna post the whole script for the button


on clicked theObject
if the name of theObject is "circle" then
		Seek()
		listTitles for the contents of text field "artistname1" of window "main"
		set songlist to result
		repeat with a from 2 to 29
			set the contents of combo box item a of combo box 1 of window "main" to (item a of songlist as string)
		end repeat
	end if
end clicked

on Seek()
	update_progress(1, 100)
	set songname to the contents of text field "songname1" of window "main"
	set artistname to the contents of text field "artistname1" of window "main"
	if artistname contains "billy joel" then
		set artistname to "joel billy"
	end if
	set songname to my clean_seek_string(songname)
	set artistname to my clean_seek_string(artistname)
	set curled to (do shell script "curl seeklyrics.com/lyrics/" & artistname & "/" & songname & ".html")'s paragraphs
	if item 140 of curled is "                      <p>Data cannot be found.</p>" then
		AZ()
	else
		repeat until item 1 of curled contains "	<b>"
			set curled to rest of curled
		end repeat
		set curled to rest of curled
		set curled to reverse of curled
		repeat until item 1 of curled contains "</pre>"
			set curled to rest of curled
		end repeat
		set curled to rest of curled
		set curled to reverse of curled
		set curled to my list_to_string(curled, return)
		set curled to my snr(curled, "<PRE>", "")
		set curled to my snr(curled, "<pre>", "")
		set curled to my snr(curled, "<i>", "")
		set curled to my snr(curled, "</i>", "")
		set curled to my snr(curled, "	", "")
		set curled to my snr(curled, "</b>", "")
		set curled to my snr(curled, "<b>", "")
		update_progress(0, 100)
		set the contents of text view 1 of scroll view 1 of window "main" to "Retrieved from [url=http://www.seeklyrics.com]www.seeklyrics.com[/url]" & return & return & curled as string
	end if
end Seek
on AZ()
	set songname to the contents of text field "songname1" of window "main"
	set artistname to the contents of text field "artistname1" of window "main"
	set songname to my clean_string(songname)
	set artistname to my clean_string(artistname)
	set curled to (do shell script "curl azlyrics.com/lyrics/" & artistname & "/" & songname & ".html")'s paragraphs
	if item 3 of curled contains "robots" then
		LyricsStyle(songname, artistname)
	else
		repeat with i from 1 to (count curled)
			if item i of curled contains "<font size=2>" then exit repeat
		end repeat
		set curled to reverse of (curled's items (i + 1) thru -1)
		repeat with i from 1 to (count curled)
			if item i of curled contains "<a href=\"http://www.azlyrics.com\">www.azlyrics.com</a>" then exit repeat
		end repeat
		set curled to reverse of (curled's items (i + 2) thru -1)
		set curled to my list_to_string(curled, return)
		set curled to my snr(curled, "<BR>", "")
		set curled to my snr(curled, "<br>", "")
		set curled to my snr(curled, "<i>", "")
		set curled to my snr(curled, "</i>", "")
		set curled to my snr(curled, "</b>", "")
		set curled to my snr(curled, "<b>", "")
		update_progress(0, 100)
		set the contents of text view 1 of scroll view 1 of window "main" to "Retrieved from [url=http://www.azlyrics.com]www.azlyrics.com[/url]" & return & return & curled
	end if
end AZ

on snr(the_string, search_string, replace_string)
	tell (a reference to my text item delimiters)
		set {old_tid, contents} to {contents, search_string}
		set {the_string, contents} to {the_string's text items, replace_string}
		set {the_string, contents} to {"" & the_string, old_tid}
	end tell
	return the_string as Unicode text
end snr

on clean_string(the_string)
	set search_strings to (" '/.()&-,")'s characters
	repeat with i from 1 to (count search_strings)
		set the_string to my snr(the_string, item i of search_strings, "")
	end repeat
	return my convert_to_lower(the_string)
end clean_string

on clean_seek_string(the_string)
	set search_strings to (" '/.()&-")'s characters
	repeat with i from 1 to (count search_strings)
		set the_string to my snr(the_string, item i of search_strings, "-")
	end repeat
	return the_string
end clean_seek_string

on convert_to_lower(the_string)
	set the_string to the_string's characters
	repeat with i from 1 to (count the_string)
		set ascii_num to ASCII number (item i of the_string)
		if ascii_num > 64 and ascii_num < 91 then set item i of the_string to (ASCII character (ascii_num + 32))
	end repeat
	return the_string as Unicode text
end convert_to_lower

on list_to_string(the_list, the_delim)
	tell (a reference to my text item delimiters)
		set {old_tid, contents} to {contents, the_delim}
		set {the_list, contents} to {"" & the_list, old_tid}
	end tell
end list_to_string

on update_progress(iteration, total_count)
	tell window "main"
		if iteration = 1 then
			tell progress indicator "progress" to start
			set indeterminate of progress indicator "progress" to true
		else
			tell progress indicator "progress" to stop
			set indeterminate of progress indicator "progress" to false
		end if
		set maximum value of progress indicator "progress" to total_count
		set content of progress indicator "progress" to iteration
		update
	end tell
end update_progress

on LyricsStyle(songname, artistname)
	set artistletter to character 1 of artistname
	set curled to (do shell script "curl lyricsstyle.com/" & artistletter & "/" & artistname & "/" & songname & ".html")'s paragraphs
	
	if item 3 of curled contains "404" then
		set the contents of text view 1 of scroll view 1 of window "main" to "Sorry that song wasn't found."
		update_progress(0, 100)
	else if item 3 of curled contains "302" then
		set the contents of text view 1 of scroll view 1 of window "main" to "Sorry that song wasn't found."
		update_progress(0, 100)
	else
		repeat until item 1 of curled is "<!-- FreeFind End No Index -->"
			set curled to rest of curled
		end repeat
		repeat until item 1 of curled is "<pre width=\"75\"><font face=\"verdana, times new roman\" size=\"+0\">"
			set curled to rest of curled
		end repeat
		set curled to reverse of rest of curled
		repeat until item 1 of curled is "</PRE>"
			set curled to rest of curled
		end repeat
		set curled to reverse of rest of rest of curled
		set curled to my list_to_string(curled, return)
		set the contents of text view 1 of scroll view 1 of window "main" to "Retrieved from [url=http://www.lyricsstyle.com]www.lyricsstyle.com[/url]" & return & return & curled as string
		update_progress(0, 100)
	end if
end LyricsStyle
to switchText of t from s to r
	set text item delimiters to s
	set t to t's text items
	set text item delimiters to r
	tell t to beginning & ({""} & rest)
end switchText

to cleanText from t
	repeat with c in " '/.()&"
		set t to switchText of t from c to "-"
	end repeat
end cleanText

to getText from urlRef
	set fileRef to ((path to "temp" as Unicode text) & "url")
	using terms from application "URL Access Scripting"
		tell application ((path to "Æ’scr" as Unicode text) & ¬
			"URL Access Scripting.app:") to download urlRef to fileRef replacing yes
	end using terms from
	read file fileRef
end getText

to listTitles for artist
	set c to ASCII character 10
	set d to text item delimiters
	try
		set artist to cleanText from artist
		set t to getText from "http://www.seeklyrics.com/lyrics/" & artist & "/"
		if "<p>Data cannot be found.</p>" is in t then error "Could not find any lyrics for " & artist & "."
		set l to {}
		set text item delimiters to "<!-- SONGS START -->"
		set t to t's text item 2
		set text item delimiters to "<b>Click Here to View All Lyrics</b>"
		tell t's text item 1 to set t to text from paragraph 3 to -1
		set text item delimiters to "<"
		set t to rest of t's text items
		set text item delimiters to ">"
		repeat with k in t
			set l's end to rest of k's text items
		end repeat
		set text item delimiters to ""
		set t to l as string
		set t to switchText of t from " Lyrics" & c to c
		set t to switchText of t from "í" to "'"
		set text item delimiters to c & c
		set t to t's text item 1
		set text item delimiters to c & " - "
		set t to rest of t's text items
		set text item delimiters to return
		set t to (t as string)'s paragraphs
		set text item delimiters to d
		t
	on error e
		set text item delimiters to d
		display dialog e buttons {"Cancel"} default button 1 with icon 0
	end try
end listTitles


can you tell me what’s wrong with this?

I’m not sure which would be quicker: going through the script and analysing what could go wrong, or building an app here to test what is causing a problem - and where. :wink:

I obviously didn’t realise that your script consisted of other handlers that share some of the same subroutines as your original post. Small wonder, then, that you seemed less than happy about my suggestion - when you said:

I seem to have inadvertently created even more confusion by suggesting alternative handlers, since we now have some subroutines that largely duplicate the role of the originals. The result is an unnecessarily bloated script that presents a potential debugging nightmare. :o

In view of this, I think it might be better if you to reverted to your original code. This should simplify things for you considerably - as well as allow you to troubleshoot more easily with familiar code.

So, to allow me to answer your original question…

…I’ve now managed to isolate the source of the Script Editor crashes here. Turns out they were caused by the coercion to unicode text in your ‘snr’ handler. Since the script should work quite happily without this, I simply changed:

to:

With your original script now working here, I could see the results and understand your question better - so I’ve written a handler that should provide what you need. It requires a small modification towards the end of the main routine, by changing:

to:

Then, simply add this short handler to the original script:

to cutDupesAndList(t)
	set d to text item delimiters
	set text item delimiters to " Lyrics"
	set t to t's text items
	set text item delimiters to d
	repeat with n from 1 to (count t) by 2
		set t's item n to 0
	end repeat
	t's strings
end cutDupesAndList

With that in place, you should be able to continue developing and refining your app using the code that you feel more comfortable with.

Once again, apologies for any confusion - and good luck with your script! :slight_smile:

Thanks for all the help! :smiley: I decided to use choose from list instead of the combo box because it is so much easier. :stuck_out_tongue: I put your name in the credits of my app - Return Lyrics (http://returnlyrics.netfirms.com). I’ll post again once I finish this version.

You’re most welcome - and I appreciate your generosity. Your site and product both look great! :slight_smile: