« [Chrome]地図用のQRコードを生成する | トップページ | [every menu item]メニューアイテムを取得 »

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

macOSのMapアプリはApplescript対応していないので、System Eventsを使うのですが

20220430020817_1280x627x144_0
20220430020817_1280x557x144_0
状態によって
メニュー名が変わります
これに、気がつかなかった…トホホ

ダウンロード - geo2qr4.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
set objFileManager to objMe's NSFileManager's defaultManager()

set theURL to ""
set ocidNSInlineData to ""
set ocidNSBitmapImagePadRep to ""


###set strLogFile to "/tmp/Geo2Qr4.txt"

tell application "System Events"
launch
end tell
(*
tell application "System Events"
tell application process "Maps"
keystroke "C" using {command down}
end tell
end tell

tell application "System Events"
keystroke "c" using {command down}
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

(*
tell application "System Events"
tell process "Maps"
tell menu bar 1
tell menu bar item "Edit"
tell menu "Edit"
click menu item "Copy Link"
end tell
end tell
end tell
end tell
end tell
*)


tell application "Finder"
set theURL to (the clipboard) as text
end tell
delay 0.5


try
set objAns to (display alert "どちら用のQRコードを作成しますか?" message "iOSAppleMapQRコードを作成する OR 一般的なQRコードを作成する" default button 1 buttons {"AppleMap", "GoogleMap", "汎用"})
on error
log "エラーしました"
return
end try


set objResponse to (button returned of objAns) as text

set theURL to theURL as text


###
###do shell script "echo \"" & theURL & "\" >> " & strLogFile & ""
####

#########################
####URLの整形
set AppleScript's text item delimiters to "ll="
set listUrl to every text item of theURL as list
set AppleScript's text item delimiters to ""

set strSubURL to text item 2 of listUrl as text

set AppleScript's text item delimiters to "&"
set listSubURL to every text item of strSubURL as list
set AppleScript's text item delimiters to ""

set strLL to text item 1 of listSubURL as text

set AppleScript's text item delimiters to ","
set listLL to every text item of strLL as list
set AppleScript's text item delimiters to ""

set theLatitude to (item 1 of listLL) as text
set theLongitude to (item 2 of listLL) as text


####################################################

set strFileName to ("" & theLatitude & "-" & theLongitude & ".png")
#####################################################
if objResponse is "AppleMap" then
set theChl to ("GEO:" & theLatitude & "," & theLongitude & "") as text
log theChl
else if objResponse is "GoogleMap" then

###GoogleMap用の小数点以下の桁揃え
###逆ゼロサプレス
(**)
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 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 & ",16z")

set theChl to ("https://www.google.com/maps/" & theGooglemapParts & "") as text
else
set theChl to ("http://maps.apple.com/?q=" & theLatitude & "," & theLongitude & "") as text
end if
#######################


###フォルダのパス
###ピクチャーフォルダのパス
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


################################################################
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()

#################################################################

####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


set theURL to ""
set the clipboard to theURL
set ocidNSInlineData to ""
set ocidNSBitmapImagePadRep to ""
return true


to doReplace(theText, orgStr, newStr)
set oldDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to orgStr
set tmpList to every text item of theText
set AppleScript's text item delimiters to newStr
set tmpStr to tmpList as text
set AppleScript's text item delimiters to oldDelim
return tmpStr
end doReplace

|

« [Chrome]地図用のQRコードを生成する | トップページ | [every menu item]メニューアイテムを取得 »

AppleScript QR」カテゴリの記事

AppleScript Map」カテゴリの記事

AppleScript NSImage」カテゴリの記事

AppleScript Barcode」カテゴリの記事