指定のURLの内容をデスクトップ上にwebarchive書類として保存するAppleScriptです。
SafariがAppleScript側にwebarchive保存機能を提供していないので、
(1)GUI Scriptingで無理やりメニューを操作してwebarchive保存
(2)AppleScriptでCocoaの機能を呼び出してwebarchive保存
(3)コマンドラインツールを呼び出してwebarchive保存
といった代替策をとる必要があります。本Scriptは(2)を行なったものです。
オリジナルのAppleScriptはShane StanleyがCocoa Scripting普及初期に書いたもので、非同期で処理しておりそのままではAppleScriptのワークフローに組み込むには難がありました(処理結果が正しく実行できたかなど、結果を確実に確認できたほうがよいので)。
そこで、他のワークフローに組み込みやすいように構造を変えてみました。ただ、内部でdelegateによる通知処理を利用しているのでフォアグラウンドでの実行が必須となります(Command-Control-R)。
AppleScript名:指定URLのページをwebarchive保存 v2 |
— Created 2014-11-13 Shane Stanley — Modified 2018-02-11 Takaaki Naganoya use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "WebKit" property theSender : missing value property thePath : missing value property theSearchString : missing value property loadDone : false –original was "webFrame" property aWebView : missing value set aPath to (POSIX path of (path to desktop)) & ((current application’s NSUUID’s UUID()’s UUIDString()) as string) & ".webarchive" set archRes to archivePageToPath("https://www.apple.com/jp/shop/browse/home/specialdeals/mac", aPath, "Mac mini", me, 10) of me on archivePageToPath(thePageURL, aPath, searchString, sender, timeoutSec) –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 my theSender to sender — store main script so we can call back set my thePath to aPath — store path for use later set my theSearchString to searchString — store for use later set my aWebView to missing value set my loadDone to false — make a WebView set theView to current application’s WebView’s alloc()’s initWithFrame:{origin:{x:0, y:0}, |size|:{width:100, height:100}} — tell it call delegate methods on me theView’s setFrameLoadDelegate:me — load the page theView’s setMainFrameURL:thePageURL –wait for download & saving repeat timeoutSec * 10 times if my loadDone is not equal to false then exit repeat end if delay 0.1 end repeat if my loadDone = false then return "timed out" — the main frame is our interest if (my loadDone) = aWebView’s mainFrame() then — get the text of the page set theText to ((my loadDone)’s DOMDocument()’s documentElement()’s outerText()) — search it if (theText’s rangeOfString:theSearchString)’s |length|() > 0 then — get the data and write it to file set theArchiveData to (my loadDone)’s dataSource()’s webArchive()’s |data|() theArchiveData’s writeToFile:thePath atomically:true — tell our script it’s all done return "The webarchive was saved" else return "Seach string not found" end if end if end archivePageToPath — called when our WebView loads a frame on WebView:curWebView didFinishLoadForFrame:webFrame set my loadDone to webFrame set my aWebView to curWebView end WebView:didFinishLoadForFrame: — called when there’s a problem on WebView:WebView didFailLoadWithError:theError forFrame:webFrame — got an error, bail WebView’s stopLoading:me set my loadDone to false error "The webarchive was not saved" end WebView:didFailLoadWithError:forFrame: |
More from my site
(Visited 180 times, 1 visits today)