ive written a fairly simple script and it works perfectly if i have the “event log” tab on script editor selected. if i select the “result” tab, it gives me an error. also, once the script has run (under the event log tab) the proper result is shown in the result pane. any reason this would happen? im a total noob. thanks
aaron
Model: macbook pro
Browser: Safari 523.15
Operating System: Mac OS X (10.5)
on run
set contact to {}
set namePattern to "<td\\b[^>]*>Contact:</td>$[\\r\\n]*^<td\\b[^>]*>[\\n\\r]*([^<]*)"
set addressPattern to "(<td\\b[^>]*>Address:</td><td\\b[^>]*>[\\n\\r]*)((^[^<]*$)[\\n\\r]*)+"
set businesses to {tons of stuff}
set myLib to load script file ((path to desktop as string) & "web_page_loading.scpt")
my get_addresses()
contact
end run
on get_addresses()
tell application "Safari"
set counter to 430
repeat 50 times
set URL of document 1 to "about:blank"
set URL of document 1 to "http://www.memberservicecenter.org/irmweb/wc.dll/txcopcoc?id=txcopcoc&doc=rol/rol1/listing&kn=" & item 1 of item counter of my businesses
tell my myLib to get_page_loading()
set mySource to the source of document 1 as string
set mySource to change "<br>" into "" in mySource with regexp
--error occurs here
set trashs to find text my addressPattern in mySource using "\\1" with regexp, all occurrences and string result
set taddress to find text my addressPattern in mySource with regexp, all occurrences and string result
set taddress to change trashs into "" in taddress
set taddress to change "^$[\\r\\n]*" into "" in taddress with regexp
set taddress to change "$[\\r\\n]*^$" into "" in taddress with regexp
set taddress to change " " into " " in taddress
set tname to {""}
try
set tname to find text my namePattern in mySource using "\\1" with regexp, all occurrences and string result
end try
set tempCollection to {}
copy item 1 of taddress to the end of tempCollection
copy item 1 of tname to the end of tempCollection
copy item 1 of item counter of my businesses to the end of tempCollection
copy tempCollection to the end of my contact
set counter to counter + 1
if counter is (count of my businesses) + 1 then exit repeat
end repeat
end tell
end get_addresses
here is the web_page_loading.scpt
on get_page_loading()
set theDelay to 10 -- the time in seconds the script will wait to let a web page load
set numTries to 3 -- the number of stop/reload cycles before giving up
set myCounter to 0
set finished to false
repeat until finished is true
set myCounter to myCounter + 1
set my_delay to 0.25
set startTime to current date
set endTime to startTime + theDelay
set web_page_is_loaded to false
delay my_delay
tell application "Safari"
repeat until web_page_is_loaded is true
if endTime is greater than (current date) then -- only wait theDelay seconds for a web page to load otherwise stop the page loading and try to reload again
if name of window 1 contains "Loading" or name of window 1 contains "Untitled" or name of window 1 contains "Failed to open page" then
delay my_delay
else
set web_page_is_loaded to true
set finished to true
delay my_delay
end if
else
tell application "System Events"
tell process "Safari"
if myCounter is numTries then -- give up if the page doesn't load after numTries stop/reload cycles
keystroke "." using command down -- stop the page
delay my_delay
tell application "Finder"
activate
display dialog "The web page will not load!"
end tell
set connected to true -- a global variable which will stop this script if there's a problem
set web_page_is_loaded to true
set finished to true
tell application "Safari" to activate
else
keystroke "." using command down -- stop the page
delay my_delay
keystroke "r" using command down -- reload the page
delay my_delay
set web_page_is_loaded to true
end if
end tell
end tell
end if
end repeat
end tell
end repeat
end get_page_loading
im sure the script isnt very good… but it works for what i need. well, sort of. except when i press result!
Hi, aaron.
You don’t say what error you’re getting. If it’s “No match”, I’d guess there’s a timing problem. With the Event Log active, Script Editor has to take time to format and display each event, which means the script runs more slowly, allowing more time for the Web pages to complete. Without the Event Log, it’s probable that your “web_page_loading” script is handing back to the main script just a bit too soon.
Before handing back, you could check that the page is fully loaded with a bit of JavaScript:
tell application "Safari"
set readyState to (do JavaScript "document.readyState" in front document)
end tell
If readyState is “complete” (or possibly “loaded”), it should then be OK for the main script to check the document’s source.