I’m making an AppleScript to render a Web contents to a image. This works on Script Editor (Control-Command-R required).
But this script’s rendered image is not same as Safari.
-- Created 2015-09-15 by Takaaki Naganoya
-- 2015 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "WebKit"
use framework "Quartz"
use framework "AppKit"
property loadDone : false
property theWebView : missing value
property userAgentName : "Version/9.0 Safari/601.1.56"
set aOutPath to "~/Desktop/outIMG_1.png"
--Check If this script runs in foreground
if not (current application's NSThread's isMainThread()) as boolean then
display alert "This script must be run from the main thread (Command-Control-R in Script Editor)." buttons {"Cancel"} as critical
error number -128
end if
set aPath to current application's NSString's stringWithString:aOutPath
set bPath to aPath's stringByExpandingTildeInPath()
--URL Validation check
set aRes to validateURL(aURL)
if aRes = false then return
set aTitle to getURLandRender(aURL)
--Specify Render to image view (HTML world)
set aTargetView to (my theWebView)'s mainFrame()'s frameView()'s documentView()
--Get Web Contents Size
set targRect to aTargetView's |bounds|()
--Make Window begin--????? Really Needed ???? Very doubtful
set aWidth to width of |size| of targRect
set aHeight to height of |size| of targRect
set aWin to current application's NSWindow's alloc()'s init()
aWin's setContentSize:{aWidth + 40, aHeight + 20}
aWin's setContentView:(my theWebView)
aWin's setOpaque:false
aWin's |center|()
--Make Window end
set aData to aTargetView's dataWithPDFInsideRect:targRect
set aImage to current application's NSImage's alloc()'s initWithData:aData
set bData to aImage's TIFFRepresentation()
set aBitmap to current application's NSBitmapImageRep's imageRepWithData:bData
--Write To File
set myNewImageData to (aBitmap's representationUsingType:(current application's NSPNGFileType) |properties|:(missing value))
set aRes to (myNewImageData's writeToFile:bPath atomically:true) as boolean
return aRes
--Download the URL page to WebView and get page title
on getURLandRender(aURL)
set my loadDone to false
set my theWebView to missing value
openURL(aURL)
set waitLoop to 1000 * 60 --60 seconds
set hitF to false
repeat waitLoop times
if my loadDone = true then
set hitF to true
exit repeat
end if
current application's NSThread's sleepForTimeInterval:("0.001" as real) --delay 0.001
end repeat
if hitF = false then return
set jsText to "document.title"
set x to ((my theWebView)'s stringByEvaluatingJavaScriptFromString:jsText) as text
return x
end getURLandRender
--Down Load URL contents to WebView
on openURL(aURL)
set noter1 to current application's NSNotificationCenter's defaultCenter()
set (my theWebView) to current application's WebView's alloc()'s init()
(my theWebView)'s setApplicationNameForUserAgent:userAgentName
(my theWebView)'s setMediaStyle:"screen"
(my theWebView)'s setDrawsBackground:true --THIS SEEMS NOT TO WORK
--(my theWebView)'s backGroundColor:(current application's NSColor's whiteColor())--Mistake
--(my theWebView)'s setShouldUpdateWhileOffscreen:true
noter1's addObserver:me selector:"webLoaded:" |name|:(current application's WebViewProgressFinishedNotification) object:(my theWebView)
(my theWebView)'s setMainFrameURL:aURL
end openURL
--Web View's Event Handler:load finished
on webLoaded:aNotification
set my loadDone to true
end webLoaded:
--URL Validation Check
on validateURL(anURL as text)
set regEx1 to current application's NSString's stringWithString:"((https|http)://)((
\\w|-)+)(([.]|[/])((
\\w|-)+))+"
set predicate1 to current application's NSPredicate's predicateWithFormat_("SELF MATCHES %@", regEx1)
set aPredRes1 to (predicate1's evaluateWithObject:anURL) as boolean
return aPredRes1
end validateURL