指定URLのWebコンテンツをWebViewに読み込んでレンダリングし、1枚ものの大きなPNG画像に保存するAppleScriptです。
資料作成時にWebサイトの内容を利用する場合、こうした1枚ものの大きな画像にレンダリングすることがよくあります(Webサイトのデザインや構成変更の提案時など)。PDFとしてレンダリングする場合が多いですが、サイズ軽減のためにPNGなどの画像でレンダリングすることもあり、その場合のために作成したものです。
ただ、実際に作っておいてナニですが、PDFで保存する場合のほうが多く、PNGでレンダリングするケースはまれです。
# そのままではmacOS 10.13以降の環境で動かなくなっていたので、一部修正しました。ただ、今後のことを考えるとWebViewではなくWkWebViewを使って動作するものがあったほうがよいでしょう
AppleScript名:指定URLをロードして1枚ものの大きなPNGにレンダリング v2a |
— Created 2015-09-15 by Takaaki Naganoya — Modified 2019-11-09 by Takaaki Naganoya — 2019 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/jp/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 if class of targRect = record then set aWidth to width of |size| of targRect set aHeight to height of |size| of targRect else if class of targRect = list then set aWidth to item 1 of item 2 of targRect set aHeight to item 2 of item 2 of targRect end if 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 |