Hi all, I am just getting into using applescript and decided to put together a little script that I can fire from quicksilver to validate a page in safari. I put in some growl notifications for kicks to see how it all works.
Here is what I have:
tell application "GrowlHelperApp"
set the allNotificationsList to ¬
{"When Sent To Validator", "If Blank Page"}
set the enabledNotificationsList to ¬
{"When Sent To Validator", "If Blank Page"}
register as application ¬
"W3C Markup Validation Service" all notifications allNotificationsList ¬
default notifications enabledNotificationsList ¬
icon of application "Script Editor"
end tell
tell application "Safari"
set frontURL to the URL of the front document
end tell
try
result
on error
tell application "GrowlHelperApp"
notify with name ¬
"If Blank Page" title ¬
"W3C Markup Validation Service" description ¬
"Cannot Validate Blank Page" application name "W3C Markup Validation Service"
end tell
return
end try
set newURL to "http://validator.w3.org/check?uri=" & frontURL
tell application "Safari" to activate
tell application "System Events" to keystroke "t" using command down
tell application "Safari" to set front document's URL to newURL
tell application "Safari"
set the URL of the front document to newURL
end tell
tell application "GrowlHelperApp"
notify with name ¬
"When Sent To Validator" title ¬
"W3C Markup Validation Service" description ¬
"Sent To Validator" application name "W3C Markup Validation Service"
end tell
While it does essentially work I am having a couple problems and would like to make a couple changes:
When running the code from script editor it opens the page a new tab every time, from quicksilver sometimes it opens but usually it doesn’t. Any ideas?
I have used this way of opening a new tab because on my macbook using the following code won’t work I get “System Events got an error: NSReceiverEvaluationScriptError: 4” but on my older powermac (ppc) it works just fine. Any clue what that is all about?
tell application "Safari" to activate
tell application "System Events"
tell process "Safari"
click menu item "New Tab" of menu "File" of menu bar 1
end tell
end tell
Lastly I was hoping to fire a growl notification once safari has finished loading the page instead of when you first run the script. I have tried using apples javascript method but couldn’t make much of it. Any guidance would be helpful.
Thanks so much for taking the time to look at my script. I am really enjoying learning this stuff!
this could be. Scripts unfortunately do behave different when running form several places.
The best way is to force opening a new tab as you did
GUI scripting is very sensitive and just choosing menu items can cause problems (timing or international localization)
If there is a shortcut alternative, it should be preferred always
Here is yout script with a modified javascript page_load routine. It’s trying 3 times to reload the page.
Maybe you can play a bit with the delay lines. The notification contains now the result of the validation.
But I don’t know Growl very well: If the text to be displayed is too long, the end will be cut
tell application "GrowlHelperApp"
set the allNotificationsList to ¬
{"When Sent To Validator", "If Blank Page"}
set the enabledNotificationsList to ¬
{"When Sent To Validator", "If Blank Page"}
register as application ¬
"W3C Markup Validation Service" all notifications allNotificationsList ¬
default notifications enabledNotificationsList ¬
icon of application "Script Editor"
end tell
try
tell application "Safari" to set frontURL to the URL of the front document
on error
Growl_Notify("If Blank Page", "W3C Markup Validation Service", "Cannot Validate Blank Page")
return
end try
set newURL to "http://validator.w3.org/check?uri=" & frontURL
tell application "Safari" to activate
tell application "System Events" to keystroke "t" using command down
tell application "Safari" to open location newURL
if page_loaded(30, newURL) then
tell application "Safari" to set t to text of document 1
set theresult to do shell script "echo " & quoted form of t & " | grep 'This page'"
Growl_Notify("When Sent To Validator", theresult, "Sent To Validator")
else
Growl_Notify("When Sent To Validator", "Could not open page", "Sent To Validator")
end if
on page_loaded(timeout_value, theURL)
delay 2
repeat with x from 1 to 3
repeat with i from 1 to the timeout_value
tell application "Safari"
if (do JavaScript "document.readyState" in document 1) is "complete" then
if name of window 1 is "Untitled" or name of window 1 contains "Failed" then
set URL of document 1 to theURL
delay 5
exit repeat
end if
return true
else if i is the timeout_value then
return false
else
delay 1
end if
end tell
end repeat
end repeat
return false
end page_loaded
on Growl_Notify(n, t, d)
tell application "GrowlHelperApp"
notify with name n title t description d application name "W3C Markup Validation Service"
end tell
end Growl_Notify