write a webpage in safari from an applescript variable

Hi.

This must be pretty easy but I can’t find a way to do it.

I have a complete webpage in an applescript variable (mypage).

No, I want to open safari and pass it the page content through the variable mypage.
What is the fastest way to do it in your opinion? I thought I’d use the “do javascript” command and then window.write it, but I can’t get it to work.

Thanks for any help!

I don’t know about javascript, but I would write the file to disk as a text file with a “.html” extension and then open that file with safari.

yes, this is of course doable, but I think It would be rather slow and disk operations are not very fast.

I was hoping to find a way which would not involve file reading/writing…

Hi,

I’m afraid there is no other way.
The document properties text and source in Safari are read only

How about this?

set jsCode to "document.write('<h2>Writing with javascript</h2>')"
tell application "Safari"
	do JavaScript jsCode in front document
end tell

yes, this work, but I I try to replace ‘

Writing with javascript

’ with a string containing a full webpage in html, I get nothing…

Show us your code. Whatever you want to write has to be in quotes so most likely there’s a problem with quotation marks in your html code interfering with the javascript command.

This works:

set mypage to "<h2>Writing with javascript</h2>"

set jsCode to "document.write('" & mypage & "')"

tell application "Safari"
	do JavaScript jsCode in front document
end tell

but if I have a full webpage in mypage, it does not. could it be the string is too long? can I brake it up into smaller pieces?

I have had some success putting the html on one line before
feeding it to JavaScript.

Regards,

Craig

NOPE. I have tried stripping CRs, LFs, and also removing all the tags, but it still does not work. :frowning:

Thanks for the reply anyway.

You still haven’t showed us your code… so how can we help you???

Sorry, you are right. I am sorry it’s a bit of a dirty code.

To get it to work properly you have to replace
USERNAME%40YOURDOMAIN.COM
YOURPASSWORD
with your login information at www.garzantilinguistica.it. However, even if you do not register, you should still get a webpage as result.

The idea is to select an english word in Microsoft Word and then to look it up at the garzanti’s website. This now works by saving the webpage to disk and then by opening it with safari, the last commented code (document.write) part does not work.

Here it is…


tell application "Microsoft Word"
	activate
	tell application "System Events" to keystroke "c" using command down
	set cercaparola to (the clipboard) as text
end tell

try
	tell application "Finder"
		set delfile to (path to startup disk as Unicode text) & "outpage.html"
		delete delfile
	end tell
end try


set basewebsite to " [url=http://www.garzantilinguistica.it/interna_eng.html]http://www.garzantilinguistica.it/interna_eng.html"[/url]
set primohostname to " \"s_dizionario=2&s_parola="
set secondohostname to "&x=27&y=12\""
--set cercaparola to the clipboard
set hostname to primohostname & cercaparola & secondohostname

set mypage to do shell script "curl   -silent -o 2>&1 /dev/null -b /Users/Mic/garzanticookie -d \"makelogin=1&login=USERNAME%40YOURDOMAIN.COM&password=YOURPASSWORD&x=14&y=10\" http://www.garzantilinguistica.it/index.html ;  curl  -b /Users/Mic/garzanticookie -d " & hostname & basewebsite
--display dialog mypage
-- aggiunge il <base tag> alla pagina

set mypage to mypage as text

set pippo to get offset of "<head>" in mypage
set fine to the number of characters of mypage


set primaparte to text 1 thru (pippo + 6) of mypage as text
set secondaparte to text (pippo + 6) thru fine of mypage as text

set mypage to primaparte & "
<base href=\"http://www.garzantilinguistica.it/\" />
" & secondaparte as text


--set the clipboard to mypage

set outstringspuria to mypage
--set outfileid to choose file name with prompt "Output file:" default name "My New File"

-- funziona solo se outpage.html non esiste!
set outfileid to (path to startup disk as Unicode text) & "outpage.html"
set ll to open for access outfileid with write permission

write outstringspuria to ll
close access ll

tell application "Safari"
	open outfileid
end tell

(*

--set mypage to "<h2>Writing with javascript</h2>" as text

set jsCode to "document.write('" & mypage & "')"
tell application "Safari"
	do JavaScript jsCode in front document
end tell

display dialog jsCode

*)

I think writing it out to a file is probably your best bet. I couldn’t get it to work properly. I tried feeding one line at a time and got some results but not too good. I used apple.com as a test website. I tried a couple other things too but no luck. I’ll let you know if I think of something else.

set mypage to do shell script "curl http://www.apple.com" --without altering line endings
set theParas to paragraphs of mypage

repeat with aPara in theParas
	set jsCode to "document.write('" & aPara & "')"
	tell application "Safari"
		do JavaScript jsCode in front document
	end tell
end repeat

Yes… maybe the JS is confused by the brackets in the page string or something?

Anyway the page from your code looks pretty good. Probably the webpage is just missing some stilesheets or some frames…

Thanks.

regulus6633, your system DOES work with my code. I now get the full page, and since I also replace the “base href” in the page code I also get stilesheets and pictures OK.

However, the method is quite slow. I think the saving the webpage to disk and reloading it is still faster, but at least your system works.
I’ll keep working on it and see if I can spare the loop somehow.

Thanks!

Try this.


set mypage to do shell script "curl [url=http://www.apple.com/startpage/]http://www.apple.com/startpage/"[/url] 

set mypage to my searchANDreplace(mypage, "
", "")
set mypage to my searchANDreplace(mypage, "
", "")
set mypage to my searchANDreplace(mypage, "
", "")
set mypage to my searchANDreplace(mypage, tab, "")

set mypage to my searchANDreplace(mypage, "'", "\\'")

set mypage to my searchANDreplace(mypage, "\"", "\\\"")


set jsCode to "document.write('" & mypage & "');"
tell application "Safari"
	do JavaScript jsCode in front document
end tell



on searchANDreplace(stringTOsearch, searchitem, replaceitem)
	set oldDelim to AppleScript's text item delimiters
	set AppleScript's text item delimiters to searchitem
	set thetextitems to text items of stringTOsearch
	set AppleScript's text item delimiters to replaceitem
	set stringTOsearch to thetextitems as string
	set AppleScript's text item delimiters to oldDelim
	return stringTOsearch
end searchANDreplace

Craig, it does not work here. Is it working for you?

Yes, it is working for me.

One thing I noticed. When I use the “Open this Scriplet in your Editor:” the
code does not work until I add a space or return somewhere in the script
and then compile again.

If I copy the script from the web page and then paste it in my editor,
then it works.

Never seen this before and not sure what is causing it.

Give it a try and let me know your results.

Cheers!

Craig

Nope, it still does not work! I can see it does its job on the event window, but nothing happens on safari…

Sorry, do not know why it is not working for you.

I know you do not want to write to file and then open but
it does appear to be the most reliable solution. :slight_smile: