If I have an entire web page as a string variable, how can I get that to render in a WebView? I can work out how to load a URL.
Also, if I have image resources in the app bundle, how do I reference them from the HTML (ie )?
Thanks
If I have an entire web page as a string variable, how can I get that to render in a WebView? I can work out how to load a URL.
Also, if I have image resources in the app bundle, how do I reference them from the HTML (ie )?
Thanks
You’re probably best off writing the string to a file in the temporary items folder:
and then loading the resultant file’s URL.
Jon
[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]
I’m doing something like that at the moment, but it just seems such a cop-out. Thanks for the feedback.
– Clive
After some searching around, I finally found out how to do this without writing to a temp file. Here is the code that worked for me…
set theHTML to ">>>INSERT VALID HTML HERE<<<"
(* Use the following line to get a valid NSUrl by specifying a file url manually *)
--set baseURL to call method "URLWithString:" of class "NSURL" with parameter "file:///Users/jed/Desktop/Test/index.html"
(* Use the following line to get a valid NSUrl from a posix file path *)
set baseURL to call method "fileURLWithPath:" of class "NSURL" with parameter "/Users/jed/Desktop/Test/index.html"
(* Get a reference to the web view's main frame *)
set mainFrame to call method "mainFrame" of object (view "myWebview" of window "window")
(* Set the html source of the main frame to our html string *)
call method "loadHTMLString:baseURL:" of mainFrame with parameters {theHTML, baseURL}
The key to making this work, is in specifying a good base url in the variable “baseURL”. Contrary to what you would expect, you DO NOT specify the directory you wish to use for the base directory. You must specify a VALID path to AN EXISTING FILE within the base directory you wish to use, and it makes the reference it needs from that url to establish the relative links.
So if you have the line in your html string, it will look for your donkey image at the local path /Users/jed/Desktop/Test/media/donkey.jpg. Yeah, this seems stupid to me too… but it works. And, remember that pointing the base url string at a valid file is mandatory, so be sure to write some error-checking into your code because the loss of that file will cause this code to do nothing. Note, too, that the file you reference does NOT have to be an html file, it can be a plain text file, an image, whatever. There just must be something there other than a folder to reference. :rolleyes:
Good luck…
j
Jobu, thanks a lot, that’s a lot of good information!
I have one more little challenge, that I can’t really work out.
Originally I wrote a droplet to examine some files and produce a HTML report that displayed in Safari. This works well, but it was always my intention to display the information directly in the droplet, via a WebView - but I couldn’t get it to work.
So, I then started from scratch and just wrote an app to display the HTML, which with your tips now I think I can get together the code to display a variable rather than read a HTML file.
But, I’ve been unable to put the two applications together - I’m unable to get the window with the WebView to open in the droplet (let alone get anything into it) nor get the file examination to run in the HTML viewer.
The droplet has a main routine something like:
on open theFiles
…
–need something to open the window at the end???
end open
And the HTML viewer is something like:
on awake from nib
…
end awake
The only minimal thing I’ve been able to do so far is have the HTML viewer launch on dropping files onto it, by editing the info.plist file. But putting an “on open” routine in the AppleScript gets no result, even with something as simple as:
on open theFiles
beep
end open
There’s also no other obvious way for me to get information about the files that were dropped on the application (I’m sure I’m missing the point here somewhere).
If I’m making sense, do you have any ideas how to do what I’m outlining?
Thanks
– Clive