ぼんやりDTP

DTPに関係したりしなかったりするぼんやりとした話をなんとなく。

選択した2つのファイルの名前を入れ替える

選択した2つのファイルの名前を入れ替える、Macで、AppleScriptで。

下記スクリプトの挙動:

  1. 選択した2つのファイルを複製する
  2. 元の2つのファイルをゴミ箱に入れる
  3. 複製された2つのファイルを元の名前を入れ替えてリネームする

元の名前のファイルはゴミ箱に入っているのでリネームがうまくいったようなら適宜手動でゴミ箱を空にする。

ファイル名のバッティングとか厳密に考えると面倒なので複製を利用することにした。

property name : "選択した2つのファイルの名前を入れ替える.scpt"

on run
    tell application "Finder"    
        set selectedItems to selection
        if (count of selectedItems) is not 2 then
            display dialog "You must select a total of two items." buttons {"OK"} default button 1 cancel button 1 with icon stop
            return
        else
            set file_1 to item 1 of selectedItems
            set file_2 to item 2 of selectedItems
        end if
                        
        set fileName_1 to name of file_1 as string
        set fileName_2 to name of file_2 as string
                
        set duplicatedFile_1 to duplicate file_1
        set duplicatedFile_2 to duplicate file_2
        
        delete file_1
        delete file_2

        set name of duplicatedFile_1 to fileName_2
        set name of duplicatedFile_2 to fileName_1
    end tell
end run

参考ページ

  1. MacScripter / Swap Files Names Between 2 files.