« [NSDateFormatter]日付時刻のフォーマット | トップページ | [NSMutableDictionary]可変レコード »

[Map]位置情報をQRコードにするv2

[Map]位置情報をQRコードにする
https://quicktimer.cocolog-nifty.com/icefloe/2022/04/post-af06ee.html
こちらのアップデートです。
現時点ではここまでにしょうか…と
NSMutableDictionaryを使ってみたものの…
処理は煩雑になるわ、テキスト形式に戻すのに記述が増えるは…で
あまり良い事なかった。
1:便利な機能は使っていく
2:記述にこだわらず、可読性や処理のシンプルさが結局後で効く

こだわらずに、最適と思われる方法にすればよかったなぁ…遠い目
(読みにくいわ…修正しにくいわで、最低な出来だと…トホホ)

ダウンロード - geo2qr4mapv2.applescript.zip




#!/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 framework "CoreImage"
use scripting additions

property objMe : a reference to current application
property objNSString : a reference to objMe's NSString
property objNSURL : a reference to objMe's NSURL
##
property objNSDictionary : a reference to objMe's NSDictionary
property objNSMutableDictionary : a reference to objMe's NSMutableDictionary
property objNSArray : a reference to objMe's NSArray
property objNSMutableArray : a reference to objMe's NSMutableArray

set objFileManager to objMe's NSFileManager's defaultManager()

###リセット
set theURL to "" as text
set ocidNSInlineData to "" as text
set ocidNSBitmapImagePadRep to "" as text

tell application "Finder"
set the clipboard to the theURL as string
end tell

objMe's NSLog("■:osascript:Start Script Geo2Qr4Map")

###コピーのサブへ
doCopyMap()

log "theURL:" & theURL
objMe's NSLog("■:osascript:コピー OK" & theURL & "")
###クリップボードからURLを取得する
tell application "Finder"
set theURL to (the clipboard) as text
end tell
delay 0.5

##################################################
objMe's NSLog("■:osascript: theURL" & theURL & "")
set strURL to theURL as text

##################################################
if strURL is "" then
###URLの取得に失敗しているパターン
set aliasIconPath to POSIX file "/System/Applications/Maps.app/Contents/Resources/AppIcon.icns" as alias
set strDefaultAnswer to "https://maps.apple.com/?ll=35.658558,139.745504" as text
try
set objResponse to (display dialog "URLの取得に失敗しました" with title "QRテキスト" default answer strDefaultAnswer buttons {"OK", "キャンセル"} default button "OK" cancel button "キャンセル" with icon aliasIconPath giving up after 20 without hidden answer)
on error
log "エラーしました"
return
end try
if true is equal to (gave up of objResponse) then
return "時間切れですやりなおしてください"
end if
if "OK" is equal to (button returned of objResponse) then
set strURL to (text returned of objResponse) as text
else
return "キャンセル"
end if
end if

############################
##値がコピー出来なかったときエラーになるので
##ここはトライ
try
####################################
###URLNSURLに格納
set ocidURL to objNSURL's alloc's initWithString:strURL
log className() of ocidURL as text
--> NSURL
####################################
###クエリー部を取り出し
set ocidQueryUrl to ocidURL's query
log className() of ocidQueryUrl as text
--> __NSCFString
log ocidQueryUrl as text
on error
###エラーしたらコピー取り直し
doCopyMap()
tell application "Finder"
set theURL to (the clipboard) as text
end tell
####################################
###URLNSURLに格納
set ocidURL to objNSURL's alloc's initWithString:strURL
log className() of ocidURL as text
--> NSURL
####################################
###クエリー部を取り出し
set ocidQueryUrl to ocidURL's query
log className() of ocidQueryUrl as text
--> __NSCFString
log ocidQueryUrl as text
end try

####################################
###取り出したクエリを&を区切り文字でリストに
set ocidArrayComponent to (ocidQueryUrl's componentsSeparatedByString:"&")
log className() of ocidArrayComponent as text
--> __NSArrayM
log ocidArrayComponent as list

####################################
####可変レコードを作成
set ocidRecordQuery to objNSMutableDictionary's alloc()'s init()
####値が空のレコードを定義
set recordQuery to {t:"", q:"", address:"", near:"", ll:"", z:"18", spn:"", saddr:"", daddr:"", dirflg:"", sll:"", sspn:"", ug:""} as record
####↑の定義で値が空の可変レコードを作成
set ocidRecordQuery to objNSMutableDictionary's dictionaryWithDictionary:recordQuery
####################################
###ここからクエリー分繰り返し
repeat with objArrayComponent in ocidArrayComponent
#####渡されたクエリーを=を境に分割してArray=リストにする
set ocidArrayFirstObject to (objArrayComponent's componentsSeparatedByString:"=")

####まずは順番に キー と 値 で格納
set ocidKey to item 1 of ocidArrayFirstObject
log ocidKey as text

set ocidValue to item 2 of ocidArrayFirstObject
log ocidValue as text

#########位置情報 緯度 経度
if (ocidKey as text) = "ll" then
set ll of ocidRecordQuery to ocidValue
####カンマでわけて緯度 経度に
set ocidArrayLL to (ocidValue's componentsSeparatedByString:",")
log ocidArrayLL as list
###最初の項目
set ocidLatitude to item 1 of ocidArrayLL
log "Latitude:" & ocidLatitude as text
###あとの項目
set ocidLongitude to item 2 of ocidArrayLL
log "Longitude:" & ocidLongitude as text

#########Address String 住所
else if (ocidKey as text) = "address" then
set address of ocidRecordQuery to ocidValue
set ocidAddEnc to ocidValue's stringByRemovingPercentEncoding
log "AddressString:" & ocidAddEnc as text

#########The zoom level. 高さ方向
else if (ocidKey as text) = "z" then
set z of ocidRecordQuery to ocidValue
set ocidZoomValue to ocidValue
log "ZoomValue:" & ocidZoomValue

#########マップビュー
else if (ocidKey as text) = "t" then
set t of ocidRecordQuery to ocidValue
set ocidMapType to ocidValue
log "MapType:" & ocidMapType
(*
m (standard view)
k (satellite view)
h (hybrid view)
r (transit view)
goole
map_action=pano
map_action=map
basemap=satellite
terrain
roadmap
*)
####################################
#########dirflg 移動方法
else if (ocidKey as text) = "dirflg" then
set dirflg of ocidRecordQuery to ocidValue
set ocidDirflgType to ocidValue
log "DirflgType:" & ocidDirflgType
(*
d (by car)
w (by foot)
r (by public transit)

goole
travelmode=driving
walking
transit
*)

#########Dirflg Parameters 出発点
else if (ocidKey as text) = "saddr" then
set saddr of ocidRecordQuery to ocidValue
set strSaddrEnc to ocidValue as text
set ocidSaddrEnc to ocidValue's stringByRemovingPercentEncoding
log "StartingPoint:" & ocidSaddrEnc as text

#########Destination 到着店
else if (ocidKey as text) = "daddr" then
set daddr of ocidRecordQuery to ocidValue
set strDaddrEnc to ocidValue as text
set ocidDaddrEnc to ocidValue's stringByRemovingPercentEncoding
log "DestinationPoint:" & ocidDaddrEnc as text

#########Search Query 検索語句
else if (ocidKey as text) = "q" then
set q of ocidRecordQuery to ocidValue
set strRecordQuery to ocidValue as text
set ocidSearchQueryEnc to ocidValue's stringByRemovingPercentEncoding
log "SearchQuery" & ocidSearchQueryEnc as text

####################################
#########語句検索時の周辺情報の有無による分岐

else if (ocidKey as text) = "sll" then
set sll of ocidRecordQuery to ocidValue
####カンマでわけて緯度 経度に
set ocidSearchArrayLL to (ocidValue's componentsSeparatedByString:",")
log ocidSearchArrayLL as list
####最初の項目
set ocidNearLatitude to item 1 of ocidSearchArrayLL
log "NearLatitude:" & ocidNearLatitude as text
####あとの項目
set ocidNearLongitude to item 2 of ocidSearchArrayLL
log "NearNearLongitude:" & ocidNearLongitude as text

else if (ocidKey as text) = "spn" then
####周囲情報の範囲
set spn of ocidRecordQuery to ocidValue


####################################
#########during search周辺 位置情報 緯度 経度

else if (ocidKey as text) = "near" then
set near of ocidRecordQuery to ocidValue
####カンマでわけて緯度 経度に
set ocidNearArrayLL to (ocidValue's componentsSeparatedByString:",")
log ocidNearArrayLL as list
###最初の項目
set ocidNearLatitude to item 1 of ocidNearArrayLL
log "NearLatitude:" & ocidNearLatitude as text
###あとの項目
set ocidNearLongitude to item 2 of ocidNearArrayLL
log "NearNearLongitude:" & ocidNearLongitude as text

####################################
#########ガイド時のug
else if (ocidKey as text) = "ug" then
set ug of ocidRecordQuery to ocidValue

end if

end repeat

log ocidRecordQuery as record
objMe's NSLog("■:osascript: RecordQuery Done")
#####################################################
#####QRコード保存要ファイル名
if (q of ocidRecordQuery as text) is not "" then
###検索語句あり
if (count of (ocidSearchQueryEnc as text)) < 8 then
set strQueryEnc to (ocidSearchQueryEnc as text) as text
else
set strQueryEnc to characters 1 thru 8 of (ocidSearchQueryEnc as text) as text
end if
set strFileName to ("" & strQueryEnc & ".png")
objMe's NSLog("■:osascript: ocidSearchQueryEnc " & (ocidSearchQueryEnc as text) & "")

else if (daddr of ocidRecordQuery as text) is not "" then
###行き先語句あり
set strDaddrEnc to characters 1 thru 8 of (ocidDaddrEnc as text) as text
set strFileName to ("" & strDaddrEnc & ".png")
objMe's NSLog("■:osascript: ocidDaddrEnc " & (ocidDaddrEnc as text) & "")
else
###語句の無い場合は日付をファイル名にする
set strDateFormat to "yyyyMMMMddhhmm" as text
set ocidForMatter to objMe's NSDateFormatter's alloc()'s init()
ocidForMatter's setLocale:(objMe's NSLocale's localeWithLocaleIdentifier:"ja_JP_POSIX")
ocidForMatter's setDateFormat:(strDateFormat)
set objDate to (current date)
set strDateTime to (ocidForMatter's stringFromDate:(objDate)) as text
set strFileName to ("" & strDateTime & ".png")
end if

####################################################
#########対応アプリの分岐
###ホスト名を取り出し
set ocidHostUrl to ocidURL's |host|
log className() of ocidHostUrl as text
--> __NSCFString
log ocidHostUrl as text
####################################
###ホストによる分岐
if (ocidHostUrl as text) is "guides.apple.com" then
-->このままの値でバーコードを作成する
set listButtonAset to {"AppleMapガイド用"} as list
set strAlertMes to "ガイドリンクはAppleMap専用です"

else if (ocidHostUrl as text) is "collections.apple.com" then
-->コレクションは現在は『ガイド』になった?
set listButtonAset to {"AppleMapガイド用"} as list
set strAlertMes to "ガイドリンクはAppleMap専用です"

else if (ocidHostUrl as text) is "maps.apple.com" then
-->ガイド以外
-->処理する
####################################
###内容によっての分岐
if (ll of ocidRecordQuery as text) is not "" then
######緯度経度がある場合
set listButtonAset to {"AppleMap", "GoogleMap", "GeoQR"} as list
set strAlertMes to "GeoQRは対応していない機種やアプリがあります"

else if (q of ocidRecordQuery as text) is not "" then
######検索語句がある場合
set listButtonAset to {"AppleMap", "GoogleMap", "汎用"} as list
set strAlertMes to "iOSAppleMapQRコードを作成する OR 一般的なQRコードを作成する"

else if (daddr of ocidRecordQuery as text) is not "" then
set listButtonAset to {"AppleMap経路", "GoogleMap経路"} as list
set strAlertMes to "経路情報用のMapになります"

else
######緯度経度無し検索語句無しだとAppleMapのみ
set listButtonAset to {"AppleMap用のみ"} as list
set strAlertMes to "ガイドリンクはAppleMap専用です"

end if
end if
##############################################
log listButtonAset
try
set objAns to (display alert "どちら用のQRコードを作成しますか?" message strAlertMes default button 1 buttons listButtonAset)
on error
log "エラーしました"
return
end try

set objResponse to (button returned of objAns) as text
##############################################

if objResponse is "GeoQR" then
### 緯度経度でGEOバーコード 対応機種に制限がある場合あり
set theChl to ("GEO:" & (ocidLatitude as text) & "," & (ocidLongitude as text) & "," & (z of ocidRecordQuery as text) & "") as text
###経路情報 Map
else if objResponse is "AppleMap経路" then
set theChl to ("http://maps.apple.com/?daddr=" & (ocidDaddrEnc as text) & "&saddr=" & (ocidSaddrEnc as text) & "") as text
###経路情報 Google
else if objResponse is "GoogleMap経路" then
set theChl to ("https://www.google.com/maps/?daddr=" & (ocidDaddrEnc as text) & "&saddr=" & (ocidSaddrEnc as text) & "") as text
####AppleMapのガイドリンク
else if objResponse is "AppleMapガイド用" then
set theChl to ("" & strURL & "") as text

else if objResponse is "GoogleMap" then
##############################################
###GoogleMap用の小数点以下の桁揃え
###逆ゼロサプレス
set theLatitude to (ocidLatitude as text)
set AppleScript's text item delimiters to "."
set listLatitude to every text item of theLatitude as list
set AppleScript's text item delimiters to ""
set strLatitudeInt to text item 1 of listLatitude as text
set strLatitudeDecimal to text item 2 of listLatitude as text
set strLatitudeDecimal to (text 1 through 7 of (strLatitudeDecimal & "000000000")) as text
set theLatitude to ("" & strLatitudeInt & "." & strLatitudeDecimal & "")

set theLongitude to (ocidLongitude as text)
set AppleScript's text item delimiters to "."
set listLongitude to every text item of theLongitude as list
set AppleScript's text item delimiters to ""
set strLongitudeInt to text item 1 of listLongitude as text
set strLongitudeDecimal to text item 2 of listLongitude as text
set strLongitudeDecimal to (text 1 through 7 of (strLongitudeDecimal & "000000000")) as text
set theLongitude to ("" & strLongitudeInt & "." & strLongitudeDecimal & "")

set theGooglemapParts to ("@" & theLatitude & "," & theLongitude & "," & (z of ocidRecordQuery as text) & "z")

set theChl to ("https://www.google.com/maps/" & theGooglemapParts & "") as text
else
set theChl to ("http://maps.apple.com/?q=" & (ocidLatitude as text) & "," & (ocidLongitude as text) & "," & (z of ocidRecordQuery as text) & "z") as text
end if


##############################################
log theChl

###フォルダのパス
###ピクチャーフォルダのパス
tell application "Finder"
set aliasDirPath to (path to pictures folder from user domain) as alias
set strDirPath to POSIX path of aliasDirPath as text
end tell
####フォルダ名
set strFolderName to "QRcode" as text
set strDirPath to ("" & strDirPath & strFolderName & "") as text
###NSStringテキスト
set ocidDirPath to objNSString's stringWithString:strDirPath
###フォルダを作る
###途中のフォルダも作る-->true
set boolMakeNewFolder to (objFileManager's createDirectoryAtPath:ocidDirPath withIntermediateDirectories:true attributes:(missing value) |error|:(missing value))

####ファイルパス
set strSaveFilePath to ("" & strDirPath & "/" & strFileName & "") as text

objMe's NSLog("■:osascript: strSaveFilePath" & strSaveFilePath & "")

##############################################
set strInputText to theChl as text
####テキストをNSString
set ocidInputString to objNSString's stringWithString:strInputText
####テキストをUTF8
set ocidUtf8InputString to ocidInputString's dataUsingEncoding:(objMe's NSUTF8StringEncoding)
####CIQRCodeGenerator初期化
set ocidQRcodeImage to objMe's CIFilter's filterWithName:"CIQRCodeGenerator"
ocidQRcodeImage's setDefaults()
###テキスト設定
ocidQRcodeImage's setValue:ocidUtf8InputString forKey:"inputMessage"
###読み取り誤差値設定L, M, Q, H
ocidQRcodeImage's setValue:"Q" forKey:"inputCorrectionLevel"
###QRコード本体のイメージ
set ocidCIImage to ocidQRcodeImage's outputImage()
-->ここで生成されるのはQRのセルが1x1pxの最小サイズ
###QRコードの縦横取得
set ocidCIImageDimension to ocidCIImage's extent()
set ocidCIImageWidth to (item 1 of item 2 of ocidCIImageDimension) as integer
set ocidCIImageHight to (item 2 of item 2 of ocidCIImageDimension) as integer
###最終的に出力したいpxサイズ
set numScaleMax to 720
###整数で拡大しないとアレなのでの値のニアなサイズになります
set numWidth to ((numScaleMax / ocidCIImageWidth) div 1) as integer
set numHight to ((numScaleMax / ocidCIImageHight) div 1) as integer
###↑サイズの拡大縮小する場合はここで値を調整すれば良い
####変換スケール作成-->拡大
set recordScalse to objMe's CGAffineTransform's CGAffineTransformMakeScale(numWidth, numHight)
##変換スケールを適応(元のサイズに元のサイズのスケール適応しても意味ないけど
set ocidCIImageScaled to ocidCIImage's imageByApplyingTransform:recordScalse
#######元のセルが1x1pxの最小サイズで出したいときはここで処理
##set ocidCIImageScaled to ocidCIImage
###イメージデータを展開
set ocidNSCIImageRep to objMe's NSCIImageRep's imageRepWithCIImage:ocidCIImageScaled
###出力用のイメージの初期化
set ocidNSImageScaled to objMe's NSImage's alloc()'s initWithSize:(ocidNSCIImageRep's |size|())
###イメージデータを合成
ocidNSImageScaled's addRepresentation:ocidNSCIImageRep
###出来上がったデータはOS_dispatch_data
set ocidOsDispatchData to ocidNSImageScaled's TIFFRepresentation()
####NSBitmapImageRep
set ocidNSBitmapImageRep to objMe's NSBitmapImageRep's imageRepWithData:ocidOsDispatchData
#################################################################
###quiet zone用に画像をパディングする
set numPadWidth to ((ocidCIImageWidth * numWidth) + (numWidth * 6)) as integer
set numPadHight to ((ocidCIImageHight * numHight) + (numHight * 6)) as integer
###左右に3セル分づつ余白 quiet zoneを足す
####まずは元のQRコードのサイズに6セルサイズ分足したサイズの画像を作って
set ocidNSBitmapImagePadRep to (objMe's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:numPadWidth pixelsHigh:numPadHight bitsPerSample:8 samplesPerPixel:4 hasAlpha:true isPlanar:false colorSpaceName:(objMe's NSCalibratedRGBColorSpace) bitmapFormat:(objMe's NSAlphaFirstBitmapFormat) bytesPerRow:0 bitsPerPixel:32)
###初期化
objMe's NSGraphicsContext's saveGraphicsState()
###ビットマップイメージ
(objMe's NSGraphicsContext's setCurrentContext:(objMe's NSGraphicsContext's graphicsContextWithBitmapImageRep:ocidNSBitmapImagePadRep))
###塗り色を『白』に指定して
objMe's NSColor's whiteColor()'s |set|()
##画像にする
objMe's NSRectFill({origin:{x:0, y:0}, |size|:{width:numPadWidth, height:numPadHight}})
###出来上がった画像にQRバーコードを左右3セル分ずらした位置にCompositeSourceOverする
ocidNSBitmapImageRep's drawInRect:{origin:{x:(numWidth * 3), y:(numHight * 3)}, |size|:{width:numPadWidth, Hight:numPadHight}} fromRect:{origin:{x:0, y:0}, |size|:{width:numPadWidth, height:numPadHight}} operation:(objMe's NSCompositeSourceOver) fraction:1.0 respectFlipped:true hints:(missing value)
####画像作成終了
objMe's NSGraphicsContext's restoreGraphicsState()
objMe's NSLog("■:osascript: restoreGraphicsState")
#################################################################

####JPEG用の圧縮プロパティ
##set ocidNSSingleEntryDictionary to objMe's NSDictionary's dictionaryWithObject:1 forKey:(objMe's NSImageCompressionFactor)
####PNG用の圧縮プロパティ
set ocidNSSingleEntryDictionary to objMe's NSDictionary's dictionaryWithObject:true forKey:(objMe's NSImageInterlaced)

#####出力イメージへ変換
set ocidNSInlineData to (ocidNSBitmapImagePadRep's representationUsingType:(objMe's NSBitmapImageFileTypePNG) |properties|:ocidNSSingleEntryDictionary)

######インラインデータをファイルに書き出し
set boolMakeQrCode to (ocidNSInlineData's writeToFile:strSaveFilePath atomically:true)

set aliasDirPath to POSIX file strDirPath as alias
###保存場所を開いて
tell application "Finder"
select aliasDirPath
end tell
####プレビューに表示
tell application "Preview"
launch
activate
open aliasDirPath
end tell

objMe's NSLog("■:osascript: Done")
set theURL to ""
set the clipboard to theURL
set ocidNSInlineData to ""
set ocidNSBitmapImagePadRep to ""


return true


to doCopyMap()
tell application "System Events"
launch
end tell
tell application "Maps"
activate
end tell
try
tell application "System Events"
tell process "Maps"
##get every menu bar
tell menu bar 1
##get every menu bar item
tell menu bar item "編集"
##get every menu bar item
tell menu "編集"
##get every menu item
tell menu item "リンクをコピー"
click
end tell
end tell
end tell
end tell
end tell
end tell
on error
tell application "System Events"
tell process "Maps"
get every menu bar
tell menu bar 1
get every menu bar item
tell menu bar item "編集"
get every menu bar item
tell menu "編集"
get every menu item
tell menu item "コピー"
click
end tell
end tell
end tell
end tell
end tell
end tell
end try
end doCopyMap

|

« [NSDateFormatter]日付時刻のフォーマット | トップページ | [NSMutableDictionary]可変レコード »

AppleScript Map」カテゴリの記事

AppleScript NSImage」カテゴリの記事