ぼんやりDTP

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

Finderで選択したファイルのファイル名の日時にファイルの作成日と変更日を設定する

Finderで選択したファイルのファイル名の日時にファイルの作成日と変更日を設定する。Macで、AppleScriptで。

Thunderbirdで送信日を「2021-02-01_16-32-45」の形式でファイル名につけて書き出したメールの作成日と変更日を変更するように書いた。

大分雑で、力技だが、まあ自家用ならいいかな。

日付のチェックは数値の妥当性までは確認していない。

--DESCRIPTION:Finderで選択したファイルのファイル名の日時にファイルの作成日と変更日を設定する.scpt

(*
動作概要:
ファイル名が「2021-02-01_16-32-45」形式のファイル名で始まっている場合、その日時にファイルの作成日と変更日を設定する
*)

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

on run
    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
                
                try
                    set newCreationDateString to my getDateAndTimeFromString(myName)
                    
                    if newCreationDateString is false then
                        display dialog "処理対象外:" & myName
                    else
                        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
                        set myScript to "setfile -m '" & newCreationDateString & "' " & myPosixPath
                        do shell script myScript
                    end if
                on error message
                    display dialog message
                end try
            end repeat
            display dialog ((myCount as string) & "個のファイルの処理が終わりました。")
        else
            display dialog ("ファイルが選択されていないようです。")
        end if
    end tell
end run

on getDateAndTimeFromString(theString)
    --2021-02-01_16-32-45
    try
        set dateAndTimeString to strings 1 thru 19 in theString
    on error
        return false
    end try
    set validationResult to my validateDateAndTime(theString)
    if validationResult is true then
        set dateString to strings 1 thru 10 in dateAndTimeString
        
        set monthString to strings 6 thru 7 in dateString
        set dayString to strings 9 thru 10 in dateString
        set yearString to strings 1 thru 4 in dateString
        
        set timeString to strings 12 thru 19 in dateAndTimeString
        set timeString to my replaceText(timeString, "-", ":")
        
        set setFileFormatString to monthString & "/" & dayString & "/" & yearString & " " & timeString
        return setFileFormatString
    else
        return false
    end if
end getDateAndTimeFromString

on validateDateAndTime(theString)
    --2021-02-01_16-32-45  
    set offsetOfNumber to {1, 2, 3, 4, 6, 7, 9, 10, 12, 13, 15, 16, 18, 19}
    set offsetOfHyphen to {5, 8, 14, 17}
    set offsetOfUnderBar to {11}
    set theCounter to 1
    repeat with aCharacter in theString
        if theCounter is in offsetOfNumber then
            try
                set a to aCharacter as number
            on error
                return false
            end try
        else if theCounter is in offsetOfHyphen then
            if aCharacter as string is not "-" then
                return false
            end if
        else if theCounter is in offsetOfUnderBar then
            if aCharacter as string is not "_" then
                return false
            end if
        end if
        set theCounter to theCounter + 1
    end repeat
    return true
end validateDateAndTime

on replaceText(theText, serchStr, replaceStr)
    set tmp to AppleScript's text item delimiters
    set AppleScript's text item delimiters to serchStr
    set theList to every text item of theText
    set AppleScript's text item delimiters to replaceStr
    set theText to theList as string
    set AppleScript's text item delimiters to tmp
    return theText
end replaceText