2024/08/27

写真番号が書かれたファイルを元にLightroomのxmp(サイドカー)へレートを書き込むAppleScript

写真番号が書かれたファイルから番号を抽出し、その番号と同じ番号の写真をLightroomで表示するAppleScriptです。

Lightroomはxmp(サイドカー)でカタログを管理していますので、そのxmpに直接文字列を書き込み、Lightroomにxmpを読み込ませることで「マーク」を反映させます。

マークとしては個人的にはフラグの「採用」を付けたいのですが、フラグ情報はxmpではなくカタログデータに書き込まれる仕様ですので、レートを付けることにしました。今回は★x5のレートです。

写真番号抽出部のカスタマイズ

最初に写真番号が書かれたファイルから番号を抽出するのですが、自分の環境用に作ったものですので、各々の環境に合わせて少し修正しないとならないかと思います。

抽出元となるファイルには「ご注文番号: 」に続いて写真番号が書かれています。

例えば、ご注文番号: 3506, 3508, 3530, 3533, 3536 のような感じです。

set fileNumbers to do shell script "echo " & quoted form of fileContent & " | grep -oE '" & regexPattern & "' | sed -n 's/ご注文番号://p' | tr -d ' '"

set AppleScript's text item delimiters to ","

「ご注文番号:」という文字列を探して、それを空の文字列に置換します(削除します)。そして更にスペースも削除します。
「ご注文番号: 3506, 3508, 3530, 3533, 3536」が
「3506,3508,3530,3533,3536」という一塊に連なった文字列になります。
次の行ではカンマ区切りで文字列を区分けするよと決めます。
これでやっと5枚の写真番号を取り出すことができました。
記載している番号の書き方に合わせ、ここら辺をいじると行けるかと思います。

レートの星の数を変更する

set cmd to "/opt/homebrew/bin/gsed -i '5i xmp:Rating=\"5\"' "
5iは5行目という意味ですが、適当に付けた物ですのでそれが相応しいかは分かりませんが、とりあえず問題なく読み込まれます。
xmp:Rating=\"5\" ←この5を1〜5の範囲で変更すれば他のレートが適用されます。

Lightroomでレートの適用

Lightroomで該当するフォルダまたはファイルを表示し、フィルムストリップ(ウィンドウ下に一列でサムネイルが表示される部分)のサムネイル右上にある矢印マークを押すか右クリックから、メタデータ>「ディスクから設定を読み込む」または「メタデータをファイルから読み込む」(訳が違うだけで同じもの)を選択すると書き込まれたxmpファイルが新たに読み込まれ、星5のレートが写真に付きます。
tell application "Finder"

try

set fileObject to selection as alias

on error

display dialog "order_ファイルを選択してください。処理を終了します。"

return

end try

if name of fileObject does not start with "order_" then

display dialog "order_ファイルを選択してください。処理を終了します。"

return

end if

end tell


-- ファイル内容を読み込んで処理

set fileContent to readFileContent(fileObject) --ファイル内容

set fileNumbers to extractFileNumbers(fileContent) as list --写真番号


set xmpFileNumbers to {} -- xmpファイル番号を格納するリスト

repeat with fileNumber in text items of fileNumbers

set end of xmpFileNumbers to fileNumber & ".xmp"

end repeat


tell application "Finder"

set targetFolder to (choose folder with prompt "RAWファイルのあるフォルダを指定してください") as text

end tell


set targetPOSIXFolder to quoted form of (text 1 thru -2 of POSIX path of targetFolder) --末尾の/を削除

-- .xmp ファイルの検索とxmp処理用リストの作成

set xmpFilePath to {}


repeat with xmpFileNumber in xmpFileNumbers

set foundFiles to paragraphs of (do shell script "find " & targetPOSIXFolder & " -type f -name '" & xmpFileNumber & "' -print")

if (count of foundFiles) > 0 then

set end of xmpFilePath to item 1 of foundFiles

else

display dialog "xmpファイルが全て見つかりません。フォルダを確認してください。"

return

end if

end repeat


-- ファイル名だけを取り出して変数に格納

set findFileNames to {}

set regexPattern to "/.?$"


repeat with filePath in xmpFilePath

set fileName to do shell script "basename " & quoted form of filePath

if fileName is not regexPattern then

set end of findFileNames to fileName

end if

end repeat


-- ないxmpファイルをerrFilesに追加

set errFiles to {}

repeat with xmpFileNumber in xmpFileNumbers

if xmpFileNumber is not in findFileNames then

set end of errFiles to xmpFileNumber as list

end if

end repeat



--xmpファイル処理

repeat with selectedFile in xmpFilePath

set selectedPOSIXFile to quoted form of the POSIX path of selectedFile

log selectedPOSIXFile

set fileExists to (do shell script "[ -e " & selectedPOSIXFile & " ] && echo 'true' || echo 'false'") as boolean --xmpファイルの存在確認

if fileExists then

-- ファイルをバックアップしてから、"xmp:Rating="が含まれている行を削除

set backupfileExists to (do shell script "[ -e " & selectedPOSIXFile & ".bak" & " ] && echo 'true' || echo 'false'") as boolean --xmpファイルの存在確認

if backupfileExists then

else

do shell script "cp " & selectedPOSIXFile & " " & selectedPOSIXFile & ".bak"

end if

do shell script "sed -i '' '/xmp:Rating=/d' " & selectedPOSIXFile

-- 5行目にxmp:Rating="5"を追加する

set cmd to "/opt/homebrew/bin/gsed -i '5i xmp:Rating=\"5\"' "

do shell script cmd & selectedPOSIXFile

else

set errFiles to errFiles & {selectedFile as text}

--display dialog "ファイルが存在しません。"

end if

end repeat

if (count of errFiles) > 0 then

activate

beep

display dialog "次のxmpファイルが見つかりませんでした: " & errFiles

else

activate

display dialog "該当番号すべてのxmpファイルを処理しました"

end if



-- ファイルの内容を読み込む関数

on readFileContent(fileObject)

set fileContent to read fileObject as «class utf8»

return fileContent

end readFileContent


-- ファイル番号を抽出する関数

on extractFileNumbers(fileContent)

set regexPattern to "ご注文番号: (.+)"

try

set fileNumbers to do shell script "echo " & quoted form of fileContent & " | grep -oE '" & regexPattern & "' | sed -n 's/ご注文番号://p' | tr -d ' '"

set AppleScript's text item delimiters to ","

set fileNumbers to text items of fileNumbers

return fileNumbers

on error errMsg

error "fileNumbers: " & errMsg

end try

end extractFileNumbers


Ï