Get info from Safari

Well, I’m new to scripting, I’ve never done any programming, and I started 2 days ago.

So, ideally what I want to do is tell safari to open a link from a google page, and then copy the contents of that page, and set the results to the lyrics of a song on itunes.

Model: Macbook
AppleScript: 2.2
Browser: Safari 525.18
Operating System: Mac OS X (10.5)

Welcome Vampire,

What do you have so far…


display dialog ("Look up the lyrics to the selected song?")


property baseURL : "http://www.google.com/search?client=safari&rls=en-us&q="
property endURL : "lyrics&ie=UTF-8&oe=UTF-8"
property A : " "
tell application "iTunes"
	if selection is not {} then
		set sel to selection
		if (length of sel) is 1 then
			set alb to (get name of (item 1 of sel))
			if alb contains "rack" then
				set alb to ""
			end if
			set title to (get artist of (item 1 of sel))
			if alb is "" then
				display dialog "The selected track's Song tag is blank." default button 1 with icon 2 giving up after 15
			end if
		else
			display dialog "Select a single track..." buttons {"Cancel"} default button 1 with icon 2 giving up after 15
			return
		end if
	else
		display dialog "Select a single track..." buttons {"Cancel"} default button 1 with icon 2 giving up after 15
		return
	end if
end tell

tell application "Finder" to open location (baseURL & title & A & alb & A & endURL) as text

set BJJF to display dialog ("Copy the text entered below to the Info section of iTunes") default answer ""
set AAJJ to text returned of BJJF
tell application "iTunes"
	set lyrics of selection to AAJJ
end tell

This works, but I want to automate finding, and setting the lyrics.

If anything is wrong in that please tell me.

Model: Macbook
AppleScript: 2.2
Browser: Safari 525.18
Operating System: Mac OS X (10.5)

This will most likely need more work, ( I will leave that to yuo :wink: )
but it works in my tests.
Changed the search to look at www.lyricsdir.com
There is a sub routine to flatten Upper case to lower case
And a sub routine to remove or replace characters the site may have a problem with.
The latter you may need to add too.
Here are the two I added.
set the ex_name to replace_chars(ex_name, “'”, “”)
set the ex_name to replace_chars(ex_name, “ö”, “o”)

Both Subroutines in use can be found at Apple

The first one removes “'
The second one replaces “ö” with “o
As in björk Theres More To Life Than This

global new_text
display dialog ("Look up the lyrics to the selected song?")


property baseURL : "http://www.lyricsdir.com/"
property endURL : "lyrics.html"
property A : " "
tell application "iTunes"
	if selection is not {} then
		set sel to selection
		if (length of sel) is 1 then
			set alb to (get name of (item 1 of sel))
			if alb contains "rack" then
				set alb to ""
			end if
			set title to (get artist of (item 1 of sel))
			if alb is "" then
				display dialog "The selected track's Song tag is blank." default button 1 with icon 2 giving up after 15
			end if
		else
			display dialog "Select a single track..." buttons {"Cancel"} default button 1 with icon 2 giving up after 15
			return
		end if
	else
		display dialog "Select a single track..." buttons {"Cancel"} default button 1 with icon 2 giving up after 15
		return
	end if
end tell
set ex_name to title & A & alb
set oldDelims to AppleScript's text item delimiters -- get normal dilm
set {TID, text item delimiters} to {text item delimiters, space}
set ex_name to text items of ex_name
set AppleScript's text item delimiters to oldDelims
repeat with i from 1 to number of items in ex_name
	set item i of ex_name to item i of ex_name & "-"
end repeat
set ex_name to ex_name as string
set this_case to 0
set ex_name to change_case(ex_name, this_case)

set the ex_name to replace_chars(ex_name, "'", "")
set the ex_name to replace_chars(ex_name, "ö", "o")

tell application "Finder" to open location (baseURL & ex_name & endURL) as text
my page_loaded(10)
tell application "Safari"
	set AAJJ to (paragraphs 5 thru -5 of (text of document 1)) as string
end tell
AAJJ
tell application "iTunes"
	if AAJJ does not contain "

Popular
" then
		
		set lyrics of selection to AAJJ
	else
		display dialog "Could not get the lyrics for the selected track" default button 1 giving up after 15
	end if
end tell
on page_loaded(timeout_value) -- in seconds
	delay 2
	repeat with i from 1 to timeout_value
		tell application "Safari"
			if (do JavaScript "document.readyState" in document 1) is "complete" then
				return true
			else if i is timeout_value then
				return false
			else
				delay 1
			end if
		end tell
	end repeat
	return false
end page_loaded
on replace_chars(this_text, search_string, replacement_string)
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to ""
	return this_text
end replace_chars
on change_case(this_text, this_case)
	if this_case is 0 then
		set the comparison_string to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
		set the source_string to "abcdefghijklmnopqrstuvwxyz'"
	else
		set the comparison_string to "abcdefghijklmnopqrstuvwxyz"
		set the source_string to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	end if
	set the new_text to ""
	repeat with this_char in this_text
		set x to the offset of this_char in the comparison_string
		if x is not 0 then
			set the new_text to (the new_text & character x of the source_string) as string
		else
			set the new_text to (the new_text & this_char) as string
		end if
	end repeat
	log new_text
	return the new_text
end change_case

Haha Perfect.

That’s amazing how you get the lyrics, but not the surrounding text.
I’m going to have to figure out how you do that.
well, you’ve given me something to do for the next few years. Haha

Thank you.

What is a delimiter?

off subject (I think)
what is a handler?