Scripting Safari: passing control between a script and a Web page

Hi. I’m new to AppleScript and I’m wondering if anyone can tell me if it’s capable of doing the following:

I want to have a script that opens a Safari window and loads a given Web page. (So far so good.) Now I want to “insert” a JavaScript object into that Web page, and pause the AppleScript. I want the Web page, in it’s own time, to then call a method on this global object to tell the AppleScript to resume.

This may help as a starting point:

http://www.apple.com/applescript/linktrigger/index.html

Peter B.


do you need it to pause.
if you use a script like the example below, it will wait until each action is completed.

tell application "Safari"
	set t to source of document 1
	do shell script "echo " & quoted form of t & " > test1.html"
	set g to do shell script "cat  test1.html " & quoted form of t & " | sed 's/<\\/head>/<\\/head><javascript> my java script <\\/javascript>/g'"
	log g
	do shell script "echo " & quoted form of g & " > test2.html"
	
	open "/test2.html"
	say "done"
end tell




The <\/head> my java script <\/javascript> gets inserted next the the tag and then the script opens the new file.
If you look at the source of the new file you will see the my java script

Thanks for the link Peter. I’m looking to launch Safari from a script, and have the script tell Safari which pages to load. The doc you link to has it kinda the opposite way around (Safari launching the script (as a handler)).

Mark: yeah, inserting the script isn’t so much my problem (see the “so far so good” comment above). It’s the passing control back and forth between the script and Safari. I.e. making the script pause and wait for the Web page to do its stuff before it (the Web page) tells the script to continue.

On further reading of the Apple docs, I think probably AppleScript isn’t the right tool for the job. I probably need to write a basic cocoa app that wraps Web Kit.

Thanks guys.