Menu

Skip to content
AppleScriptの穴
  • Home
  • Products
  • Books
  • Docs
  • Events
  • Forum
  • About This Blog
  • License
  • 仕事依頼

AppleScriptの穴

Useful & Practical AppleScript archive. Click '★Click Here to Open This Script' Link to download each AppleScript

カテゴリー: Internet

iPhoneから自宅/会社のMac上のAppleScriptを呼び出すEntangler

Posted on 10月 29, 2018 by Takaaki Naganoya

iOSデバイスからMacのAppleScriptを呼び出そう

# 本記事は、2015/9/20に投稿した内容を再掲載したものです(一部修正)

以前、Otto’s Antennaという名前で紹介したアプリケーションがバージョンアップして、「Entangler」という名前になっていました。

これは、iOSデバイスからMac上のAppleScriptをMac上で実行させて、結果をiOSデバイス側で取得/確認するためのアプリケーションです(ハリウッド映画だと、リモート自爆スイッチとかに使いそう)。

主な用途は、仕事場で時間のかかる処理を行うAppleScript(膨大なデータを処理してPDF出力してサーバーにアップロードするとか、とにかく時間のかかる内容)を帰宅後に実行するなど、「職場の資源を使わないと実行できないプログラムを外出先から実行する」ためのトリガーに使うといったところです。

営業から「こういう資料を至急ほしい」と言われて、職場のマシンのファイルを送ればいいだけの処理のために職場に戻りたく…ないですよね。わざわざ帰社したくないとか、時間のかかる処理を夜間に行わせて結果だけ返してほしいとか、Macをそういう使い方をするためのツールとして使うようなケースに役立ちます。

また、大きな会場でKeynoteによるプレゼンを行いつつ、iPhoneでスライドをめくる(Mac上のKeynote書類をすすめるAppleScriptを実行する)とか。あるいは、プリンタに入れておける最大枚数を超えるような超大量のプリントアウトを行いたいんだけど、プリンターの場所まで行かないと他部署からのプリントアウト状況とか用紙補給状況がわからず、プリンターの前から自席のMacのプリントアウトを開始させる必要があるケースとか。

iOS/watchOS側に呼び出しアプリケーションをインストールし、同じApple IDでペアリングされているMac側に実行用のアプリケーションをインストールします。

離れた場所のFAXの前まで行って、自宅のMacからFAX送信させよう

ちょうど、奥方様の実家のFAXの動作確認をする必要があったので、MacにFAXモデムをつないで、指定の電話番号にFAX出力するAppleScriptを書いて、iPhone上のEntanglerから呼び出してみました。

# macOSのFAX出力機能(eFAX)はmacOS 10.13で廃止になりました(執筆時はmacOS 10.10.5でテスト)。LAN上のmacOS 10.12以下のマシンにFAXモデムをつないで運用することになります

EntanglerというアプリケーションのMac版をMacにインストールし、iOS版をiPhoneにインストールして、iOS側からMac側の機能を呼び出すというのが、基本的な運用スタイルです(Apple Watch版もあるとは知りませんでした)。

前バージョンであるOtto’s Antennaよりもデザインがよくなって、iOSデバイスから操作してからの反応が速くなっていました。EntanglerはAppleScriptのほかにもAutomatorアクションやshell scriptなども実行できるそうです(AppleScriptしかテストする気がないですけれども)。

Scriptを実行するMacはSleepせずに、ネットワーク(LAN/WiFi)に接続した状態にしておく必要があります(要注意)。


▲MacBook ProにUSRoboticsのUSB FAX Modem(OS X 10.10.5でも使える)をつなぎ、Sleepしないようにしてスタンバイ


▲Mac上のEntanglerにFAX送信するAppleScriptを登録

実行するAppleScriptは、~/Library/Application Scripts/com.amolloy.ottosantenna/ に入れておく必要があります。実行可能なAppleScriptは、通常形式、バンドル形式、テキスト形式です。

Mac上のEntanglerはMac App Store上で配布されているアプリケーションであるため、当然のことながらSandbox化されています。このため、このEntanglerで呼び出すAppleScriptは「Sandbox内で動く」ことを考慮してある必要があります(OSのバージョンによって制限のきつさ/ゆるさは変わってきますが、アクセス可能なファイル/フォルダが大幅に制限され、OS側から取得できる情報が制限されます)。

AppleScript名:ダイアログてすと
on run anArg
  display dialog (anArg as string)
end run

★Click Here to Open This Script 

▲iOSデバイス側からの実行時にMac側に受け渡されるパラメータを確認


▲iPhone上のEntanglerを起動して、「print_a_fax」Scriptを実行


▲FAX受信中(「ps-ax」と通知が出ている)


▲確認のためにFAXで印刷したYahoo!のトップページ。印刷が途切れているのは、本来A4用紙が必要なところに無理やり別のサイズ(たぶんB5)の紙を突っ込んだため

Otto’s Antenna→Entanglerのアップデートで解消された不具合

前バージョンである「Otto’s Antenna」ではいくつか問題があったわけですが、それがバージョン2.0(2018/10/29再掲載時 バージョン2.3)であるEntanglerではどうなっているでしょうか?

(1)日本語のファイル名のScriptを登録するとおかしくなるバグ

Otto’s Antennaでは、日本語のファイル名のAppleScriptを登録すると、「こんにちは」と「こんにちは~」の2つのファイルが登録されたように見え、「こんにちは~」のほうは実行できないゴーストでした。

実際に日本語のファイル名のAppleScriptを登録してみたところ、この前バージョンで発生していたバグは再発しませんでした。

(2)AppleScriptに引数を渡せない

Otto’s Antennaでは、Shell ScriptなどにArgumentsを渡せるのに、AppleScriptにはArgumentsを渡せない仕様になっていました。

Entanglerで実験してみたところ、iOSデバイス上のEntanglerでScriptボタンを長押しすると、Argumentsの入力ダイアログが表示されます。ダイアログにArgumentsを入力すると、たしかにAppleScript側でArgumentsを受信できました。

AppleScript名:ダイアログてすと
on run anArg
  display dialog (anArg as string)
end run

★Click Here to Open This Script 

今回のFAX送信実験でいえば、送信先のFAX番号を引数に渡せるようにすることも可能です。

(3)Scriptが実行されるまでの時間が長い

Otto’s AntennaのときにはKeynoteのスライドをめくるような用途にはちょっと使えないぐらい応答に時間がかかっていました。iPhone上で操作してからMac上でAppleScriptが実行されるまで、だいたい10秒ぐらいかかっていました。

Entanglerでは、ずいぶん早くなりました。1〜2秒といったところでしょうか。Keynoteのスライドめくりには依然としてちょっと物足りないかもしれませんが、応答速度の向上は歓迎できます。

Otto’s Antennaのときにダメだと感じた点が、ずいぶん改善されています。まだEntanglerのすべての機能を試せていませんが、現時点でも満足できています。

Siriショートカットより65535倍役立つ

iOS 12上の「ショートカット」(Siriショートカット)アプリでも、LAN内でIPアドレス固定で指定できれば、「SSH経由でスクリプトを実行」ショートカットからLAN内のMac上の指定アカウントの指定AppleScriptを実行することができます。

ただし、「ショートカット」アプリはあくまでLAN内という制限がつくため、インターネットごしに(iCloudごしに)AppleScriptを実行できるEntanglerの方がはるかに自由がきいて強力です。

Posted in Internet Remote Control | Tagged iOS | 1 Comment

Safariで表示中のWebページの最終更新日時を取得

Posted on 8月 17, 2018 by Takaaki Naganoya

指定のWebページ(URL)の最終更新日時(Last Modified Date)を取得するAppleScriptです。

AppleScriptそのものにWebの最終更新日時を取得する関数や機能はありません。はい、おしまい。

……というのでは、AppleScriptの本質がぜんぜん分かっていないね、ということになります。AppleScriptは「それができるアプリケーション」(など)に依頼を出すのが処理スタイルだからです。

まずは、Safariにコマンドを投げて実行するスタイル。

AppleScript名:Safariのdo javascriptコマンドで最終更新日時を取得
— Created 2018-08-17 by Takaaki Naganoya
— 2018 Piyomaru Software
tell application "Safari"
  tell front document
    set dRes to (do JavaScript "document.lastModified;")
  end tell
end tell

★Click Here to Open This Script 

だいたいは、これで手を打つでしょう。ただし、最近のmacOSではセキュリティ強化のためにSafariのdo javascriptコマンドがデフォルトでは禁止されているので、Safariで「開発」メニューを表示させたあとに、「開発」メニューの「AppleEventからのJavaScriptを許可」「スマート検索フィールドからのJavaScriptを許可」を実行しておく必要があります(→ 書籍「AppleScript 10大最新技術」P-84)。

Mac AppStore上で配布/販売するアプリケーションの中で処理することを考えると、SafariをコントロールすることをInfo.plist内で宣言しておけばとくに問題はありません。

do javascriptコマンドの実行で一般的にはファイナルアンサーなのですが、なぜでしょう。リアルタイム日付が返ってくるパターンが多いです。

次は、shellのcurlコマンドを呼び出すスタイルです。指定URLのレスポンスヘッダーを出力させられるので、これを検索して出力します。ただ、YouTubeをはじめとするWebサイトでこの最終更新日を返してこないので、これでもダメな時はダメです。

AppleScript名:curlコマンドで最終更新日時を取得
— Created 2018-08-17 by Takaaki Naganoya
— 2018 Piyomaru Software
tell application "Safari"
  tell front document
    set aURL to URL
  end tell
end tell

try
  set uRes to (do shell script "curl -D – -s -o /dev/null " & aURL & " | grep Date:")
on error
  return false
end try

★Click Here to Open This Script 

これも現在日時を返してくるパターンが多いですね。また、噂レベルではあるものの「do shell scriptコマンドは極力使わないほうがいいよ」というお達しがScripter界隈で流れているので、将来的に何かがあるのかもしれません(昔の、ごくごく初期のMac OS XはBSDレイヤーというかBSDコマンド類が、OSインストール時にオプション扱いだったので、そういう未来はあるかもしれない)。

Mac AppStore上で配布/販売するアプリケーションの中で処理するのも、とくに問題はないのですが、今度はネットワーク接続することをあらかじめ宣言しておくのと、httpによる通信を行うことを宣言しておかないとネットワーク接続ができません。

最後の手段。Cocoaを呼び出して自前でWebのレスポンスヘッダーを取得するスタイル。

AppleScript名:Cocoaの機能で最終更新日時を取得
— Created 2018-08-17 by Takaaki Naganoya
— 2018 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

property NSString : a reference to current application’s NSString
property NSLocale : a reference to current application’s NSLocale
property NSURLRequest : a reference to current application’s NSURLRequest
property NSDateFormatter : a reference to current application’s NSDateFormatter
property NSURLConnection : a reference to current application’s NSURLConnection
property NSURLRequestUseProtocolCachePolicy : a reference to current application’s NSURLRequestUseProtocolCachePolicy

tell application "Safari"
  tell front document
    set aURL to URL
  end tell
end tell

set aURL to (current application’s |NSURL|’s URLWithString:aURL)
set {exRes, headerRes, aData} to checkURLResourceExistence(aURL, 3) of me
set aDate to headerRes’s |date| as string

set lastUpdateDate to dateFromStringWithDateFormat(aDate, "EEE, dd MMM yyyy HH:mm:ss zzz") of me
return lastUpdateDate

— 指定URLにファイル(画像など)が存在するかチェック
–> {存在確認結果(boolean), レスポンスヘッダー(NSDictionary), データ(NSData)}
on checkURLResourceExistence(aURL, timeOutSec as real)
  set aRequest to (NSURLRequest’s requestWithURL:aURL cachePolicy:(NSURLRequestUseProtocolCachePolicy) timeoutInterval:timeOutSec)
  
set aRes to (NSURLConnection’s sendSynchronousRequest:aRequest returningResponse:(reference) |error|:(missing value))
  
set dRes to (first item of (aRes as list))
  
set bRes to (second item of (aRes as list))
  
if bRes is not equal to missing value then
    set hRes to (bRes’s allHeaderFields())
    
set aResCode to (bRes’s statusCode()) as integer
  else
    set hRes to {}
    
set aResCode to -1 –error
  end if
  
return {(aResCode = 200), hRes, dRes}
end checkURLResourceExistence

–指定形式の日付テキストをAppleScriptのdateオブジェクトに変換
on dateFromStringWithDateFormat(dateString, dateFormat)
  set dStr to NSString’s stringWithString:dateString
  
set dateFormatStr to NSString’s stringWithString:dateFormat
  
  
set aDateFormatter to NSDateFormatter’s alloc()’s init()
  
aDateFormatter’s setDateFormat:dateFormatStr
  
aDateFormatter’s setLocale:(NSLocale’s alloc()’s initWithLocaleIdentifier:"en_US_POSIX")
  
  
set aDestDate to (aDateFormatter’s dateFromString:dStr)
  
  
return aDestDate as date
end dateFromStringWithDateFormat

★Click Here to Open This Script 

結果は3つとも変わりませんでした。Cocoa呼び出しするものも、作り置きしておいたサブルーチンを使いまわしただけなので、作るのに3分もかかっていません。

curlを呼び出すスタイル同様、Mac AppStore上で配布/販売するアプリケーションの中で処理するのもとくに問題はないのですが、httpによる通信を行うことを宣言しておかないとネットワーク接続ができません。

Safariでdo javascriptコマンドを実行するものは、最初にdo javascriptコマンドを実行する設定が必要。curlコマンドはまあそんなもんだろうかと。Cocoaの機能を呼び出す方法は、ここまでやってダメならあきらめがつくというところでしょうか。

Posted in Calendar Internet JavaScript URL | Tagged 10.11savvy 10.12savvy 10.13savvy NSDateFormatter NSLocale NSString NSURLConnection NSURLRequest Safari | Leave a comment

Safariで検索を実行

Posted on 4月 2, 2018 by Takaaki Naganoya

Safariで指定の検索エンジンで検索を実行するAppleScriptです。

URL欄にキーワードを入れて検索を行うと、文字入力の途中で待たされることがあるため、別途ダイアログで入力して検索できたほうが便利です。

Script Menuに入れて呼び出して使っています。

AppleScript名:Safariで検索を実行
tell application "Safari"
  set aText to text returned of (display dialog "検索キーワードを入力" default answer "")
  
  
set dCount to count every document
  
if dCount > 0 then
    search the web in front document for aText
  else
    search the web for aText
  end if
end tell

★Click Here to Open This Script 

Posted in Internet | Tagged 10.11savvy 10.12savvy 10.13savvy Safari | 1 Comment

Read Safari Bookmark v2

Posted on 2月 11, 2018 by Takaaki Naganoya

Safariのブックマーク内容からURLとページタイトルを取得してテキスト書き出しするAppleScriptです。

一番のみどころは、Predicate文でURLStringがnilでなければ、という記述を行っている箇所です。AppleScriptのデータに変換するとnilはmissing valueになるので、ここでmissing valueを指定したくなるところですが、nilを指定しました。

実行のためには、AppleScriptの実行プログラム(スクリプトエディタとかScript Debugger)を「システム環境設定」の「セキュリティとプライバシー」>「プライバシー」>「フルディスクアクセス」に登録しておく必要があります。AppleScriptアプレットから実行する場合には、そのAppleScriptアプレットそのものを「フルディスクアクセス」に登録する必要があります。

AppleScript名:Read Safari Bookmark v2
— Created 2016-02-27 by Takaaki Naganoya
— Piyomaru Software 2016
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

script spdBk
  property outList : {}
end script

set (outList of spdBk) to {}
set libPath to (POSIX path of (path to library folder from user domain)) & "Safari/Bookmarks.plist"
set aRec to retDictFromPlist(libPath) of me
set aList to aRec’s Children

set aPredicate to current application’s NSPredicate’s predicateWithFormat:"URLString != nil"
set filteredArray to (aList’s filteredArrayUsingPredicate:aPredicate) as list

repeat with i in filteredArray
  set the end of (outList of spdBk) to "■" & (Title of URIDictionary of i) & return & (URLString of i)
end repeat

set aRes to listToStringUsingTextItemDelimiter(contents of (outList of spdBk), return & return) of me

–Read plist as record
on retDictFromPlist(aPath as text)
  set thePath to current application’s NSString’s stringWithString:aPath
  
set thePath to thePath’s stringByExpandingTildeInPath()
  
set theDict to current application’s NSDictionary’s dictionaryWithContentsOfFile:thePath
  
return theDict
end retDictFromPlist

on listToStringUsingTextItemDelimiter(sourceList, textItemDelimiter)
  set anArray to current application’s NSArray’s arrayWithArray:sourceList
  
set aString to anArray’s componentsJoinedByString:textItemDelimiter
  
return (aString as string)
end listToStringUsingTextItemDelimiter

★Click Here to Open This Script 

Posted in file Internet | Tagged 10.11savvy 10.12savvy 10.13savvy Safari | Leave a comment

Safariで表示中のURLをwebarchive保存

Posted on 2月 11, 2018 by Takaaki Naganoya

Safariで表示中の最前面のウィンドウのURLの内容をデスクトップ上にWebarchive書類として保存するAppleScriptです。

webArchiveはDeprecatedになったので、macOS 10.14あたりまでのOSで使うならOK。今後については何か別の機能が実装されるのかどうか。macOS 10.15で別の何かが紹介されるのでしょうか。

AppleScript名:Safariで表示中のURLをwebarchive保存
— 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 loadDone : false –original was "webFrame"
property aWebView : missing value

tell application "Safari"
  if (count every document) = 0 then return
  
tell front document
    set aURL to URL
  end tell
end tell

set aPath to (POSIX path of (path to desktop)) & ((current application’s NSUUID’s UUID()’s UUIDString()) as string) & ".webarchive"
set archRes to archivePageToPathWithoutSearching(aURL, aPath, me, 10) of me

on archivePageToPathWithoutSearching(thePageURL, aPath, 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 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 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"
  end if
end archivePageToPathWithoutSearching

— 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:

★Click Here to Open This Script 

Posted in file Internet Require Control-Command-R to run | Tagged 10.11savvy 10.12savvy 10.13savvy Safari | 3 Comments

指定URLのページをwebarchive保存 v2

Posted on 2月 11, 2018 by Takaaki Naganoya

指定の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:

★Click Here to Open This Script 

Posted in file Internet Require Control-Command-R to run | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

指定URLをロードしてページ中の画像を取得 v2

Posted on 2月 11, 2018 by Takaaki Naganoya
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:

★Click Here to Open This Script 

Posted in file Internet JavaScript Require Control-Command-R to run | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

指定URLをロードしてtitleを取得

Posted on 2月 11, 2018 by Takaaki Naganoya
AppleScript名:指定URLをロードしてtitleを取得
— 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 "https://www.youtube.com/watch?v=WuziqYptTyE"
set aTitle to getPageTitle(aURL)
–>  "戦場の絆ポータブル【HD】鉱山都市 オンライン対戦 2015.09.04 – YouTube"

on getPageTitle(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
  
  
set jsText to "document.title"
  
set x to ((my theWebView)’s stringByEvaluatingJavaScriptFromString:jsText) as text
  
set my theWebView to missing value
  
  
return x
end getPageTitle

–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:

★Click Here to Open This Script 

Posted in Internet JavaScript Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

web上の画像をローカルにダウンロードして保存

Posted on 2月 11, 2018 by Takaaki Naganoya
AppleScript名:web上の画像をローカルにダウンロードして保存
— Created 2013-12-27 Shane Stanley
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set thePicURL to "http://www.macosxautomation.com/applescript/apps/gfx/EverydayCover300.jpg"
set thePath to POSIX path of ((path to desktop as text) & "Test.jpg")
set aRes to saveImageURLToPath(thePicURL, thePath)

on saveImageURLToPath(thePicURL, thePath)
  set theNSURL to current application’s |NSURL|’s URLWithString:thePicURL
  
set picData to current application’s NSData’s dataWithContentsOfURL:theNSURL
  
picData’s writeToFile:thePath atomically:true
end saveImageURLToPath

★Click Here to Open This Script 

Posted in file Internet | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

URLの妥当性チェック

Posted on 2月 11, 2018 by Takaaki Naganoya
AppleScript名:URLの妥当性チェック
— Created 2015-09-06 by Takaaki Naganoya
— 2015 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
–http://stackoverflow.com/questions/1471201/how-to-validate-an-url-on-the-iphone

set aRes1 to validateURL("http://www.apple.com/jp")
–>  true

set aRes2 to validateURL("http.s://www.gmail.com")
–>  false
set aRes3 to validateURL("https:.//gmailcom")
–>  false
set aRes4 to validateURL("https://gmail.me.")
–>  false
set aRes5 to validateURL("https://www.gmail.me.com.com.com.com")
–>  true
set aRes6 to validateURL("http:/./ww-w.wowone.com")
–>  false
set aRes7 to validateURL("http://.www.wowone")
–>  false
set aRes8 to validateURL("http://www.wow-one.com")
–>  true
set aRes9 to validateURL("http://www.wow_one.com")
–>  true
set aRes10 to validateURL("http://.")
–>  false
set aRes11 to validateURL("http://")
–>  false
set aRes12 to validateURL("http://k")
–>  false

return {aRes2, aRes3, aRes4, aRes5, aRes6, aRes7, aRes8, aRes9, aRes10, aRes11, aRes12}
–>  {​​​​​false, ​​​​​false, ​​​​​false, ​​​​​true, ​​​​​false, ​​​​​false, ​​​​​true, ​​​​​true, ​​​​​false, ​​​​​false, ​​​​​false​​​}

–URLの妥当性チェック
on validateURL(anURL as text)
  –set regEx1 to current application’s NSString’s stringWithString:"(http|https)://((\\w)*|([0-9]*)|([-|_])*)+([\\.|/]((\\w)*|([0-9]*)|([-|_])*))+"
  
set regEx1 to current application’s NSString’s stringWithString:"((https|http)://)((\\w|-)+)(([.]|[/])((\\w|-)+))+"
  
set predicate1 to current application’s NSPredicate’s predicateWithFormat_("SELF MATCHES %@", regEx1)
  
set aPredRes1 to (predicate1’s evaluateWithObject:anURL) as boolean
  
return aPredRes1
end validateURL

★Click Here to Open This Script 

Posted in Internet regexp Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

VPN経由のネットワーク接続を切断する

Posted on 2月 11, 2018 by Takaaki Naganoya
AppleScript名:VPN経由のネットワーク接続を切断する
— Created 2017-09-30 by Takaaki Naganoya
— 2017 Piyomaru Software

tell application "System Events"
  tell current location of network preferences
    set sList to every service whose kind is 13 and active is true –種別がVPNのNetwork Serviceで状態がactive(接続中)のものを取得
    
if sList = {} then return false
    
    
if length of sList is not equal to 1 then
      –複数VPNが存在している場合にはユーザー選択
      
set ssRes to choose from list sList
      
if ssRes = false then return false
      
set sRes to first item of ssRes
    else
      set sRes to first item of sList
    end if
    
    
disconnect sRes
  end tell
end tell

★Click Here to Open This Script 

Posted in Internet | Tagged 10.11savvy 10.12savvy 10.13savvy System Events | Leave a comment

VPN経由でネットワーク接続

Posted on 2月 11, 2018 by Takaaki Naganoya

VPN経由でネットワーク接続を行うAppleScriptです。

システム環境設定の「ネットワーク」で「VPN」を追加、定義し、正常にVPN接続できていることを確認してください。本AppleScriptはあらかじめ定義してあるVPN接続先への自動接続を行うものです。

複数のVPN接続を定義してある場合にはユーザーに選択を求めます。

VPN接続関連のAppleScriptの機能に、勝手に新規接続先を定義してVPN接続できるようなものはありません。あくまで、すでにユーザーが定義したVPN接続先に対して接続/切断を行うだけの機能です。

AppleScript名:VPN経由でネットワーク接続
— Created 2017-09-30 by Takaaki Naganoya
— 2017 Piyomaru Software

tell application "System Events"
  tell current location of network preferences
    set sList to every service whose kind is 13 –種別がVPNのNetwork Service
    
if sList = {} then return false
    
    
if length of sList is not equal to 1 then
      –複数VPNが存在している場合にはユーザー選択
      
set ssRes to choose from list sList
      
if ssRes = false then return false
      
set sRes to first item of ssRes
    else
      set sRes to first item of sList
    end if
    
    
connect sRes
  end tell
end tell

★Click Here to Open This Script 

Posted in Internet | Tagged 10.11savvy 10.12savvy 10.13savvy System Events | Leave a comment

(GET)Wikipedia APIでキーワード検索を行う

Posted on 2月 10, 2018 by Takaaki Naganoya
AppleScript名:(GET)Wikipedia APIでキーワード検索を行う
— Created 2016-11-04 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
–https://www.mediawiki.org/wiki/API:Main_page/ja

set reqURLStr to "https://jp.wikipedia.org/w/api.php" –Japanese Version

set aRec to {action:"query", titles:"アップル (企業)", |prop|:"revisions", rvprop:"content", |format|:"json"}
set aURL to retURLwithParams(reqURLStr, aRec) of me
set aRes to callRestGETAPIAndParseResults(aURL) of me

set aRESTres to (query of json of aRes)
return aRESTres as list of string or string –as anything

–GET methodのREST APIを呼ぶ
on callRestGETAPIAndParseResults(aURL)
  set aRequest to current application’s NSMutableURLRequest’s requestWithURL:(current application’s |NSURL|’s URLWithString:aURL)
  
  
aRequest’s setHTTPMethod:"GET"
  
aRequest’s setCachePolicy:(current application’s NSURLRequestReloadIgnoringLocalCacheData)
  
aRequest’s setHTTPShouldHandleCookies:false
  
aRequest’s setTimeoutInterval:60
  
aRequest’s setValue:"application/json" forHTTPHeaderField:"Accept"
  
  
set aRes to current application’s NSURLConnection’s sendSynchronousRequest:aRequest returningResponse:(reference) |error|:(missing value)
  
set resList to aRes as list
  
  
set bRes to contents of (first item of resList)
  
set resStr to current application’s NSString’s alloc()’s initWithData:bRes encoding:(current application’s NSUTF8StringEncoding)
  
  
set jsonString to current application’s NSString’s stringWithString:resStr
  
set jsonData to jsonString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding)
  
set aJsonDict to current application’s NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value)
  
  
–Get Response Code & Header
  
set dRes to contents of second item of resList
  
if dRes is not equal to missing value then
    set resCode to (dRes’s statusCode()) as number
    
set resHeaders to (dRes’s allHeaderFields()) as record
  else
    set resCode to 0
    
set resHeaders to {}
  end if
  
  
return {json:aJsonDict, responseCode:resCode, responseHeader:resHeaders}
end callRestGETAPIAndParseResults

on retURLwithParams(aBaseURL, aRec)
  set aDic to current application’s NSMutableDictionary’s dictionaryWithDictionary:aRec
  
  
set aKeyList to (aDic’s allKeys()) as list
  
set aValList to (aDic’s allValues()) as list
  
set aLen to length of aKeyList
  
  
set qList to {}
  
repeat with i from 1 to aLen
    set aName to contents of item i of aKeyList
    
set aVal to contents of item i of aValList
    
set the end of qList to (current application’s NSURLQueryItem’s queryItemWithName:aName value:aVal)
  end repeat
  
  
set aComp to current application’s NSURLComponents’s alloc()’s initWithString:aBaseURL
  
aComp’s setQueryItems:qList
  
set aURL to (aComp’s |URL|()’s absoluteString()) as text
  
  
return aURL
end retURLwithParams

on urlencodeStr(aStr)
  set aString to current application’s NSString’s stringWithString:aStr
  
set aString to (aString’s stringByAddingPercentEncodingWithAllowedCharacters:(current application’s NSCharacterSet’s URLQueryAllowedCharacterSet())) as text
  
return aString
end urlencodeStr

★Click Here to Open This Script 

Posted in Internet REST API | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

connpassイベントサーチAPIで検索を行うv2

Posted on 2月 10, 2018 by Takaaki Naganoya
AppleScript名:connpassイベントサーチAPIで検索を行うv2
— Created 2016-10-29 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
–http://connpass.com/about/api/
–http://piyocast.com/as/archives/4300

set reqURLStr to "https://connpass.com/api/v1/event/"

set aRec to {keyword:"AppleScript", ym:"201611"} –サーチクエリーと対象月
set aURL to retURLwithParams(reqURLStr, aRec) of me
set aRes to callRestGETAPIAndParseResults(aURL) of me

set aRESTres to (json of aRes) as record
return aRESTres
–>
(*
{results_available:1, results_start:1, |events|:{{place:"マイ・スペース MS&BB 池袋西武横店 1号室", event_url:"http://ashole.connpass.com/event/44103/", accepted:2, title:"AppleScript本フィードバック会", limit:7, event_type:"participation", owner_id:64136, ended_at:"2016-11-26T20:30:00+09:00", updated_at:"2016-11-01T09:22:15+09:00", lon:"139.711383200000", waiting:0, event_id:44103, hash_tag:"AppleScript,Mac,macOS,Mac OS X", owner_nickname:"Piyomaru", lat:"35.726486900000", started_at:"2016-11-26T18:30:00+09:00", owner_display_name:"Piyomaru", catch:"「AppleScript最新リファレンス」「AppleScript最新10大技術」についての解説", series:{|url|:"http://ashole.connpass.com/", |id|:3041, title:"AppleScriptの穴"}, address:"〒171-0022 東京都豊島区南池袋1-16-20(ぬかりやビル2階)", |description|:"<p>macOS標準装備で、GUIアプリケーションを操作できるマクロ言語「AppleScript」、その20年以上の歴史をまとめ、最新情報を盛り込んだ電子書籍「AppleScript最新リファレンス」「AppleScript 最新10大技術」を発行いたしました。</p>\n<p>・電子書籍オンライン販売URL\n<a href=\"https://piyomarusoft.booth.pm\" rel=\"nofollow\">https://piyomarusoft.booth.pm</a></p>\n<p>これらの本について、分からない点やもっと知りたい点について、筆者本人と直接お話できる場を設けました。</p>\n<p>参加資格は、AppleScriptを実際に使っている方、興味を持っている方で、筆者の書籍を実際に購入した、あるいは購入しようと考えている方です。事前に内容を読んであることが望ましいです。</p>\n<p>筆者Blog「AppleScriptの穴」\n<a href=\"http://piyocast.com/as/\" rel=\"nofollow\">http://piyocast.com/as/</a></p>"}}, results_returned:1}
*)

–GET methodのREST APIを呼ぶ
on callRestGETAPIAndParseResults(aURL)
  set aRequest to current application’s NSMutableURLRequest’s requestWithURL:(current application’s |NSURL|’s URLWithString:aURL)
  
  
aRequest’s setHTTPMethod:"GET"
  
aRequest’s setCachePolicy:(current application’s NSURLRequestReloadIgnoringLocalCacheData)
  
aRequest’s setHTTPShouldHandleCookies:false
  
aRequest’s setTimeoutInterval:60
  
aRequest’s setValue:"application/json" forHTTPHeaderField:"Accept"
  
  
set aRes to current application’s NSURLConnection’s sendSynchronousRequest:aRequest returningResponse:(reference) |error|:(missing value)
  
set resList to aRes as list
  
  
set bRes to contents of (first item of resList)
  
set resStr to current application’s NSString’s alloc()’s initWithData:bRes encoding:(current application’s NSUTF8StringEncoding)
  
  
set jsonString to current application’s NSString’s stringWithString:resStr
  
set jsonData to jsonString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding)
  
set aJsonDict to current application’s NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value)
  
  
–Get Response Code & Header
  
set dRes to contents of second item of resList
  
if dRes is not equal to missing value then
    set resCode to (dRes’s statusCode()) as number
    
set resHeaders to (dRes’s allHeaderFields()) as record
  else
    set resCode to 0
    
set resHeaders to {}
  end if
  
  
return {json:aJsonDict, responseCode:resCode, responseHeader:resHeaders}
end callRestGETAPIAndParseResults

on retURLwithParams(aBaseURL, aRec)
  set aDic to current application’s NSMutableDictionary’s dictionaryWithDictionary:aRec
  
  
set aKeyList to (aDic’s allKeys()) as list
  
set aValList to (aDic’s allValues()) as list
  
set aLen to length of aKeyList
  
  
set qList to {}
  
repeat with i from 1 to aLen
    set aName to contents of item i of aKeyList
    
set aVal to contents of item i of aValList
    
set the end of qList to (current application’s NSURLQueryItem’s queryItemWithName:aName value:aVal)
  end repeat
  
  
set aComp to current application’s NSURLComponents’s alloc()’s initWithString:aBaseURL
  
aComp’s setQueryItems:qList
  
set aURL to (aComp’s |URL|()’s absoluteString()) as text
  
  
return aURL
end retURLwithParams

on urlencodeStr(aStr)
  set aString to current application’s NSString’s stringWithString:aStr
  
set aString to (aString’s stringByAddingPercentEncodingWithAllowedCharacters:(current application’s NSCharacterSet’s URLQueryAllowedCharacterSet())) as text
  
return aString
end urlencodeStr

★Click Here to Open This Script 

Posted in Internet REST API | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

(GET)Yahoo! 住所ジオコーダAPIを呼び出す

Posted on 2月 8, 2018 by Takaaki Naganoya

Yahoo!の住所ジオコーダAPIを呼び出して、住所情報から緯度、経度を取得するAppleScriptです。

実行のためには、Yahoo!に利用登録を行い、API Keyを取得してretAccessKey()ハンドラ内に書いておいてください。これは、サンプル用にわかりやすさを優先したためで、実際にはKeychainにAPI Keyを入れておいて問い合わせ、Script中にAPI Keyを直接書かないのが望ましいところです。

GoogleやYahoo!のWeb APIベース、AppleのOS内蔵住所ジオコーダなどさまざまですが、個人的にはYahoo!の住所ジオコーダをよく使っています。ただし、指定の住所がかならずしも一発で緯度・経度情報に変換できるわけでもないので、エラー時には後ろから1文字ずつ削除して再試行しています。

AppleScript名:(GET)Yahoo! 住所ジオコーダAPIを呼び出す
— Created 2016-11-25 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
–http://developer.yahoo.co.jp/webapi/map/openlocalplatform/v1/geocoder.html

set anAddress to "東京都墨田区押上1-1−2"
set aResList to addressGeoCoderByYahoo(anAddress) of me
–>  {​​​​​"35.66042757", ​​​​​"139.72918139"​​​}

on addressGeoCoderByYahoo(addrStr)
  set reqURLStr to "http://geo.search.olp.yahooapis.jp/OpenLocalPlatform/V1/geoCoder"
  
set aKey to retAccessKey() of me
  
set aRec to {query:addrStr, output:"json", appid:aKey, datum:"tky"} –日本測地系
  
set aURL to retURLwithParams(reqURLStr, aRec) of me
  
set aRes to callRestGETAPIAndParseResults(aURL) of me
  
  
set aRESCode to responseCode of aRes
  
if aRESCode is not equal to 200 then return false
  
set aRESHeader to responseHeader of aRes
  
  
set aJSONres to (json of aRes)
  
  
set anAdd1 to (aJSONres’s valueForKeyPath:"Feature")’s firstObject()
  
set aGPSstr to (anAdd1’s valueForKeyPath:"Geometry.Coordinates")
  
set {aLon, aLat} to separateStrByAMark(aGPSstr, ",") of me
  
  
return {aLat, aLon}
end addressGeoCoderByYahoo

–GET methodのREST APIを呼ぶ
on callRestGETAPIAndParseResults(aURL)
  set aRequest to current application’s NSMutableURLRequest’s requestWithURL:(current application’s |NSURL|’s URLWithString:aURL)
  
  
aRequest’s setHTTPMethod:"GET"
  
aRequest’s setCachePolicy:(current application’s NSURLRequestReloadIgnoringLocalCacheData)
  
aRequest’s setHTTPShouldHandleCookies:false
  
aRequest’s setTimeoutInterval:60
  
aRequest’s setValue:"application/json" forHTTPHeaderField:"Accept"
  
  
–CALL REST API
  
set aRes to current application’s NSURLConnection’s sendSynchronousRequest:aRequest returningResponse:(reference) |error|:(missing value)
  
  
–Parse Results
  
set resList to aRes as list
  
  
set bRes to contents of (first item of resList)
  
set resStr to current application’s NSString’s alloc()’s initWithData:bRes encoding:(current application’s NSUTF8StringEncoding)
  
  
set jsonString to current application’s NSString’s stringWithString:resStr
  
set jsonData to jsonString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding)
  
set aJsonDict to current application’s NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value)
  
  
–Get Response Code & Header
  
set dRes to contents of second item of resList
  
if dRes is not equal to missing value then
    set resCode to (dRes’s statusCode()) as number
    
set resHeaders to (dRes’s allHeaderFields()) as record
  else
    set resCode to 0
    
set resHeaders to {}
  end if
  
  
return {json:aJsonDict, responseCode:resCode, responseHeader:resHeaders}
end callRestGETAPIAndParseResults

on retURLwithParams(aBaseURL, aRec)
  set aDic to current application’s NSMutableDictionary’s dictionaryWithDictionary:aRec
  
  
set aKeyList to (aDic’s allKeys()) as list
  
set aValList to (aDic’s allValues()) as list
  
set aLen to length of aKeyList
  
  
set qList to {}
  
repeat with i from 1 to aLen
    set aName to contents of item i of aKeyList
    
set aVal to contents of item i of aValList
    
set the end of qList to (current application’s NSURLQueryItem’s queryItemWithName:aName value:aVal)
  end repeat
  
  
set aComp to current application’s NSURLComponents’s alloc()’s initWithString:aBaseURL
  
aComp’s setQueryItems:qList
  
set aURL to (aComp’s |URL|()’s absoluteString()) as text
  
  
return aURL
end retURLwithParams

on retAccessKey()
  return "xxXxxxXxXXxxxXXXXXXXXXXXXXXxXXXxxxXXxXXxxXXxxxXXXxxXXXX-" –Yahoo! API Key
end retAccessKey

on urlencodeStr(aStr)
  set aString to current application’s NSString’s stringWithString:aStr
  
set aString to (aString’s stringByAddingPercentEncodingWithAllowedCharacters:(current application’s NSCharacterSet’s URLQueryAllowedCharacterSet())) as text
  
return aString
end urlencodeStr

–リストに入れたレコードを、指定の属性ラベルの値で抽出
on filterRecListByLabel1(aRecList as list, aPredicate as string)
  set aArray to current application’s NSArray’s arrayWithArray:aRecList
  
set aPredicate to current application’s NSPredicate’s predicateWithFormat:aPredicate
  
set filteredArray to aArray’s filteredArrayUsingPredicate:aPredicate
  
set bList to filteredArray as list
  
return bList
end filterRecListByLabel1

–テキストを指定記号を元に分割する
on separateStrByAMark(aStr as string, aMark as string)
  set aMarkLen to length of aMark
  
set aOffset to offset of "," in aStr
  
set aPart to text 1 thru (aOffset – 1) of aStr
  
set bPart to text (aOffset + aMarkLen) thru -1 of aStr
  
return {aPart, bPart}
end separateStrByAMark

★Click Here to Open This Script 

Posted in geolocation Internet REST API | Tagged 10.11savvy 10.12savvy 10.13savvy | 2 Comments

(GET)Yahoo! 逆住所ジオコーダAPIを呼び出す

Posted on 2月 8, 2018 by Takaaki Naganoya

Yahoo!の逆住所ジオコーダAPIを呼び出して、緯度、経度から住所情報を取得するAppleScriptです。

実行のためには、Yahoo!に利用登録を行い、API Keyを取得してretAccessKey()ハンドラ内に書いておいてください。これは、サンプル用にわかりやすさを優先したためで、実際にはKeychainにAPI Keyを入れておいて問い合わせ、Script中にAPI Keyを直接書かないのが望ましいところです。

AppleScript名:(GET)Yahoo! 逆住所ジオコーダAPIを呼び出す
— Created 2016-11-20 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"

–http://developer.yahoo.co.jp/webapi/map/openlocalplatform/v1/reversegeocoder.html

set aLat to 35.74 as string
set aLon to 139.6 as string
set aResList to reverseGeoCoderByYahoo(aLat, aLon) of me
–>  {​​​​​"東京都", ​​​​​"練馬区"​​​}

on reverseGeoCoderByYahoo(aLat, aLon)
  set reqURLStr to "http://reverse.search.olp.yahooapis.jp/OpenLocalPlatform/V1/reverseGeoCoder"
  
set aKey to retAccessKey() of me
  
set aRec to {lat:aLat, lon:aLon, output:"json", appid:aKey, datum:"tky"}
  
set aURL to retURLwithParams(reqURLStr, aRec) of me
  
set aRes to callRestGETAPIAndParseResults(aURL) of me
  
  
set aRESCode to responseCode of aRes
  
if aRESCode is not equal to 200 then return false
  
set aRESHeader to responseHeader of aRes
  
  
set aJSONres to (json of aRes)
  
set anAddress to (aJSONres’s valueForKeyPath:"Feature.Property")’s firstObject()
  
set anElement to anAddress’s valueForKeyPath:"AddressElement"
  
  
set aPrefecture to first item of (my filterRecListByLabel1(anElement, "Level == ’prefecture’"))
  
–>  {​​​​​Kana:"とうきょうと", ​​​​​Name:"東京都", ​​​​​Level:"prefecture", ​​​​​Code:"13"​​​}
  
  
set aCity to first item of (my filterRecListByLabel1(anElement, "Level == ’city’"))
  
–>  {​​​​​Kana:"ねりまく", ​​​​​Name:"練馬区", ​​​​​Level:"city", ​​​​​Code:"13120"​​​}
  
  
set aPref to |Name| of aPrefecture
  
set aCt to |Name| of aCity
  
return {aPref, aCt}
end reverseGeoCoderByYahoo

–GET methodのREST APIを呼ぶ
on callRestGETAPIAndParseResults(aURL)
  set aRequest to current application’s NSMutableURLRequest’s requestWithURL:(current application’s |NSURL|’s URLWithString:aURL)
  
  
aRequest’s setHTTPMethod:"GET"
  
aRequest’s setCachePolicy:(current application’s NSURLRequestReloadIgnoringLocalCacheData)
  
aRequest’s setHTTPShouldHandleCookies:false
  
aRequest’s setTimeoutInterval:60
  
aRequest’s setValue:"application/json" forHTTPHeaderField:"Accept"
  
  
–CALL REST API
  
set aRes to current application’s NSURLConnection’s sendSynchronousRequest:aRequest returningResponse:(reference) |error|:(missing value)
  
  
–Parse Results
  
set resList to aRes as list
  
  
set bRes to contents of (first item of resList)
  
set resStr to current application’s NSString’s alloc()’s initWithData:bRes encoding:(current application’s NSUTF8StringEncoding)
  
  
set jsonString to current application’s NSString’s stringWithString:resStr
  
set jsonData to jsonString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding)
  
set aJsonDict to current application’s NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value)
  
  
–Get Response Code & Header
  
set dRes to contents of second item of resList
  
if dRes is not equal to missing value then
    set resCode to (dRes’s statusCode()) as number
    
set resHeaders to (dRes’s allHeaderFields()) as record
  else
    set resCode to 0
    
set resHeaders to {}
  end if
  
  
return {json:aJsonDict, responseCode:resCode, responseHeader:resHeaders}
end callRestGETAPIAndParseResults

on retURLwithParams(aBaseURL, aRec)
  set aDic to current application’s NSMutableDictionary’s dictionaryWithDictionary:aRec
  
  
set aKeyList to (aDic’s allKeys()) as list
  
set aValList to (aDic’s allValues()) as list
  
set aLen to length of aKeyList
  
  
set qList to {}
  
repeat with i from 1 to aLen
    set aName to contents of item i of aKeyList
    
set aVal to contents of item i of aValList
    
set the end of qList to (current application’s NSURLQueryItem’s queryItemWithName:aName value:aVal)
  end repeat
  
  
set aComp to current application’s NSURLComponents’s alloc()’s initWithString:aBaseURL
  
aComp’s setQueryItems:qList
  
set aURL to (aComp’s |URL|()’s absoluteString()) as text
  
  
return aURL
end retURLwithParams

on retAccessKey()
  return "xxXxxxXxXXxxxXXXXXXXXXXXXXXxXXXxxxXXxXXxxXXxxxXXXxxXXXX-" –Yahoo! API Key
end retAccessKey

on urlencodeStr(aStr)
  set aString to current application’s NSString’s stringWithString:aStr
  
set aString to (aString’s stringByAddingPercentEncodingWithAllowedCharacters:(current application’s NSCharacterSet’s URLQueryAllowedCharacterSet())) as text
  
return aString
end urlencodeStr

–リストに入れたレコードを、指定の属性ラベルの値で抽出
on filterRecListByLabel1(aRecList as list, aPredicate as string)
  set aArray to current application’s NSArray’s arrayWithArray:aRecList
  
set aPredicate to current application’s NSPredicate’s predicateWithFormat:aPredicate
  
set filteredArray to aArray’s filteredArrayUsingPredicate:aPredicate
  
set bList to filteredArray as list
  
return bList
end filterRecListByLabel1

★Click Here to Open This Script 

Posted in geolocation Internet REST API | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

WordPress XML-RPC Frameworkのじっけん

Posted on 2月 8, 2018 by Takaaki Naganoya

WordPress XML-RPC Frameworkを呼び出して、指定のWordPressとXML-RPCによる通信を行うテスト用のAppleScriptです。

# 本BlogではAppleScriptからの自動更新時以外はWordPressのXML-RPC通信機能をプラグイン「Disable XML-RPC」によって止めているので、本Scriptを実行してもエラーになる可能性があります。他のWordPressのサイトでお試しください

本Blogがホスティング業者との行き違いでデータベースをシャットダウンされて、やむなく再構築を行おうと決意してから迅速に再構築を進めて来られたのは、手元にAppleScriptの元コードがすべて無傷で残っていたことと、これまで半自動だった記事の投稿をすべてAppleScriptから自動化できる目処が立っていたためでした。

・指定のAppleScriptをHTML化(URLリンク付き)するAppleScriptライブラリ
・指定内容のHTMLをWordPressにXML-RPC経由で投稿するAppleScript

といった「飛び道具」を整備することで、指定のAppleScript書類をWordPressに投稿できるようになりました。

とくに、XML-RPCについてはAppleScriptの標準搭載命令「call xmlrpc」がとことん使い物にならず、WordPressへの通信を行うとクラッシュすることを(ずいぶん昔に)確認してあったため、、、かわりになる部品を地道に探してありました。

WordPressとのXML-RPCによる通信を行うmacOS用フレームワーク「wpxmlrpc」を見つけ、Xcode上でビルドしてAppleScriptから呼び出す実験を行い、このように実際の投稿に利用しています。Github上のドキュメントはたいへんに素っ気なく、そのままObjective-CのコードをAppleScriptに置き換えても動作しない程度の素朴すぎる内容でしたが、REST APIの呼び出しAppleScriptを参考に内容を類推してひととおり動作できるところまでこぎつけました。

–> wpxmlrpc.framework

AppleScript名:WordPress XML-RPC Frameworkのじっけん
— Created 2018-02-08 by Takaaki Naganoya
— 2018 Piyomaru Software
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
use framework "wpxmlrpc" –https://github.com/wordpress-mobile/wpxmlrpc

set aRes to callXMLRPC("http://piyocast.com/as/xmlrpc.php", "demo.addTwoNumbers", {2, 3}) of me
–>  5

set aRes to callXMLRPC("http://piyocast.com/as/xmlrpc.php", "mt.supportedMethods", {}) of me
–>  {​​​​​"wp.getUsersBlogs", ​​​​​​​"wp.newPost", ​​​​​​​"wp.editPost", ​​​​​​​"wp.deletePost", ​​​​​​​"wp.getPost", ​​​​​​​"wp.getPosts", ​​​​​​​"wp.newTerm", ​​​​​​​"wp.editTerm", ​​​​​​​"wp.deleteTerm", ​​​​​​​"wp.getTerm", ​​​​​​​"wp.getTerms", ​​​​​​​"wp.getTaxonomy", ​​​​​​​"wp.getTaxonomies", ​​​​​​​"wp.getUser", ​​​​​​​"wp.getUsers", ​​​​​​​"wp.getProfile", ​​​​​​​"wp.editProfile", ​​​​​​​"wp.getPage", ​​​​​​​"wp.getPages", ​​​​​​​"wp.newPage", ​​​​​​​"wp.deletePage", ​​​​​​​"wp.editPage", ​​​​​​​"wp.getPageList", ​​​​​​​"wp.getAuthors", ​​​​​​​"wp.getCategories", ​​​​​​​"wp.getTags", ​​​​​​​"wp.newCategory", ​​​​​​​"wp.deleteCategory", ​​​​​​​"wp.suggestCategories", ​​​​​​​"wp.uploadFile", ​​​​​​​"wp.deleteFile", ​​​​​​​"wp.getCommentCount", ​​​​​​​"wp.getPostStatusList", ​​​​​​​"wp.getPageStatusList", ​​​​​​​"wp.getPageTemplates", ​​​​​​​"wp.getOptions", ​​​​​​​"wp.setOptions", ​​​​​​​"wp.getComment", ​​​​​​​"wp.getComments", ​​​​​​​"wp.deleteComment", ​​​​​​​"wp.editComment", ​​​​​​​"wp.newComment", ​​​​​​​"wp.getCommentStatusList", ​​​​​​​"wp.getMediaItem", ​​​​​​​"wp.getMediaLibrary", ​​​​​​​"wp.getPostFormats", ​​​​​​​"wp.getPostType", ​​​​​​​"wp.getPostTypes", ​​​​​​​"wp.getRevisions", ​​​​​​​"wp.restoreRevision", ​​​​​​​"blogger.getUsersBlogs", ​​​​​​​"blogger.getUserInfo", ​​​​​​​"blogger.getPost", ​​​​​​​"blogger.getRecentPosts", ​​​​​​​"blogger.newPost", ​​​​​​​"blogger.editPost", ​​​​​​​"blogger.deletePost", ​​​​​​​"metaWeblog.newPost", ​​​​​​​"metaWeblog.editPost", ​​​​​​​"metaWeblog.getPost", ​​​​​​​"metaWeblog.getRecentPosts", ​​​​​​​"metaWeblog.getCategories", ​​​​​​​"metaWeblog.newMediaObject", ​​​​​​​"metaWeblog.deletePost", ​​​​​​​"metaWeblog.getUsersBlogs", ​​​​​​​"mt.getCategoryList", ​​​​​​​"mt.getRecentPostTitles", ​​​​​​​"mt.getPostCategories", ​​​​​​​"mt.setPostCategories", ​​​​​​​"mt.supportedMethods", ​​​​​​​"mt.supportedTextFilters", ​​​​​​​"mt.getTrackbackPings", ​​​​​​​"mt.publishPost", ​​​​​​​"pingback.ping", ​​​​​​​"pingback.extensions.getPingbacks", ​​​​​​​"demo.sayHello", ​​​​​​​"demo.addTwoNumbers"​​​​​}

on callXMLRPC(paramURL, aMethod, aParamList)
  set aURL to current application’s |NSURL|’s URLWithString:paramURL
  
set aReq to current application’s NSMutableURLRequest’s alloc()’s initWithURL:aURL
  
aReq’s setHTTPMethod:"POST"
  
  
set encoder to current application’s WPXMLRPCEncoder’s alloc()’s initWithMethod:aMethod andParameters:aParamList
  (
aReq’s setHTTPBody:(encoder’s dataEncodedWithError:(missing value)))
  
  
set aRes to current application’s NSURLConnection’s sendSynchronousRequest:aReq returningResponse:(reference) |error|:(missing value)
  
  
set resList to aRes as list
  
  
set bRes to contents of (first item of resList)
  
set cRes to second item of resList
  
(* (NSHTTPURLResponse) <NSHTTPURLResponse: 0x6000000289a0> { URL: http://piyocast.com/as/xmlrpc.php } { status code: 200, headers {
Connection = close;
"Content-Type" = "text/xml; charset=UTF-8";
Date = "Wed, 07 Feb 2018 16:10:49 GMT";
Server = Apache;
"Transfer-Encoding" = Identity;
} } *)
  
  
set decoder to current application’s WPXMLRPCDecoder’s alloc()’s initWithData:bRes
  
set errF to (decoder’s isFault()) as boolean
  
  
if errF = true then
    —Error
    
set xmlRPCres to faultCode of resStr
    
set xmlRPCbody to faultString of resStr
    
    
return false
  else
    –Success?
    
set xmlRPCres to (decoder’s object()) as list of string or string
    
    
–Error
    
if xmlRPCres = missing value then return false
  end if
  
  
return xmlRPCres
  
end callXMLRPC

★Click Here to Open This Script 

Posted in Internet Network XML-RPC | Tagged 10.11savvy 10.12savvy 10.13savvy | 2 Comments

Post navigation

  • Newer posts

電子書籍(PDF)をオンラインストアで販売中!

Google Search

Popular posts

  • macOS 13, Ventura(継続更新)
  • アラートダイアログ上にWebViewで3Dコンテンツを表示(WebGL+three.js)v3
  • UI Browserがgithub上でソース公開され、オープンソースに
  • macOS 13 TTS Voice環境に変更
  • Xcode 14.2でAppleScript App Templateを復活させる
  • 2022年に書いた価値あるAppleScript
  • ChatGPTで文章のベクトル化(Embedding)
  • 新発売:AppleScriptからSiriを呼び出そう!
  • iWork 12.2がリリースされた
  • 従来と異なるmacOS 13の性格?
  • 新発売:CotEditor Scripting Book with AppleScript
  • macOS 13対応アップデート:AppleScript実践的テクニック集(1)GUI Scripting
  • AS関連データの取り扱いを容易にする(はずの)privateDataTypeLib
  • macOS 13でNSNotFoundバグふたたび
  • macOS 12.5.1、11.6.8でFinderのselectionでスクリーンショット画像をopenできない問題
  • ChatGPTでchatに対する応答文を取得
  • 新発売:iWork Scripting Book with AppleScript
  • Finderの隠し命令openVirtualLocationが発見される
  • macOS 13.1アップデートでスクリプトエディタの挙動がようやくまともに
  • あのコン過去ログビューワー(暫定版)

Tags

10.11savvy (1101) 10.12savvy (1242) 10.13savvy (1390) 10.14savvy (586) 10.15savvy (434) 11.0savvy (277) 12.0savvy (185) 13.0savvy (55) CotEditor (60) Finder (47) iTunes (19) Keynote (98) NSAlert (60) NSArray (51) NSBezierPath (18) NSBitmapImageRep (20) NSBundle (20) NSButton (34) NSColor (51) NSDictionary (27) NSFileManager (23) NSFont (18) NSImage (41) NSJSONSerialization (21) NSMutableArray (62) NSMutableDictionary (21) NSPredicate (36) NSRunningApplication (56) NSScreen (30) NSScrollView (22) NSString (117) NSURL (97) NSURLRequest (23) NSUTF8StringEncoding (30) NSView (33) NSWorkspace (20) Numbers (56) Pages (37) Safari (41) Script Editor (20) WKUserContentController (21) WKUserScript (20) WKUserScriptInjectionTimeAtDocumentEnd (18) WKWebView (23) WKWebViewConfiguration (22)

カテゴリー

  • 2D Bin Packing
  • 3D
  • AirDrop
  • AirPlay
  • Animation
  • AppleScript Application on Xcode
  • beta
  • Bluetooth
  • Books
  • boolean
  • bounds
  • Bug
  • Calendar
  • call by reference
  • Clipboard
  • Code Sign
  • Color
  • Custom Class
  • dialog
  • drive
  • exif
  • file
  • File path
  • filter
  • folder
  • Font
  • Font
  • GAME
  • geolocation
  • GUI
  • GUI Scripting
  • Hex
  • History
  • How To
  • iCloud
  • Icon
  • Image
  • Input Method
  • Internet
  • iOS App
  • JavaScript
  • JSON
  • JXA
  • Keychain
  • Keychain
  • Language
  • Library
  • list
  • Locale
  • Machine Learning
  • Map
  • Markdown
  • Menu
  • Metadata
  • MIDI
  • MIME
  • Natural Language Processing
  • Network
  • news
  • Noification
  • Notarization
  • Number
  • Object control
  • OCR
  • OSA
  • PDF
  • Peripheral
  • PRODUCTS
  • QR Code
  • Raw AppleEvent Code
  • Record
  • rectangle
  • recursive call
  • regexp
  • Release
  • Remote Control
  • Require Control-Command-R to run
  • REST API
  • Review
  • RTF
  • Sandbox
  • Screen Saver
  • Script Libraries
  • sdef
  • search
  • Security
  • selection
  • shell script
  • Shortcuts Workflow
  • Sort
  • Sound
  • Spellchecker
  • Spotlight
  • SVG
  • System
  • Tag
  • Telephony
  • Text
  • Text to Speech
  • timezone
  • Tools
  • Update
  • URL
  • UTI
  • Web Contents Control
  • WiFi
  • XML
  • XML-RPC
  • イベント(Event)
  • 未分類

アーカイブ

  • 2023年9月
  • 2023年8月
  • 2023年7月
  • 2023年6月
  • 2023年5月
  • 2023年4月
  • 2023年3月
  • 2023年2月
  • 2023年1月
  • 2022年12月
  • 2022年11月
  • 2022年10月
  • 2022年9月
  • 2022年8月
  • 2022年7月
  • 2022年6月
  • 2022年5月
  • 2022年4月
  • 2022年3月
  • 2022年2月
  • 2022年1月
  • 2021年12月
  • 2021年11月
  • 2021年10月
  • 2021年9月
  • 2021年8月
  • 2021年7月
  • 2021年6月
  • 2021年5月
  • 2021年4月
  • 2021年3月
  • 2021年2月
  • 2021年1月
  • 2020年12月
  • 2020年11月
  • 2020年10月
  • 2020年9月
  • 2020年8月
  • 2020年7月
  • 2020年6月
  • 2020年5月
  • 2020年4月
  • 2020年3月
  • 2020年2月
  • 2020年1月
  • 2019年12月
  • 2019年11月
  • 2019年10月
  • 2019年9月
  • 2019年8月
  • 2019年7月
  • 2019年6月
  • 2019年5月
  • 2019年4月
  • 2019年3月
  • 2019年2月
  • 2019年1月
  • 2018年12月
  • 2018年11月
  • 2018年10月
  • 2018年9月
  • 2018年8月
  • 2018年7月
  • 2018年6月
  • 2018年5月
  • 2018年4月
  • 2018年3月
  • 2018年2月

https://piyomarusoft.booth.pm/items/301502

メタ情報

  • ログイン
  • 投稿フィード
  • コメントフィード
  • WordPress.org

Forum Posts

  • 人気のトピック
  • 返信がないトピック

メタ情報

  • ログイン
  • 投稿フィード
  • コメントフィード
  • WordPress.org
Proudly powered by WordPress
Theme: Flint by Star Verte LLC