The following script prompts you for a song name and the artist of that song. It then searches www.lyricsdir.com for the lyrics and writes them to a text file. I would like to see this script expanded, see what else other people can think of. Maybe more websites to search. Anyway here is the code.
display dialog "What is the song called?" default answer ""
set theSong to text returned of result
display dialog "Who is the artist?" default answer ""
set theArtist to text returned of result
set theSong to edittext(theSong)
set theArtist to edittext(theArtist)
set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {" "}
set theSong to text items of theSong
set theArtist to text items of theArtist
set AppleScript's text item delimiters to {"-"}
set theSong to theSong as Unicode text
set theArtist to theArtist as Unicode text
set AppleScript's text item delimiters to {""}
get "http://www.lyricsdir.com/" & theArtist & "-" & theSong & "-lyrics.html"
do shell script "/usr/bin/curl --user-agent '' " & quoted form of result
set theLyrics to result
set AppleScript's text item delimiters to {"<div id=\"lyrics\">"}
set theLyrics to (last text item of theLyrics) as Unicode text
set AppleScript's text item delimiters to {"</div>"}
set theLyrics to first text item of theLyrics
set AppleScript's text item delimiters to {"<br />"}
set theLyrics to text items of theLyrics
set AppleScript's text item delimiters to {""}
set theLyrics to text 2 thru -1 of (theLyrics as Unicode text)
set AppleScript's text item delimiters to ASTID
on edittext(someText)
return do shell script "/usr/bin/python -c \"import sys; print unicode(sys.argv[1], 'utf8').lower().encode('utf8')\" " & quoted form of someText
-- Remove anything that is not alphanumeric or a space
return do shell script "echo " & quoted form of someText & " | /usr/bin/ruby -ne 'print $_.delete(\"^a-z\", \"^A-Z\", \"^0-9\", \"^ \")'"
end edittext
set user_text to theLyrics
set file_name to (choose file name with prompt "Please type a file name" default name theSong & ".txt")
if file_name as string does not end with ".txt" then set file_name to ((file_name as string) & ".txt")
my make_report(file_name, user_text)
set dMes to return & return & "Would you like to open the file now?"
set opts to (display dialog "Done!" & dMes buttons {"Yes", "No Thanks"} default button 1 with icon 1 giving up after 30)
if button returned of opts is "Yes" or gave up of opts is true then
tell application "TextEdit"
activate
try
open (file_name as alias)
on error errMs
display dialog errMs buttons {"Cancel"}
end try
end tell
end if -- button is "Done"
to make_report(file_name, user_text)
try
do shell script "rm " & quoted form of POSIX path of file_name
end try
try
set fileRefr to (a reference to (open for access file_name with write permission))
write user_text to fileRefr
close access fileRefr
on error errx number errNum from badObj
try
close access fileRefr
end try
log errNum
if (errNum is equal to -48) then
do shell script "rm " & quoted form of POSIX path of file_name
my make_report()
else
display dialog "There has been an error creating the file:" & return & return & (badObj as string) & errx & return & "error number: " & errNum buttons {"Cancel"}
end if
end try
end make_report
on error_and_cancel(ms)
if gave up of (display dialog ms with icon 2 buttons {"Cancel"} default button 1 giving up after 15) then error number -128
end error_and_cancel
display dialog "Please specify the type of lyric search:" buttons {"Song being played on iTunes", "Specify song"} default button "Song being played on iTunes"
if the button returned of the result is "Song being played on iTunes" then
tell application "iTunes"
set theSong to the name of the current track as text
set theArtist to the artist of the current track as text
display dialog theArtist & return & theSong
end tell
else
display dialog "What is the song called?" default answer ""
set theSong to text returned of result
display dialog "Who is the artist?" default answer ""
set theArtist to text returned of result
end if
set theSong to edittext(theSong)
set theArtist to edittext(theArtist)
set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {" "}
set theSong to text items of theSong
set theArtist to text items of theArtist
set AppleScript's text item delimiters to {"-"}
set theSong to theSong as Unicode text
set theArtist to theArtist as Unicode text
set AppleScript's text item delimiters to {""}
get "http://www.lyricsdir.com/" & theArtist & "-" & theSong & "-lyrics.html"
do shell script "/usr/bin/curl --user-agent '' " & quoted form of result
set theLyrics to result
set AppleScript's text item delimiters to {"<div id=\"lyrics\">"}
set theLyrics to (last text item of theLyrics) as Unicode text
set AppleScript's text item delimiters to {"</div>"}
set theLyrics to first text item of theLyrics
set AppleScript's text item delimiters to {"<br />"}
set theLyrics to text items of theLyrics
set AppleScript's text item delimiters to {""}
set theLyrics to text 2 thru -1 of (theLyrics as Unicode text)
set AppleScript's text item delimiters to ASTID
on edittext(someText)
return do shell script "/usr/bin/python -c \"import sys; print unicode(sys.argv[1], 'utf8').lower().encode('utf8')\" " & quoted form of someText
-- Remove anything that is not alphanumeric or a space
return do shell script "echo " & quoted form of someText & " | /usr/bin/ruby -ne 'print $_.delete(\"^a-z\", \"^A-Z\", \"^0-9\", \"^ \")'"
end edittext
set user_text to theLyrics
set file_name to (choose file name with prompt "Please type a file name" default name theSong & ".txt")
if file_name as string does not end with ".txt" then set file_name to ((file_name as string) & ".txt")
my make_report(file_name, user_text)
set dMes to return & return & "Would you like to open the file now?"
set opts to (display dialog "Done!" & dMes buttons {"Yes", "No Thanks"} default button 1 with icon 1 giving up after 30)
if button returned of opts is "Yes" or gave up of opts is true then
tell application "TextEdit"
activate
try
open (file_name as alias)
on error errMs
display dialog errMs buttons {"Cancel"}
end try
end tell
end if -- button is "Done"
to make_report(file_name, user_text)
try
do shell script "rm " & quoted form of POSIX path of file_name
end try
try
set fileRefr to (a reference to (open for access file_name with write permission))
write user_text to fileRefr
close access fileRefr
on error errx number errNum from badObj
try
close access fileRefr
end try
log errNum
if (errNum is equal to -48) then
do shell script "rm " & quoted form of POSIX path of file_name
my make_report()
else
display dialog "There has been an error creating the file:" & return & return & (badObj as string) & errx & return & "error number: " & errNum buttons {"Cancel"}
end if
end try
end make_report
on error_and_cancel(ms)
if gave up of (display dialog ms with icon 2 buttons {"Cancel"} default button 1 giving up after 15) then error number -128
end error_and_cancel
I like your idea a lot hendo13! (I also like automation.)
sorry for reviving this but i thought i would release a quick update to this script with thye new site it uses (which isn’t perfect)
What i would still like to see in it is error handling, and to work out a quarky bug that somtimes it doesn’t get the lyrics but grabs all the html in the rest of the page instead:P
if anyone knows how to fix this please build on this script.
display dialog "Please specify the type of lyric search:" buttons {"Song being played on iTunes", "Specify song"} default button "Song being played on iTunes"
if the button returned of the result is "Song being played on iTunes" then
tell application "iTunes"
try
set theSong to the name of the current track as text
set theArtist to the artist of the current track as text
on error
display dialog "There is no song currently playing. Please play a song and restart this application."
return -128
end try
display dialog "Lyrics Snagger will now attempt to fetch the lyrics for " & theSong & " by " & theArtist & "."
end tell
else
display dialog "What is the song called?" default answer ""
set theSong to text returned of result
display dialog "Who is the artist?" default answer ""
set theArtist to text returned of result
end if
set theSong to edittext(theSong)
set theArtist to edittext(theArtist)
set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {" "}
set theSong to text items of theSong
set theArtist to text items of theArtist
set AppleScript's text item delimiters to {"_"}
set theSong to theSong as Unicode text
set theArtist to theArtist as Unicode text
set AppleScript's text item delimiters to {""}
get "http://www.lyricwiki.org/" & theArtist & ":" & theSong
do shell script "/usr/bin/curl --user-agent '' " & quoted form of result
set theLyrics to result
set AppleScript's text item delimiters to {"<div id=\"lyric\">"}
set theLyrics to (last text item of theLyrics) as Unicode text
set AppleScript's text item delimiters to {"</div>"}
set theLyrics to first text item of theLyrics
set AppleScript's text item delimiters to {"<br/>"}
set theLyrics to text items of theLyrics
set AppleScript's text item delimiters to {return}
set theLyrics to text 2 thru -1 of (theLyrics as Unicode text)
set AppleScript's text item delimiters to ASTID
on edittext(someText)
-- Remove anything that is not alphanumeric or a space
return do shell script "echo " & quoted form of someText & " | /usr/bin/ruby -ne 'print $_.delete(\"^a-z\", \"^A-Z\", \"^0-9\", \"^ \")'"
end edittext
set user_text to theLyrics
set file_name to (choose file name with prompt "Please type a file name" default name theSong & ".txt")
if file_name as string does not end with ".txt" then set file_name to ((file_name as string) & ".txt")
my make_report(file_name, user_text)
set dMes to return & return & "Would you like to open the file now?"
set opts to (display dialog "Done!" & dMes buttons {"Yes", "No Thanks"} default button 1 with icon 1 giving up after 30)
if button returned of opts is "Yes" or gave up of opts is true then
tell application "TextEdit"
activate
try
open (file_name as alias)
on error errMs
display dialog errMs buttons {"Cancel"}
end try
end tell
end if -- button is "Done"
to make_report(file_name, user_text)
try
do shell script "rm " & quoted form of POSIX path of file_name
end try
try
set fileRefr to (a reference to (open for access file_name with write permission))
write user_text to fileRefr
close access fileRefr
on error errx number errNum from badObj
try
close access fileRefr
end try
log errNum
if (errNum is equal to -48) then
do shell script "rm " & quoted form of POSIX path of file_name
my make_report()
else
display dialog "There has been an error creating the file:" & return & return & (badObj as string) & errx & return & "error number: " & errNum buttons {"Cancel"}
end if
end try
end make_report
on error_and_cancel(ms)
if gave up of (display dialog ms with icon 2 buttons {"Cancel"} default button 1 giving up after 15) then error number -128
end error_and_cancel