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.
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.
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?
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
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.
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
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.