Re: Web Renderer (URL -> image)
Re: Web Renderer (URL -> image)
- Subject: Re: Web Renderer (URL -> image)
- From: Emmanuel LEVY <email@hidden>
- Date: Fri, 18 Sep 2015 10:22:50 +0200
In case it helps anyone, in Smile you can open a web page with:
----
set www to make new web window with properties {path name: "http://apple.com"}
----
you can resize the window with:
----
set properties of www to {width: 1024, height: 840}
----
you can save it as a bitmap image with:
----
take screenshot of www into "~/Desktop/smile-is-also-good-as-a-web-browser.jpg"
----
and you can save it as a vector image with:
----
save www in "~/Desktop/smile-is-also-good-as-a-web-browser.pdf"
----
Best to all!
Emmanuel
On Sep 16, 2015, at 5:28 AM, Takaaki Naganoya wrote:
> Hi All,
>
> 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.
> The Page header and footer are quite different (not drawn).
>
> Is there a nice idea?
>
> <AppleScript>
> -- 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"
> set aURL to "http://www.apple.com/us/shop/browse/home/specialdeals/mac"
> --set aURL to "http://piyocast.com/as"
>
>
> --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
>
> </AppleScript>
>
> --
> Takaaki Naganoya
> email@hidden
> http://piyocast.com/as/
>
>
>
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> AppleScript-Users mailing list (email@hidden)
> Help/Unsubscribe/Update your Subscription:
> Archives: http://lists.apple.com/archives/applescript-users
>
> This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden