いつもはSwiftの記事ばかり書いていますが、これはAppleScript+写真Appの記事です。
写真Appは便利
昔はiPhoto1を使っていたけど、いつのまにか写真Appを使うようになっていた。なんせ、iCloudで写真を簡単に同期できちゃいますからね2。
位置情報の設定における苛立ち
今でこそiOS3の「写真」で位置情報を編集できるようになったものの、つい最近までmacOSの写真Appでしか位置情報を編集できなかったのです。インターフェースも分かりにくくて、「情報」ウィンドウに地名や緯度/経度を入力することで設定できます。
しかも、いざそこで設定しようとした時に…
「位置情報を検索中…」
この文字が表示されている間は、位置情報を設定できないという…。それも結構待たされる。
しかし、この文字が表示されている間でも位置情報(緯度/経度)を設定できてしまう裏技があるのです。
AppleScriptを使って写真Appで位置情報を設定する
写真AppはApple純正のアプリケーションなだけあって、AppleScriptに対応しています。
まず、選択されている写真のリストを取得します:
set selectedImages to (get selection)
緯度経度をカンマ区切りの文字で入力したら、それをそれぞれの写真に設定します:
set theLocList to every text item of newLocationString
repeat with theImage in selectedImages
set the location of theImage to theLocList
end repeat
全体のスクリプトは次のようになります:
(Gistにあるよ)
tell application "Photos"
activate
try
set selectedImages to (get selection)
set numberOfImages to (count of selectedImages)
set imagesDescription to ""
if numberOfImages ≤ 0 then
error "Please select an image."
else if numberOfImages = 1 then
set imageTitle to the name of first item of selectedImages
if (not (exists (imageTitle))) or (imageTitle is "") then
set imageTitle to the filename of first item of selectedImages
end if
set imagesDescription to "\"" & (imageTitle as string) & "\""
else
set imagesDescription to numberOfImages & " images"
end if
set dialogMessage to ("New location for " & imagesDescription & ":")
set response to display dialog dialogMessage default answer "" buttons {"Cancel", "OK"} default button "OK" with icon note
set newLocationString to text returned of response
if ((button returned of response) = "OK") and (newLocationString is not "") then
set AppleScript's text item delimiters to ","
set latitudeLongitude to every text item of newLocationString
if (count of latitudeLongitude) is not 2 then
error "Invalid Location."
end if
repeat with theImage in selectedImages
set the location of theImage to latitudeLongitude
end repeat
display alert "New location set to: " & newLocationString
end if
on error errorMsg number errorNumber
display alert errorMsg as critical
end try
end tell
ね?簡単でしょ。
メニューバーにスクリプトを表示させるようにすれば、とっても便利: https://support.apple.com/ja-jp/guide/script-editor/scpedt27975/mac
-
iOS 14以降だったか15以降だったか… ↩