[map]URLの分解 緯度経緯の取得
#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions
property refMe : a reference to current application
################################
######ペーストボードを取得
################################
set ocidPasteboard to refMe's NSPasteboard's generalPasteboard()
#####テキストとして受け取る
set ocidReadPasteboardTypeString to ocidPasteboard's stringForType:(refMe's NSPasteboardTypeString)
set strURL to "https://maps.apple.com/?ll=35.658558,139.745504"
set strURL to "https://www.google.com/maps/@35.6585805,139.7454329,17z/"
################################################
###### URL部分
################################################
set coidURLStr to refMe's NSString's stringWithString:strURL
set coidURL to refMe's NSURL's URLWithString:coidURLStr
set ocidComponents to refMe's NSURLComponents's alloc()'s initWithURL:coidURL resolvingAgainstBaseURL:false
################################################
###### ホスト名取り出し
################################################
set ocidHostName to ocidComponents's |host|()
set strHostName to ocidHostName as text
####処理分岐するなら
if strHostName contains "maps.apple.com" then
log "AppleMap"
set ocidGeoLLArray to doGetLLappleMap(coidURL)
set strLatitude to (ocidGeoLLArray's objectAtIndex:0) as text
set strLongitude to (ocidGeoLLArray's objectAtIndex:1) as text
else if strHostName contains "google.com" then
log "GoogleMap"
set ocidGeoLLArray to doGetLLgooleMap(coidURL)
set strLatitude to (ocidGeoLLArray's objectAtIndex:1) as text
set strLongitude to (ocidGeoLLArray's objectAtIndex:2) as text
else
log "他サービス"
return "緯度経度値を取得できません"
end if
log strLatitude
log strLongitude
################################################
###### サブルーチン
################################################
###################################################
###Googlemap処理
to doGetLLgooleMap(argURL)
################################################
###### URLをコンポーネントに
################################################
set ocidComponents to refMe's NSURLComponents's alloc()'s initWithURL:argURL resolvingAgainstBaseURL:false
################################################
###### パスを取り出し
################################################
set ocidPathComponent to ocidComponents's |path|()
###格納用の可変Array
set ocidPathArray to refMe's NSMutableArray's alloc()'s initWithCapacity:0
###カンマで区切ってArrayにして
set ocidPathArray to ocidPathComponent's componentsSeparatedByString:"/"
###クエリーの数だけ繰り返し
repeat with itemPathArray in ocidPathArray
set strItemPathArray to itemPathArray as text
###対象のパスアイテムを取得
if strItemPathArray starts with "@" then
set strGeoLL to strItemPathArray as text
end if
end repeat
################################################
###### 必要なデータのみ取り出す
################################################
#####区切り文字指定
set ocidLineEndChrSet to refMe's NSCharacterSet's characterSetWithCharactersInString:"@,z"
###テキストを戻して
set ocidGeoLL to refMe's NSString's stringWithString:strGeoLL
###区切り文字でリストに
set ocidGeoLLArray to ocidGeoLL's componentsSeparatedByCharactersInSet:ocidLineEndChrSet
###戻り値はArrayのまま戻す
return ocidGeoLLArray
(* 値を取り出す時は
##値を取り出す
set strLatitude to (ocidGeoLLArray's objectAtIndex:1) as text
set strLongitude to (ocidGeoLLArray's objectAtIndex:2) as text
*)
end doGetLLgooleMap
###################################################
###AppleMap処理
to doGetLLappleMap(argURL)
################################################
###### URLをコンポーネントに
################################################
set ocidComponents to refMe's NSURLComponents's alloc()'s initWithURL:argURL resolvingAgainstBaseURL:false
################################################
###### コンポーネントからクエリーを取り出し
################################################
set ocidURLQueryArray to ocidComponents's queryItems()
################################################
###### クエリーをレコードに
################################################
###格納用の可変ディクショナリ
set ocidQueryDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
###クエリーの数だけ繰り返し
repeat with itemURLQueryArray in ocidURLQueryArray
###KEY
set ocidQueryKey to itemURLQueryArray's |name|()
####値
set ocidQueryValue to itemURLQueryArray's value()
####ディクショナリに格納
(ocidQueryDict's setObject:ocidQueryValue forKey:ocidQueryKey)
end repeat
################################################
###### キーで値を取り出す
################################################
###緯度経度取得
set ocidGeoLL to (ocidQueryDict's valueForKey:"ll")
###カンマで区切ってArrayにして
set ocidGeoLLArray to ocidGeoLL's componentsSeparatedByString:","
###戻り値はArrayのまま戻す
return ocidGeoLLArray
(* ##値を取り出す時は
set strLatitude to (ocidGeoLLArray's objectAtIndex:0) as text
set strLongitude to (ocidGeoLLArray's objectAtIndex:1) as text
*)
end doGetLLappleMap
| 固定リンク
「Map」カテゴリの記事
- [Maps]表示中の位置をブラウザに転送(GoogleMapへ転送する)macOS15対応(2025.03.17)
- [Maps]表示中の位置をブラウザに転送(AppleMap WEB版へ転送する)macOS15対応(2025.03.17)
- [Maps]表示中の位置をQRコードにするmacOS15対応(2025.03.17)
- [XML]位置情報の取得(ip-api.com)(2024.05.17)
- 地名検索を修正しました(2024.01.12)