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

カテゴリー: geolocation

都道府県リストから隣接都道府県を含む該当のコードを抽出する

Posted on 2月 8, 2018 by Takaaki Naganoya

膨大な位置情報の距離を計算するような場合に、対象の都道府県と隣接する都道府県に限定して距離計算を行うことで計算量を削減するためのAppleScriptです。

ある1点の位置情報に対して大量の地点との距離計算を行う処理は割とよくあります。自宅からの最寄駅の計算や、日本全国に点在している施設の最寄駅の計算などなどです。

たいていは、総当たりですべての計算対象との距離計算を行い、近い順でソートすることになります。自分の場合では日本国内の700か所ぐらいの(「戦場の絆」が設置されている)ゲームセンターの位置と日本全国の鉄道の駅(8,000か所程度)を比較して最寄駅を計算しました。

700 x 8,000 = 5,600,000回の距離計算を行うわけで、あまりに時間がかかるのでめまいがします(90分ぐらいかかりました)。1か所あたりの距離計算の所用時間は0.086秒程度なのでいちがいに遅いとはいえません(CocoaのCoreLocationの機能を使わないで、地球を球としてみなして近似的な距離計算を行えば速くなることでしょう)。

ここで、脳みそが頭に入っている人間であれば考えるわけです。もっと速く計算できる方法があるんじゃないか? と。

だいたい、北海道にある地点の最寄駅を求めるのに九州の最果てにある駅との距離計算を行う必要はありません。8,000箇所のすべての駅との距離計算を行う中には、膨大な「無意味な計算」が含まれています。この、「無意味な計算」を除外してあげれば、計算量が半分で済む場合には計算時間は半分になります。

そうやって考えていくと、都道府県レベルで隣接している地点については計算を行う必要がありますが、隣接していない都道府県にある駅との計算は行う意味がないことに気づきます。

そして実際に本テーブルを作成して計算に導入したところ、当初の90分が10分に短縮されました。さらに、データの持ち方を変えて最終的には5分ぐらいまで短縮できました。

「都道府県レベルで計算対象の除外ができて、高速処理を実現できたんだから、市区町村レベルの隣接情報を作って計算すれば、さらに高速化できるのでは?」

と考える人がいるかもしれません。ただ、その処理を実際に頭の中でシミュレーションしてみると、市区町村レベルでは「駅」が存在していないものもあったりで、日本国内に3,0000程度あるとみられるそのレベルの地方公共団体の隣接データの作成は大変です。たしかに高速化できるかもしれませんが、その成果は微々たるものになることでしょう(東京、名古屋、大阪などの大都市にかぎっては効果があるかも? その他の地域ではさほど効果はありません)。

対象は日本国内の都道府県。海外の住所情報でも同様に都道府県にコードを振って隣接情報をデータ化すれば同様の処理は可能です。

# BridgePlus Script Libraryを併用しなくてもすむ v2を追加掲載しておきました

AppleScript名:都道府県リストから隣接都道府県を含む該当のコードを抽出する
— Created 2015-12-06 by Takaaki Naganoya
— 2015 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use BridgePlus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html

property prefList : {{prefCode:1, prefName:"北海道", neighbors:{}}, {prefCode:2, prefName:"青森県", neighbors:{3, 5}}, {prefCode:3, prefName:"岩手県", neighbors:{2, 4, 5}}, {prefCode:4, prefName:"宮城県", neighbors:{3, 5, 6, 7}}, {prefCode:5, prefName:"秋田県", neighbors:{2, 3, 4, 6}}, {prefCode:6, prefName:"山形県", neighbors:{3, 4, 5, 7, 15}}, {prefCode:7, prefName:"福島県", neighbors:{4, 6, 8, 9, 10, 15}}, {prefCode:8, prefName:"茨城県", neighbors:{7, 9, 10, 11, 12}}, {prefCode:9, prefName:"栃木県", neighbors:{7, 8, 10, 11, 12}}, {prefCode:10, prefName:"群馬県", neighbors:{7, 9, 11, 15, 20}}, {prefCode:11, prefName:"埼玉県", neighbors:{8, 9, 10, 12, 13, 19, 20}}, {prefCode:12, prefName:"千葉県", neighbors:{8, 11, 13}}, {prefCode:13, prefName:"東京都", neighbors:{11, 12, 19, 14}}, {prefCode:14, prefName:"神奈川県", neighbors:{13, 19, 22}}, {prefCode:15, prefName:"新潟県", neighbors:{6, 7, 10, 16, 20}}, {prefCode:16, prefName:"富山県", neighbors:{15, 17, 20, 21}}, {prefCode:17, prefName:"石川県", neighbors:{16, 18, 21}}, {prefCode:18, prefName:"福井県", neighbors:{17, 21, 25, 26}}, {prefCode:19, prefName:"山梨県", neighbors:{11, 13, 14, 20, 22}}, {prefCode:20, prefName:"長野県", neighbors:{10, 11, 15, 16, 19, 21, 22, 23}}, {prefCode:21, prefName:"岐阜県", neighbors:{16, 17, 18, 20, 23, 24, 25}}, {prefCode:22, prefName:"静岡県", neighbors:{14, 19, 20, 23}}, {prefCode:23, prefName:"愛知県", neighbors:{20, 21, 22, 24}}, {prefCode:24, prefName:"三重県", neighbors:{21, 23, 25, 26, 29}}, {prefCode:25, prefName:"滋賀県", neighbors:{18, 21, 24, 26}}, {prefCode:26, prefName:"京都府", neighbors:{18, 24, 25, 27, 28, 29}}, {prefCode:27, prefName:"大阪府", neighbors:{26, 29, 28, 30}}, {prefCode:28, prefName:"兵庫県", neighbors:{26, 27, 31, 33}}, {prefCode:29, prefName:"奈良県", neighbors:{24, 25, 26, 27, 30}}, {prefCode:30, prefName:"和歌山県", neighbors:{24, 27, 29}}, {prefCode:31, prefName:"鳥取県", neighbors:{28, 33, 32, 34}}, {prefCode:32, prefName:"島根県", neighbors:{31, 34, 35}}, {prefCode:33, prefName:"岡山県", neighbors:{28, 31, 34}}, {prefCode:34, prefName:"広島県", neighbors:{33, 31, 32, 35}}, {prefCode:35, prefName:"山口県", neighbors:{32, 34}}, {prefCode:36, prefName:"徳島県", neighbors:{37, 39}}, {prefCode:37, prefName:"香川県", neighbors:{36, 38, 39}}, {prefCode:38, prefName:"愛媛県", neighbors:{37, 39}}, {prefCode:39, prefName:"高知県", neighbors:{36, 37, 38}}, {prefCode:40, prefName:"福岡県", neighbors:{44, 43, 41}}, {prefCode:41, prefName:"佐賀県", neighbors:{40, 42}}, {prefCode:42, prefName:"長崎県", neighbors:{41}}, {prefCode:43, prefName:"熊本県", neighbors:{40, 42, 44, 45, 46}}, {prefCode:44, prefName:"大分県", neighbors:{40, 43, 45}}, {prefCode:45, prefName:"宮崎県", neighbors:{43, 44, 46}}, {prefCode:46, prefName:"鹿児島県", neighbors:{43, 45}}, {prefCode:47, prefName:"沖縄県", neighbors:{}}}

set aPref to 13 –Tokyo
set aRes to my filterRecListByLabel2(prefList, "prefCode == [c]%@", {aPref})
set targList to neighbors of aRes & aPref
–>  {​​​​​11, ​​​​​12, ​​​​​19, ​​​​​14, ​​​​​13​​​}–Saitama, Chiba, yamanashi, Kanagawa, Tokyo

–リストに入れたレコードを、指定の属性ラベルの値で抽出(predicateとパラメータを分離)し、1つのアイテムだけを返す
on filterRecListByLabel2(aRecList as list, aPredicate as string, aParam)
  set aArray to current application’s NSArray’s arrayWithArray:aRecList
  
set aPredicate to current application’s NSPredicate’s predicateWithFormat:aPredicate argumentArray:aParam
  
set filteredArray to aArray’s filteredArrayUsingPredicate:aPredicate
  
set bList to ASify from filteredArray as list
  
set cList to first item of bList
  
return cList
end filterRecListByLabel2

★Click Here to Open This Script 

AppleScript名:都道府県リストから隣接都道府県を含む該当のコードを抽出する v2.scpt
— Created 2015-12-06 by Takaaki Naganoya
— 2015 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
—BridgePlusを使わなくていいように書き換え

property prefList : {{prefCode:1, prefName:"北海道", neighbors:{}}, {prefCode:2, prefName:"青森県", neighbors:{3, 5}}, {prefCode:3, prefName:"岩手県", neighbors:{2, 4, 5}}, {prefCode:4, prefName:"宮城県", neighbors:{3, 5, 6, 7}}, {prefCode:5, prefName:"秋田県", neighbors:{2, 3, 4, 6}}, {prefCode:6, prefName:"山形県", neighbors:{3, 4, 5, 7, 15}}, {prefCode:7, prefName:"福島県", neighbors:{4, 6, 8, 9, 10, 15}}, {prefCode:8, prefName:"茨城県", neighbors:{7, 9, 10, 11, 12}}, {prefCode:9, prefName:"栃木県", neighbors:{7, 8, 10, 11, 12}}, {prefCode:10, prefName:"群馬県", neighbors:{7, 9, 11, 15, 20}}, {prefCode:11, prefName:"埼玉県", neighbors:{8, 9, 10, 12, 13, 19, 20}}, {prefCode:12, prefName:"千葉県", neighbors:{8, 11, 13}}, {prefCode:13, prefName:"東京都", neighbors:{11, 12, 19, 14}}, {prefCode:14, prefName:"神奈川県", neighbors:{13, 19, 22}}, {prefCode:15, prefName:"新潟県", neighbors:{6, 7, 10, 16, 20}}, {prefCode:16, prefName:"富山県", neighbors:{15, 17, 20, 21}}, {prefCode:17, prefName:"石川県", neighbors:{16, 18, 21}}, {prefCode:18, prefName:"福井県", neighbors:{17, 21, 25, 26}}, {prefCode:19, prefName:"山梨県", neighbors:{11, 13, 14, 20, 22}}, {prefCode:20, prefName:"長野県", neighbors:{10, 11, 15, 16, 19, 21, 22, 23}}, {prefCode:21, prefName:"岐阜県", neighbors:{16, 17, 18, 20, 23, 24, 25}}, {prefCode:22, prefName:"静岡県", neighbors:{14, 19, 20, 23}}, {prefCode:23, prefName:"愛知県", neighbors:{20, 21, 22, 24}}, {prefCode:24, prefName:"三重県", neighbors:{21, 23, 25, 26, 29}}, {prefCode:25, prefName:"滋賀県", neighbors:{18, 21, 24, 26}}, {prefCode:26, prefName:"京都府", neighbors:{18, 24, 25, 27, 28, 29}}, {prefCode:27, prefName:"大阪府", neighbors:{26, 29, 28, 30}}, {prefCode:28, prefName:"兵庫県", neighbors:{26, 27, 31, 33}}, {prefCode:29, prefName:"奈良県", neighbors:{24, 25, 26, 27, 30}}, {prefCode:30, prefName:"和歌山県", neighbors:{24, 27, 29}}, {prefCode:31, prefName:"鳥取県", neighbors:{28, 33, 32, 34}}, {prefCode:32, prefName:"島根県", neighbors:{31, 34, 35}}, {prefCode:33, prefName:"岡山県", neighbors:{28, 31, 34}}, {prefCode:34, prefName:"広島県", neighbors:{33, 31, 32, 35}}, {prefCode:35, prefName:"山口県", neighbors:{32, 34}}, {prefCode:36, prefName:"徳島県", neighbors:{37, 39}}, {prefCode:37, prefName:"香川県", neighbors:{36, 38, 39}}, {prefCode:38, prefName:"愛媛県", neighbors:{37, 39}}, {prefCode:39, prefName:"高知県", neighbors:{36, 37, 38}}, {prefCode:40, prefName:"福岡県", neighbors:{44, 43, 41}}, {prefCode:41, prefName:"佐賀県", neighbors:{40, 42}}, {prefCode:42, prefName:"長崎県", neighbors:{41}}, {prefCode:43, prefName:"熊本県", neighbors:{40, 42, 44, 45, 46}}, {prefCode:44, prefName:"大分県", neighbors:{40, 43, 45}}, {prefCode:45, prefName:"宮崎県", neighbors:{43, 44, 46}}, {prefCode:46, prefName:"鹿児島県", neighbors:{43, 45}}, {prefCode:47, prefName:"沖縄県", neighbors:{}}}

set aPref to 13 –Tokyo
set aRes to my filterRecListByLabel2(prefList, "prefCode == [c]%@", {aPref})
set targList to neighbors of aRes & aPref
–>  {​​​​​11, ​​​​​12, ​​​​​19, ​​​​​14, ​​​​​13​​​}–Saitama, Chiba, yamanashi, Kanagawa, Tokyo

–リストに入れたレコードを、指定の属性ラベルの値で抽出(predicateとパラメータを分離)し、1つのアイテムだけを返す
on filterRecListByLabel2(aRecList as list, aPredicate as string, aParam)
  set aArray to current application’s NSArray’s arrayWithArray:aRecList
  
set aPredicate to current application’s NSPredicate’s predicateWithFormat:aPredicate argumentArray:aParam
  
set filteredArray to aArray’s filteredArrayUsingPredicate:aPredicate
  
set bList to filteredArray as list
  
set cList to first item of bList
  
return cList
end filterRecListByLabel2

★Click Here to Open This Script 

Posted in geolocation list | Tagged 10.11savvy 10.12savvy 10.13savvy | 1 Comment

2点間の距離を求める v3

Posted on 2月 8, 2018 by Takaaki Naganoya

Core Locationを用いた地球上の2点間の距離計算のAppleScriptです。

地球は楕円体であるため、その楕円率を考慮して距離計算する必要があります。Core Locationには楕円率を考慮した計算が行われていることを期待しています。

完全球体として2点間の距離を計算すると近似値になってしまうものの、(東京近郊の2点の計算時)実際の計算結果を比較すると、4.2kmの距離計算時に1メートル程度の誤差としてしか表れてこないため、計算精度をさほど求められない用途においては、地球を完全球体として扱った近似計算でも十分に有用なケースがあります。

東京-鹿児島間で距離計算したときに、

  A. 楕円率を考慮して計算:960392.341424メートル
  B. 完全球体として近似計算:960581.2302612メートル
  誤差(A.- B.):189メートル

ぐらいの誤差です。日本国内の1点からの最寄りの施設を計算するぐらいなら完全球体として計算しても大丈夫でしょう。処理時間とか計算対象の座標数を考慮し、用途に応じて選択するとよいでしょう。

追記:
気になったので、CoreLocationで距離を求めるタイプ(First)と、完全球体で近似計算を行うタイプ(Second)のルーチンで1万回ループで実行して、実行時間を求めてみました。

MacBookPro10,1,  macOS Version 10.14.6 (Build 18G6006),  10000 iterations
         First Run   Total Time    Average  
First        0.005       11.614      0.001
Second       0.001        2.958      0.000
Ratio (excluding first run): 3.93:1

結果は、予想どおりCoreLocationで行うルーチン(本Script)の方が約4倍低速でした。ただし、実際の処理に組み込んでこの通りにならないケースもあるので、実際の処理で計測してどちらを用いるべきかを判断することになるでしょう。

AppleScript名:2点間の距離を求める v3
— Created 2015-03-03 by Shane Stanley
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "CoreLocation"

set aPlace to current application’s CLLocation’s alloc()’s initWithLatitude:35.737072 longitude:139.637826
set bPlace to current application’s CLLocation’s alloc()’s initWithLatitude:35.753108 longitude:139.595123
set distanceInMetres to aPlace’s distanceFromLocation:bPlace
–>  4252.71123319014

★Click Here to Open This Script 

Posted in geolocation | Tagged 10.11savvy 10.12savvy 10.13savvy | 1 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

Maps.appで開始地点と到着地点の住所を指定して経路表示 v3

Posted on 2月 6, 2018 by Takaaki Naganoya
AppleScript名:Maps.appで開始地点と到着地点の住所を指定して経路表示 v3
— Created 2017-01-17 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aURL to "http://maps.apple.com/"
set aRec to {saddr:"中村橋駅", daddr:"よみうりホール"} –経路表示
set bURL to retURLwithParams(aURL as string, aRec as record)
open location bURL

on retURLwithParams(aBaseURL as string, aRec as record)
  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 bLen to length of aValList
  
if aLen is not equal to bLen then return false
  
  
set qList to {}
  
repeat with i from 1 to aLen
    set aName to (contents of item i of aKeyList) as string
    
set aVal to (contents of item i of aValList) as string
    
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

★Click Here to Open This Script 

com.apple.Maps

Posted in geolocation URL | Tagged 10.11savvy 10.12savvy 10.13savvy Maps | Leave a comment

Maps.appで住所検索表示 v4

Posted on 2月 6, 2018 by Takaaki Naganoya
AppleScript名:Map.appで住所検索表示 v4
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aBaseURL to "http://maps.apple.com/"

set aParam to "東京都港区六本木6丁目10番1号" –Apple Japanの住所
set aRec to {q:aParam}
set cURL to retURLwithParams(aBaseURL, aRec) of me

tell application "Maps"
  open location cURL
end tell

on retURLwithParams(aBaseURL as string, aRec as record)
  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 bLen to length of aValList
  
if aLen is not equal to bLen then return false
  
  
set qList to {}
  
repeat with i from 1 to aLen
    set aName to (contents of item i of aKeyList) as string
    
set aVal to (contents of item i of aValList) as string
    
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

★Click Here to Open This Script 

com.apple.Maps

Posted in geolocation URL | Tagged 10.11savvy 10.12savvy 10.13savvy Maps | Leave a comment

Maps.appで緯度経度指定表示 v3

Posted on 2月 6, 2018 by Takaaki Naganoya
AppleScript名:Map.appで緯度経度指定表示 v3
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aURL to "http://maps.apple.com/?ll=41.82459,140.7174504&t=m" –ラウンドワンスタジアム函館

open location aURL

★Click Here to Open This Script 

com.apple.Maps

Posted in geolocation | Tagged 10.11savvy 10.12savvy 10.13savvy Maps | 1 Comment

Post navigation

  • Newer posts

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

Google Search

Popular posts

  • アラートダイアログ上にWebViewで3Dコンテンツを表示(WebGL+three.js)v3
  • Xcode 14.2でAppleScript App Templateを復活させる
  • 2022年に書いた価値あるAppleScript
  • ChatGPTで文章のベクトル化(Embedding)
  • ChatGPTでchatに対する応答文を取得
  • macOS 14, Sonoma
  • Dockアイコンにプログレスバーを追加 v3
  • 画像をExcelのワークシート上に配置
  • 新発売:Mail.app Scripting Book with AppleScript
  • 指定のアプリケーションの実行アーキテクチャを変更
  • 出るか?「AppleScript最新リファレンス」のバージョン2.8対応版
  • 指定画像をbase64エンコード文字列に変換→デコード
  • HexDump to BASIC
  • 当分、macOS 14へのアップデートを見送ります
  • macOS 14の変更がmacOS 13にも反映
  • 新刊発売 AppleScript最新リファレンス v2.8対応
  • SHARP MZ MML再生_アルハンブラ_mono
  • Claris FileMaker 2023がリリースされる
  • macOS 13の複合的な不具合。とくにPDF書き出しについて
  • AppleScriptによるWebブラウザ自動操縦ガイドをmacOS 13対応アップデート

Tags

10.11savvy (1101) 10.12savvy (1242) 10.13savvy (1390) 10.14savvy (586) 10.15savvy (434) 11.0savvy (278) 12.0savvy (190) 13.0savvy (74) 14.0savvy (19) CotEditor (61) Finder (48) iTunes (19) Keynote (100) 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 (57) Pages (38) Safari (41) Script Editor (22) WKUserContentController (21) WKUserScript (20) WKWebView (23) WKWebViewConfiguration (22)

カテゴリー

  • 2D Bin Packing
  • 3D
  • AirDrop
  • AirPlay
  • Animation
  • AppleScript Application on Xcode
  • Beginner
  • 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)
  • 未分類

アーカイブ

  • 2024年1月
  • 2023年12月
  • 2023年11月
  • 2023年10月
  • 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