アラートダイアログ上にWKWebViewを表示するAppleScriptです。
テストのためにYouTubeのムービーの自動再生URL(戦場の絆のリプレイムービー)をオープンしています。
▲Table ViewとWeb Viewを組み合わせて、所定の場所にYouTubeムービーの頭出しを行う試作品を作ってみたものの、JavaScript経由でWKWebViewをコントロールするのが難しくて頓挫
本来であれば、WkWebViewに対してJavaScript経由でさまざまな操作を行いたいところですが、オープン中のWebコンテンツに対して新たなJavaScriptのインスタンスを生成してセキュリティチェックを行なって、実際に実行を行うのが(自分には)少々難しく感じました。WkWebViewだとあまり凝った制御はできない印象です。Safariをコントロールするほうが自由度が高いところ。
WkWebViewを「自由にこづきまわして操作できる部品」として使うためには、素のままのWkWebViewではない何かを使ったほうがいいのかも?
AppleScript名:アラートダイアログ上にWebViewを表示.scptd |
— – Created by: Takaaki Naganoya – Created on: 2019/03/02 — – Copyright © 2019 Piyomaru Software, All Rights Reserved — use AppleScript version "2.4" — Yosemite (10.10) or later use framework "Foundation" use framework "AppKit" use framework "WebKit" use scripting additions property |NSURL| : a reference to current application’s |NSURL| property NSAlert : a reference to current application’s NSAlert property NSColor : a reference to current application’s NSColor property NSString : a reference to current application’s NSString property NSScreen : a reference to current application’s NSScreen property NSButton : a reference to current application’s NSButton property WKWebView : a reference to current application’s WKWebView property NSScrollView : a reference to current application’s NSScrollView property WKUserScript : a reference to current application’s WKUserScript property NSURLRequest : a reference to current application’s NSURLRequest property NSRunningApplication : a reference to current application’s NSRunningApplication property NSUTF8StringEncoding : a reference to current application’s NSUTF8StringEncoding property NSBackingStoreBuffered : a reference to current application’s NSBackingStoreBuffered property WKUserContentController : a reference to current application’s WKUserContentController property NSMomentaryLightButton : a reference to current application’s NSMomentaryLightButton property WKWebViewConfiguration : a reference to current application’s WKWebViewConfiguration property NSAlertSecondButtonReturn : a reference to current application’s NSAlertSecondButtonReturn property WKUserScriptInjectionTimeAtDocumentEnd : a reference to current application’s WKUserScriptInjectionTimeAtDocumentEnd property theResult : 0 property returnCode : 0 property theDataSource : {} set aURL to "https://www.youtube.com/embed/GP_tVXTYdmY?autoplay=1&hd=1" set paramObj to {myMessage:"Browse a Replay", mySubMessage:"Browse Senjo-No-Kizuna Replay Movie", targURL:aURL} my performSelectorOnMainThread:"browseWebContents:" withObject:(paramObj) waitUntilDone:true on browseWebContents:paramObj set aMainMes to myMessage of paramObj set aSubMes to mySubMessage of paramObj set tmpURL to (targURL of paramObj) set aWidth to 600 set aHeight to 450 –WebViewをつくる set aConf to WKWebViewConfiguration’s alloc()’s init() –指定URLのJavaScriptをFetch set jsSource to my fetchJSSourceString(tmpURL) set userScript to WKUserScript’s alloc()’s initWithSource:jsSource injectionTime:(WKUserScriptInjectionTimeAtDocumentEnd) forMainFrameOnly:true set userContentController to WKUserContentController’s alloc()’s init() userContentController’s addUserScript:(userScript) aConf’s setUserContentController:userContentController set aWebView to WKWebView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aWidth, aHeight – 100)) configuration:aConf aWebView’s setNavigationDelegate:me aWebView’s setUIDelegate:me aWebView’s setTranslatesAutoresizingMaskIntoConstraints:true set bURL to |NSURL|’s URLWithString:tmpURL set aReq to NSURLRequest’s requestWithURL:bURL aWebView’s loadRequest:aReq –Webコンテンツのローディング — set up alert set theAlert to NSAlert’s alloc()’s init() tell theAlert its setMessageText:aMainMes its setInformativeText:aSubMes its addButtonWithTitle:"OK" its addButtonWithTitle:"Cancel" its setAccessoryView:aWebView end tell — show alert in modal loop NSRunningApplication’s currentApplication()’s activateWithOptions:0 my performSelectorOnMainThread:"doModal:" withObject:(theAlert) waitUntilDone:true –Stop Web View Action set bURL to |NSURL|’s URLWithString:"about:blank" set bReq to NSURLRequest’s requestWithURL:bURL aWebView’s loadRequest:bReq if (my returnCode as number) = 1001 then error number -128 end browseWebContents: on doModal:aParam set (my returnCode) to aParam’s runModal() end doModal: on viewDidLoad:aNotification return true end viewDidLoad: on fetchJSSourceString(aURL) set jsURL to |NSURL|’s URLWithString:aURL set jsSourceString to NSString’s stringWithContentsOfURL:jsURL encoding:(NSUTF8StringEncoding) |error|:(missing value) return jsSourceString end fetchJSSourceString |
More from my site
(Visited 91 times, 1 visits today)