アラートダイアログ上のWebViewにSVGで作った円グラフを表示するAppleScriptです。
Objective-Cで作られたチャート表示フレームワークなどを呼び出して試していますが、スクリプトエディタ上で作成したAppleScriptから呼び出すと、アニメーションしなかったり表示が行えなかったりといろいろ環境にフィットしていません。
# 逆をいえば、Xcode上でCocoa AppleScriptアプリケーションを作成した場合には割とチャート表示部品をそのまま使えます
そこで、単純なグラフ表示を行うようにレベルを下げ、SVGをWkWebView上に表示する形式にしてみました。
本Scriptでは最低限のサンプルを表示させていますが、SVGの組み立てをもう少し強化してグラフの背景を透明にできればいい感じになるのではないでしょうか。
▲世界地図あたりだといい感じに見えるかもしれません。当該の国だけ色を変えるような処理を仕込めば何かに使えそうです。
AppleScript名:アラートダイアログ上のWebViewに円グラフを表示.scptd |
— – Created by: Takaaki Naganoya – Created on: 2019/11/18 — – 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 NSImage : a reference to current application’s NSImage 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 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 WKUserContentController : a reference to current application’s WKUserContentController property WKWebViewConfiguration : a reference to current application’s WKWebViewConfiguration property WKUserScriptInjectionTimeAtDocumentEnd : a reference to current application’s WKUserScriptInjectionTimeAtDocumentEnd property theResult : 0 property returnCode : 0 –set svgCon to read (choose file) set svgCon to retSampleSVG() of me set paramObj to {myMessage:"Browse Pie Chart", mySubMessage:"This is a sample pie chart made by SVG", targContents:svgCon} –my browseLocalWebContents:paramObj my performSelectorOnMainThread:"browseLocalWebContents:" withObject:(paramObj) waitUntilDone:true on browseLocalWebContents:paramObj set aMainMes to (myMessage of paramObj) as string set aSubMes to (mySubMessage of paramObj) as string set tmpURL to (targContents of paramObj) as string –local contents set aWidth to 330 set aHeight to 430 –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 aWebView’s setOpaque:false aWebView’s setBackgroundColor:(NSColor’s clearColor()) –aWebView’s scrollView()’s setBackgroundColor:(NSColor’s clearColor()) aWebView’s loadHTMLString:tmpURL baseURL:(missing value) — 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 browseLocalWebContents: on doModal:aParam set (my returnCode) to (aParam’s runModal()) as number 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 on retSampleSVG() return "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"300\" height=\"300\" fill-opacity=\"0.9\"> <path d=\"M 150,0 A 150,150 0 1,1 141.8986347236027,299.7810664959306 L 150,150 Z\" fill=\"#0153d8\" stroke=\"#fff\"></path> <text x=\"170\" y=\"150\" fill=\"#fff\" font-size=\"20\">Piyomaru</text> <text x=\"170\" y=\"190\" fill=\"#fff\" font-size=\"40\">50.86%</text> </svg>" end retSampleSVG |
More from my site
(Visited 143 times, 1 visits today)
SVGのPath指定をParseしてx,y座標の最小値、最大値を求める – AppleScriptの穴 says:
[…] SVGで作られた世界地図を表示し、指定の国を赤く塗るというAppleScriptを試作したことがありました(choose countryといった命令を作る野望が)。しかし、メルカトル図法の世界地図で塗った […]