AppleScript名:指定URLをロードしてページ中の画像を取得 v2 |
— Created 2015-09-07 by Takaaki Naganoya — 2015 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "WebKit" property loadDone : false property theWebView : missing value set aURL to "http://piyocast.com/as/archives/1176" set aRes to getPage(aURL) set aList to getPageImages() of me set theWebView to missing value –Purge aList –theWebViewにロードしておいたページから画像情報を取得 on getPageImages() set aList to {} set imgNum to ((my theWebView)’s stringByEvaluatingJavaScriptFromString:"document.images.length") as integer repeat with i from 0 to (imgNum – 1) set jsT to "document.images[" & (i as string) & "].height" set aHeight to ((my theWebView)’s stringByEvaluatingJavaScriptFromString:jsT) as integer set jsT to "document.images[" & (i as string) & "].width" set aWidth to ((my theWebView)’s stringByEvaluatingJavaScriptFromString:jsT) as integer set jsT to "document.images[" & (i as string) & "].src" set aSRC to ((my theWebView)’s stringByEvaluatingJavaScriptFromString:jsT) as text set the end of aList to {aHeight, aWidth, aSRC} end repeat return aList end getPageImages –theWebViewにロードしておいたページからタイトルを取得 on getPageTitle() set x to ((my theWebView)’s stringByEvaluatingJavaScriptFromString:"document.title") as text return x end getPageTitle –指定のURLの内容をtheWebViewに取得 on getPage(aURL) –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 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 return true end getPage –WebViewにURLを読み込む on openURL(aURL) set noter1 to current application’s NSNotificationCenter’s defaultCenter() set my theWebView to current application’s WebView’s alloc()’s init() noter1’s addObserver:me selector:"webLoaded:" |name|:(current application’s WebViewProgressFinishedNotification) object:(my theWebView) my (theWebView’s setMainFrameURL:aURL) end openURL –Web Viewのローディング完了時に実行 on webLoaded:aNotification set my loadDone to true end webLoaded: |
More from my site
(Visited 62 times, 1 visits today)