AppleScriptで指定URLのページを表示する「WebKit Utilities」がmacOS 10.13以降の環境で動かなくなっていたので、書き換えて動くようにしておきました。
–> Download WebKit Utilities_archive v2
これ自体が役に立ったということはなく、単にAppleScript Librariesの書き方のサンプルと理解しています。Webサイトの表示を行うよりも1枚ものの画像やPDFにレンダリングする処理のほうがバッチ処理の中では相性がいいと思います。
また、このScriptはWkWebViewではなく古いWebViewを使っているので、じきに動かなくなります。
AppleScript名:WebKit Utilitiesで指定URLをウィンドウ表示 |
— Created 2017-03-24 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use webLib : script "WebKit Utilities" set targetURL to "http://www.piyocast.com/" display URL targetURL window size {1024, 1024} |
AppleScript名:WebKit Utilities |
— An example AppleScript/Objective-C library that uses the WebKit framework.
use AppleScript use framework "AppKit" use framework "WebKit" property NSURL : a reference to current application’s NSURL property WebView : a reference to current application’s WebView property NSScreen : a reference to current application’s NSScreen property NSThread : a reference to current application’s NSThread property NSWindow : a reference to current application’s NSWindow property NSURLRequest : a reference to current application’s NSURLRequest property NSMutableDictionary : a reference to current application’s NSMutableDictionary on _DisplayWebWindowWithURL:theURLString windowSize:theWindowSize — Create and display a horizontally centered window with a WebView set {thisWindowWidth, thisWindowHeight} to theWindowSize set screenBounds to the NSScreen’s mainScreen’s visibleFrame as any if class of screenBounds = record then set screenWidth to screenBounds’s |size|’s width set screenHeight to screenBounds’s |size|’s height set windowLeft to ((screenWidth – thisWindowWidth) / 2) + (screenBounds’s origin’s x) set windowBottom to screenHeight – thisWindowHeight + (screenBounds’s origin’s y) – 40 else copy screenBounds to {{originX, originY}, {screenWidth, screenHeight}} set windowLeft to ((screenWidth – thisWindowWidth) / 2) + originX set windowBottom to screenHeight – thisWindowHeight + originY – 40 end if set theStyleMask to (get current application’s NSTitledWindowMask) + (get current application’s NSClosableWindowMask) + (get current application’s NSMiniaturizableWindowMask) + (get current application’s NSResizableWindowMask) set webWindow to NSWindow’s alloc()’s initWithContentRect:(current application’s NSMakeRect(windowLeft, windowBottom, thisWindowWidth, thisWindowHeight)) ¬ styleMask:theStyleMask backing:(current application’s NSBackingStoreBuffered) defer:false — set webWindow’s title to "WebView Created with AppleScript – " & theURLString set theWebView to WebView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, thisWindowWidth, thisWindowHeight)) ¬ frameName:"WebKit Frame" groupName:"WebKit Group" set webWindow’s contentView to theWebView tell webWindow to makeKeyAndOrderFront:(missing value) — Start loading the URL set theURL to NSURL’s URLWithString:theURLString set theURLRequest to NSURLRequest’s requestWithURL:theURL tell theWebView’s mainFrame to loadRequest:theURLRequest webWindow end _DisplayWebWindowWithURL:windowSize: — The following two handlers exist to deal with running on a background thread. — If we’re on a background thread, we have to run the UI code on the main thread. on _DisplayWebWindowMainThread:parameterDictionary try set theURLString to parameterDictionary’s objectForKey:"url string" set theWindowSize to parameterDictionary’s objectForKey:"window size" set webWindow to my _DisplayWebWindowWithURL:theURLString windowSize:theWindowSize tell parameterDictionary to setObject:webWindow forKey:"result" on error errMsg number errNum using terms from scripting additions display alert "Error!" message errMsg & " (" & errNum & ")" end using terms from tell parameterDictionary to setObject:{errMsg, errNum} forKey:"error" end try end _DisplayWebWindowMainThread: on display URL theURLString window size theWindowSize — All UI methods must be invoked on the main thread. if (NSThread’s isMainThread) then my _DisplayWebWindowWithURL:theURLString windowSize:theWindowSize else — Parameters, results and error information are communicated between threads via a dictionary. set parameterDictionary to NSMutableDictionary’s dictionary() tell parameterDictionary to setObject:theURLString forKey:"url string" tell parameterDictionary to setObject:theWindowSize forKey:"window size" its performSelectorOnMainThread:"_DisplayWebWindowMainThread:" withObject:parameterDictionary waitUntilDone:true — Propagate errors from the other thread set errInfo to parameterDictionary’s objectForKey:"error" if errInfo is not missing value then error errInfo’s first item number errInfo’s second item parameterDictionary’s objectForKey:"result" end if end display URL |