見出し画像

見えないウォーターマークのドロップレット化

ドロップレットの作成

ファイルをドラッグ&ドロップするだけで一括で見えないウォーターマークを埋め込むスクリプトを作りました。

ドロップレットの処理は以下を参考にさせていただきました。

コード

on run
    
    tell application "Finder"
        beep
        display alert "ドロップレット形式です。
ファイルをドラッグ&ドロップしてください。" as informational
        return
    end tell
end run
-------------------------------------------------処理の開始
on open DropObj
    tell application "Finder"
        activate
        -------------------------------------------------念のために各種変数をリセットする
        set the button_pressed to ""
        set the exeName to ""
        set newexeName to ""
        -------------------------------------------------ファイル名を入力するダイアログを表示
        display dialog "埋め込み文字列(半角4文字)を入力" default answer "" buttons {"OK", "Cancel"} with icon 1 default button 1 with title "埋め込み文字列" cancel button "Cancel"
        -------------------------------------------------リネームファイルを取得IndentifyText
        set IndentifyText to text returned of the result as Unicode text
        if IndentifyText is "" then
            ---------------------------------------------空ならデフォルト値を設定する
            set IndentifyText to "AaBb"
        end if
        -------------------------------------------------ファイルの数だけ繰り返し
        repeat with ObjFiles in DropObj
            tell application "Finder"
                --------------------------------------------------ファイル名を取得
                set theName to name of ObjFiles
                ------------------------------------------------パスを取得
                set theAlias to ObjFiles as alias
                ------------------------------------------------パスを文字列に変換
                -- set theAliasPath to theAlias as string -- Alias表現
                set theAliasPath to POSIX path of theAlias -- POSIX表現
                ------------------------------------------------拡張子を判定
                set exeName to name extension of ObjFiles
                --------------------------------------------------拡張子を小文字に変更文字
                --------------------------------------------------macwiki.sourceforge.jpより引用
                repeat with aChar in every character of exeName
                    set asNum to ASCII number aChar
                    if (asNum > 64) and (asNum < 91) then set aChar to ASCII character (asNum + 32) -- tr/A-Z/a-z/
                    -- if (asNum > 96) and (asNum < 123) then set aChar to ASCII character (asNum - 32) -- tr/a-z/A-Z/
                    set newexeName to newexeName & aChar
                end repeat
                --------------------------------------------------新しいファイル名を定義
                set BaseName to my replace(theName, "." & exeName, "")
                set the new_file_name to the (the BaseName & "_WMK." & newexeName) as Unicode text
                -------------------------------------------------フルパスからファイル名を抜いてディレクトリ名を作る
                set DirName to my replace(theAliasPath, theName, "")
                -------------------------------------------------ファイル名を変更
                ---set name of file theName of folder DirName to new_file_name
                set CommandLine to "zsh -c \"source ~/.zshrc; invisible-watermark -v -a encode -t bytes -m rivaGan -w '" & IndentifyText & "' -o '" & DirName & new_file_name & "' '" & DirName & theName & "'\""
                --display dialog CommandLine
                do shell script CommandLine
                -------------------------------------------------拡張子の名前をリセット
                set the exeName to ""
                -------------------------------------------------拡張子の名前をリセット
                set newexeName to ""
            end tell
        end repeat
    end tell
end open
--------------------------------------------------#サブルーチン
to replace(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 replace
--------------------------------------------------#サブルーチン

Exif情報が消えてしまう対策

用途によっては利点にもなるのですが、invisible-watermarkを通したJPEGは、Exif情報が消えてしまいます。それだけならまだしもカラープロファイルが消えてしまうのが我慢ならなかったです。
これをexif-toolで復元する事にします。
処理としては以下の様な感じです。

  1. exif-toolでオリジナルのJPEGのExif情報をJSONファイルにして保存

  2. invisible-watermarkで見えないウォーターマークを埋め込んでJPEGを出力

  3. 出力したJPEGにexif-toolでオリジナルのJPEGファイルのExif情報を書き戻す

  4. JSONファイルを消す

調べるまで知らなかったのですが、Exifのタグでカラープロファイルも表現できるらしく(iccプロファイルを埋め込んでいる場合もある)Exifを復元すると、プロファイルも復元しました。

追加で必要なツール

brewでexif-toolとjqをインストールします。jqはJSONを加工するツールで、なぜ必要かというと、JSONで保存したExif情報の中にオリジナルのファイルのパスが"SourceFile"タグとして埋め込まれている場合、実際のJPEGと一致しないとexif-toolで書き戻せないためで、該当タグを保存時に消してしまうのに使います。

brew install exiftool
・何やら表示される
brew install jq
・何やら表示される

コード(Exifキープ版)

on run
    tell application "Finder"
        beep
        display alert "ドロップレット形式です。
ファイルをドラッグ&ドロップしてください。" as informational
        return
    end tell
end run
-------------------------------------------------処理の開始
on open DropObj
    tell application "Finder"
        activate
        -------------------------------------------------念のために各種変数をリセットする
        set the button_pressed to ""
        set the exeName to ""
        set newexeName to ""
        -------------------------------------------------ファイル名を入力するダイアログを表示
        display dialog "埋め込み文字列(半角4文字)を入力" default answer "" buttons {"OK", "Cancel"} with icon 1 default button 1 with title "埋め込み文字列" cancel button "Cancel"
        -------------------------------------------------リネームファイルを取得IndentifyText        
        set IndentifyText to text returned of the result as Unicode text
        if IndentifyText is "" then
            ---------------------------------------------空ならデフォルト値を設定する
            set IndentifyText to "AaBb"
        end if
        -------------------------------------------------ファイルの数だけ繰り返し
        repeat with ObjFiles in DropObj
            tell application "Finder"
                --------------------------------------------------ファイル名を取得
                set theName to name of ObjFiles
                ------------------------------------------------パスを取得
                set theAlias to ObjFiles as alias
                ------------------------------------------------パスを文字列に変換
                -- set theAliasPath to theAlias as string -- Alias表現
                set theAliasPath to POSIX path of theAlias -- POSIX表現
                ------------------------------------------------拡張子を判定
                set exeName to name extension of ObjFiles
                --------------------------------------------------拡張子を小文字に変更文字
                --------------------------------------------------macwiki.sourceforge.jpより引用
                repeat with aChar in every character of exeName
                    set asNum to ASCII number aChar
                    if (asNum > 64) and (asNum < 91) then set aChar to ASCII character (asNum + 32) -- tr/A-Z/a-z/
                    -- if (asNum > 96) and (asNum < 123) then set aChar to ASCII character (asNum - 32) -- tr/a-z/A-Z/
                    set newexeName to newexeName & aChar
                end repeat
                --------------------------------------------------新しいファイル名を定義                
                set BaseName to my replace(theName, "." & exeName, "")
                set the new_file_name to the (the BaseName & "_WMK." & newexeName) as Unicode text
                -------------------------------------------------フルパスからファイル名を抜いてディレクトリ名を作る
                set the exif_json_name to the (the BaseName & "_exif." & "json") as Unicode text
                -------------------------------------------------フルパスからファイル名を抜いてディレクトリ名を作る
                set DirName to my replace(theAliasPath, theName, "")
                -------------------------------------------------ファイルごとのメイン処理
                set CommandLine to "exiftool -q -j --SourceFile '" & DirName & theName & "' | jq '[.[0]|del(.SourceFile)]' > \\\"" & DirName & exif_json_name & "\\\""
                doZshScript(CommandLine) of me
                set CommandLine to "invisible-watermark -v -a encode -t bytes -m rivaGan -w '" & IndentifyText & "' -o '" & DirName & new_file_name & "' '" & DirName & theName & "'"
                doZshScript(CommandLine) of me
                set CommandLine to "exiftool -overwrite_original -j=\\\"" & DirName & exif_json_name & "\\\" '" & DirName & new_file_name & "' && rm \\\"" & DirName & exif_json_name & "\\\""
                doZshScript(CommandLine) of me
                -------------------------------------------------拡張子の名前をリセット
                set the exeName to ""
                -------------------------------------------------拡張子の名前をリセット
                set newexeName to ""
            end tell
        end repeat
    end tell
end open
--------------------------------------------------#サブルーチン
to replace(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 replace
--------------------------------------------------#サブルーチン
--------------------------------------------------#サブルーチン
to doZshScript(cmdLine)
    -- display dialog "zsh -c \"source ~/.zshrc;" & cmdLine & "\""
    do shell script "zsh -c \"source ~/.zshrc;" & cmdLine & "\""
    return
end doZshScript
--------------------------------------------------#サブルーチン

この記事が気に入ったらサポートをしてみませんか?