Need Help With Applescript/Safari

Hello,
Im trying to make an applescript/app to get my grades from a website and put them into a email or text document.
Heres what i have so far:


tell application "Safari"
	activate
	if not (exists document 1) then make new document
	set URL of document 1 to "http://whs.rocklin.k12.ca.us/rheath/Grades/index.html"
	delay (5)
	tell application "System Events"
		keystroke tab
		keystroke tab
		keystroke "melnik"
		keystroke tab
		keystroke "9006952"
		keystroke return
		delay (5)
	end tell
end tell

(Hopefully youre not using safari when you run this script :smiley: )
What i have so far logs me in and stuff but i want to see if I can copy the part that says:
Graphic Comm. Per. 6 98.8% A
Into a text document or email

any help?

Hi,

this is difficult, because this site uses (frames and) javascript to compose the real URL of the report page.
You can see the javascript code in the terminal typing this:

curl http://whs.rocklin.k12.ca.us/rheath/Grades/frame1.html

I have no idea about javascript, but perhaps somebody can figure out how to compose the URL.
When the text is accessible, parsing it with applescript or the shell is quite easy

Try something like this, LoinSees. (I’ve added one or two delays to help reduce problems with a slower connection.)


to load_page()
	tell front document of application "Safari"
		repeat 10 times
			if name starts with "Loading" or name starts with "Untitled" then exit repeat
			delay 0.1
		end repeat
		repeat while name starts with "Loading" or name starts with "Untitled"
			delay 0.2
		end repeat
	end tell
end load_page

tell application "Safari"
	activate
	if not (exists document 1) then make document
	open location "http://whs.rocklin.k12.ca.us/rheath/Grades/index.html"
end tell

load_page()

tell application "System Events"
	keystroke tab & tab & "melnik" & tab
	delay 1
	keystroke "9006952" & return
end tell

load_page()

tell application "Safari" to set t to text of document 1

set text item delimiters to "Overall"
tell t's text item 2 to set t to paragraph 2 & "    " & paragraph 3
set text item delimiters to {""}

-- Either:
tell application "TextEdit"
	make document with properties {text:t}
	activate
end tell

-- Or:
(*
tell application "Mail"
	make outgoing message with properties {subject:"Latest Grades", content:t, visible:true}
	activate
end tell
*)


Very impressive grades, BTW. :slight_smile:

Thanks Kai, that works great. Im going to try to get it to get the grades from all my classes now :wink:

Thats only an elective class lol :P. My other classes arn’t that good… :confused:

ok im stumped :confused:
i know im a newb at this but can someone plese tell me what im doing wrong?


to load_page()
	tell front document of application "Safari"
		repeat 10 times
			if name starts with "Loading" or name starts with "Untitled" then exit repeat
			delay 0.05
		end repeat
		repeat while name starts with "Loading" or name starts with "Untitled"
			delay 0.2
		end repeat
	end tell
end load_page

tell application "Safari" to quit
tell application "TextEdit" to quit without saving

tell application "TextEdit"
	if not (exists document 1) then make document
end tell

--Get Grades of Graphic Comm

tell application "Safari"
	activate
	if not (exists document 1) then make document
	open location "http://whs.rocklin.k12.ca.us/rheath/Grades/index.html"
end tell

load_page()

tell application "System Events"
	keystroke tab & tab & "melnik" & tab
	delay 0.5
	keystroke "9006952"
	delay 0.5
	keystroke return
end tell
load_page()

tell application "Safari" to set GraphicCommGrade to text of document 1

set text item delimiters to "Overall"
tell GraphicCommGrade's text item 2 to set GraphicCommGrade to "Graphic Comm" & "    " & paragraph 3
set text item delimiters to {""}

tell application "TextEdit" to make document with properties {text:GraphicCommGrade}
tell application "System Events"
	tell process "TextEdit"
		activate document 2
		keystroke "a" using command down
		keystroke "c" using command down
		activate document 1
		keystroke "v" using command down
		keystroke return
	end tell
end tell
tell application "TextEdit" to close document 2

tell application "Safari"
	if exists document 2 then (close (document 2))
	close (document 1)
end tell


--//////////////////////////////////////////////////////--


--Get Grades of French
tell application "Safari"
	activate
	if not (exists document 1) then make document
	open location "http://whs.rocklin.k12.ca.us/rrice/index.html"
end tell

load_page()

tell application "System Events"
	keystroke tab & tab & "melnik" & tab
	delay 0.5
	keystroke "9006952"
	delay 0.5
	keystroke return
end tell

load_page()

tell application "Safari" to set FrenchGrade to text of document 1

set text item delimiters to "Overall"
tell FrenchGrade's text item 2 to set FrenchGrade to "French 1" & "    " & paragraph 3
set text item delimiters to {""}

tell application "TextEdit" to make document with properties {text:FrenchGrade}
tell application "System Events"
	tell process "TextEdit"
		activate document 2
		keystroke "a" using command down
		keystroke "c" using command down
		activate document 1
		keystroke "v" using command down
		keystroke return
	end tell
end tell
tell application "TextEdit" to close document 2

tell application "Safari"
	if exists document 2 then (close (document 2))
	close (document 1)
end tell


--//////////////////////////////////////////////////////--


--Get Grades of Language Arts
tell application "Safari"
	activate
	if not (exists document 1) then make document
	open location "http://whs.rocklin.k12.ca.us/tsamson/Grades/index.html"
end tell

load_page()

tell application "System Events"
	keystroke tab & tab & "melnik" & tab
	delay 0.5
	keystroke "9006952"
	delay 0.5
	keystroke return
end tell

load_page()

tell application "Safari" to set lagrade to text of document 1

set text item delimiters to "Overall"
tell lagrade's text item 2 to set la to "Language Arts" & "    " & paragraph 3
set text item delimiters to {""}

tell application "TextEdit" to make document with properties {text:lagrade}
tell application "System Events"
	tell process "TextEdit"
		activate document 2
		keystroke "a" using command down
		keystroke "c" using command down
		activate document 1
		keystroke "v" using command down
		keystroke return
	end tell
end tell
tell application "TextEdit" to close document 2

tell application "Safari"
	if exists document 2 then (close (document 2))
	close (document 1)
end tell


--//////////////////////////////////////////////////////--


--Get Grades of Algebra II
tell application "Safari"
	activate
	if not (exists document 1) then make document
	open location "http://whs.rocklin.k12.ca.us/vlovitt/Grades/index.html"
end tell

load_page()

tell application "System Events"
	key code 48 & 49 & 125 & 125 & 49 & 48
	keystroke "melnik" & tab
	delay 0.5
	keystroke "9006952"
	delay 0.5
	keystroke return
end tell

load_page()

tell application "Safari" to set algiigrade to text of document 1

set text item delimiters to "Overall"
tell algiigrade's text item 2 to set algiigrade to "Algebra II" & "    " & paragraph 3
set text item delimiters to {""}

tell application "TextEdit" to make document with properties {text:algiigrade}
tell application "System Events"
	tell process "TextEdit"
		activate document 2
		keystroke "a" using command down
		keystroke "c" using command down
		activate document 1
		keystroke "v" using command down
		keystroke return
	end tell
end tell
tell application "TextEdit" to close document 2

tell application "Safari"
	if exists document 2 then (close (document 2))
	close (document 1)
end tell

I was able to get it to open it all in seperate text files but im trying to put them into 1…
:o wow this can get tiring…

Instead of 4 repetitions of the same code it’s clearer to use a subroutine and a repeat block
and then put the result into the textfile.
The forth URL unfortunately doesn’t work


set URLlist to {"http://whs.rocklin.k12.ca.us/rheath/Grades/index.html", "http://whs.rocklin.k12.ca.us/rrice/index.html", "http://whs.rocklin.k12.ca.us/tsamson/Grades/index.html", "http://whs.rocklin.k12.ca.us/vlovitt/Grades/index.html"}
set SubjectList to {"Graphic Comm", "French 1", "Language Art", "Algebra II"}

set Gradetext to ""
repeat with i from 1 to count URLlist
	set Gradetext to Gradetext & get_grade_data(item i of URLlist, item i of SubjectList) & return
end repeat

tell application "TextEdit"
	make document with properties {text:Gradetext}
	activate
end tell

to load_page()
	tell front document of application "Safari"
		repeat 10 times
			if name starts with "Loading" or name starts with "Untitled" then exit repeat
			delay 0.1
		end repeat
		repeat while name starts with "Loading" or name starts with "Untitled"
			delay 0.2
		end repeat
	end tell
end load_page

on get_grade_data(theURL, Subject)
	tell application "Safari"
		activate
		if not (exists document 1) then make document
		open location theURL
	end tell
	
	load_page()
	
	tell application "System Events"
		keystroke tab & tab & "melnik" & tab
		delay 1
		keystroke "9006952" & return
	end tell
	
	load_page()
	
	tell application "Safari" to set GradeData to text of document 1
	
	set text item delimiters to "Overall"
	tell GradeData's text item 2 to set GradeData to Subject & "   " & paragraph 3
	set text item delimiters to {""}
	return GradeData
end get_grade_data

thanks stephan,
but then how would you change the class period (Math Class)?
Also how would you put the grades in the text document?
Im a little confused… :confused:

o.k. I see, for the two special cases I changed the get_grade_data handler

on get_grade_data(theURL, Subject)
	tell application "Safari"
		activate
		if not (exists document 1) then make document
		open location theURL
	end tell
	
	load_page()
	
	tell application "System Events"
		if Subject is "Algebra II" then
			key code 48 & 49 & 125 & 125 & 49 & 48
		else
			keystroke tab & tab
		end if
		keystroke "melnik" & tab
		delay 1
		keystroke "9006952" & return
	end tell
	
	load_page()
	
	tell application "Safari" to set GradeData to text of document 1
	if Subject is "Language Art" then return GradeData
	set text item delimiters to "Overall"
	tell GradeData's text item 2 to set GradeData to Subject & "   " & paragraph 3
	set text item delimiters to {""}
	return GradeData
end get_grade_data

Thanks Stephen! :smiley:

Well, putting the grades into a text doc was a little too confusing, so i scratched that idea…
Anyways, heres how it turned out:


set URLlist to {"http://whs.rocklin.k12.ca.us/rheath/Grades/index.html", "http://whs.rocklin.k12.ca.us/rrice/index.html", "http://whs.rocklin.k12.ca.us/tsamson/Grades/index.html", "http://whs.rocklin.k12.ca.us/vlovitt/Grades/index.html", "http://whs.rocklin.k12.ca.us/ckuehn/ckuehngrades/index.html", "http://whs.rocklin.k12.ca.us/smain/index.html"}
set SubjectList to {"Graphic Comm", "French 1", "Language Art", "Algebra II", "Carissa Kuehn", "Suzie Main"}

set Gradetext to ""
repeat with i from 1 to count URLlist
	set Gradetext to Gradetext & open_grades(item i of URLlist, item i of SubjectList) & return
end repeat

to load_page()
	tell front document of application "Safari"
		repeat 10 times
			if name starts with "Loading" or name starts with "Untitled" then exit repeat
			delay 0.1
		end repeat
		repeat while name starts with "Loading" or name starts with "Untitled"
			delay 0.2
		end repeat
	end tell
end load_page

on open_grades(theURL, Subject)
	tell application "Safari"
		activate
		if not (exists document 1) then make document
		open location theURL
	end tell
	
	load_page()
	
	tell application "System Events"
		if Subject is "Algebra II" then
			key code 48 & 49 & 125 & 125 & 49 & 48
		else
			keystroke tab & tab
		end if
		keystroke "melnik" & tab
		delay 1
		keystroke "9006952" & return
	end tell
	
	load_page()
	
end open_grades

I guess this is for anyone who has a similar system at their schools… if ppl here still go to school :slight_smile:

–==++:D Special Thanks To StephanK and Kai :D++==–

Thanks for completing the thread with your final solution. Folks who’ve helped like to see that.

And… No there’s no way to include an image of any sort on this forum - they’re turned off to save server space.

You can put something like this: I’m a Button Developer for Apple XCode where the contents look like this:

[color=red][b]I'm a[/b][/color] [url=http://mattgemmell.com/images/button_developer_for_hire.png]Button Developer for Apple XCode[/url]