ぼんやりDTP

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

ファイルの変更日を作成日に設定したり、作成日を変更日に設定したりする

ファイルの変更日を作成日に設定したり、作成日を変更日に設定したりする、Macで。

時計の狂った Mac で保存されたりしたファイルの処理用に。

「選択ファイルの変更日を作成日に設定する」のは AppleScript だけでできるが、 「選択ファイルの作成日を変更日に設定する」のは AppleScript だけではできない。

property name : "選択ファイルの変更日を作成日に設定する.scpt"

tell application "Finder"
    set myTarget to selection
    set myCount to count of myTarget
    if (myCount) > 0 then
        repeat with myFile in myTarget
            set myName to name of myFile
            set myCreationDate to creation date of myFile
            set myModifiedDate to modification date of myFile
            try
                set modification date of myFile to myCreationDate
            end try
        end repeat
        display dialog ((myCount as Unicode text) & "個のファイルの処理が終わりました。")
    else
        display dialog ("ファイルが選択されていないようです。")
    end if
end tell
property name : "選択ファイルの作成日を変更日に設定する.scpt"

tell application "Finder"
    set myTarget to selection
    set myCount to count of myTarget
    if (myCount) > 0 then
        repeat with myFile in myTarget
            set myName to name of myFile
            set myCreationDate to creation date of myFile
            set myModifiedDate to modification date of myFile
            set newCreationDateString to my dateToSetFileFormat(myModifiedDate)
            try
                --set creation date of myFile to myModifiedDate --これは効かない
                set myFileAlias to myFile as alias
                set myPosixPath to POSIX path of myFileAlias as string
                set myPosixPath to quoted form of myPosixPath
                set myScript to "setfile -d '" & newCreationDateString & "' " & myPosixPath
                do shell script myScript
            on error message
                display dialog message
            end try
        end repeat
        display dialog ((myCount as string) & "個のファイルの処理が終わりました。")
    else
        display dialog ("ファイルが選択されていないようです。")
    end if
end tell

on dateToSetFileFormat(theDate)
    set setFileFormatString to ""
    set theMonth to month of theDate as number
    set setFileFormatString to setFileFormatString & theMonth as string
    set theDay to day of theDate as number
    set setFileFormatString to setFileFormatString & "/" & theDay as string
    set theYear to year of theDate as number
    set setFileFormatString to setFileFormatString & "/" & theYear as string
    set theTime to time string of theDate
    set setFileFormatString to setFileFormatString & " " & theTime as string
    return setFileFormatString
end dateToSetFileFormat

参照ページ

  1. サンプルのページ
  2. ファイルの作成日を変更する - 名もないテクノ手
  3. SetFile コマンドの代わり – Good Harvest
  4. Mac setfile コマンド: ファイルの作成・更新日時を変更
  5. SetFile_ファイルの詳細情報を設定する - renoji.com
  6. OSXの固有コマンド - MacWiki
  7. 鳶嶋工房 / AppleScript / 入門 / エラーに備えよう
  8. https://sites.google.com/site/zzaatrans/home/applescriptlangguide/reference/aslr_error_codes-html?tmpl=%2Fsystem%2Fapp%2Ftemplates%2Fprint%2F&showPrintDialog=1
  9. AppleScriptのファイル参照にまつわるメモ - ザリガニが見ていた...。