1

I'd like to copy creation date from a file and apply it to the folder the file is sitting in. I'm using SetFile for this purpose. I"m getting error: Invalid date/time. My assumption is that it's the format of the date that is not working. How do I format the date properly? Could someone suggest solution? Thanks

    set filesToProcess to choose folder with prompt "Select folders:" with multiple selections allowed

        repeat with thisFile in filesToProcess
            tell application "Finder"
                try
                    set creationDate to creation date of (first file in the entire contents of thisFile whose name ends with "low_r.pdf")
set formattedCreationDate to  --creationDate needs reformatting?
                    do shell script "SetFile -d " & formattedCreationDate & " " & quoted form of (POSIX path of thisFile)
                on error fileNotFound
                    set label index of thisFile to 2
                    display dialog fileNotFound
                end try
            end tell
        end repeat
  • what does creationDate look like now? man page says it must be in a very specific format. date is a string of the form: "mm/dd/[yy]yy [hh:mm:[:ss] [AM | PM]]" Notes: Enclose the string in quotation marks if it contains spaces. The date must be in the Unix epoch, that is, between 1/1/1970 and 1/18/2038. If the year is provided as a two-digit year, it is assumed to be in the 21st century and must be from 00 (2000) through 38 (2038). – ThatOneDude Apr 08 '15 at 00:45
  • 1
    See my answer http://stackoverflow.com/questions/29480474/applescript-am-i-able-to-save-a-text-file-using-a-variable-for-the-filename-and , it has a stringFromDate method which shows how to handle date stuff. With that info you should be able to create the date string as SetFile wants. –  Apr 08 '15 at 03:18
  • @Zero Thanks. Yes it does the job. Can that also be extended to include time in format 20:04:33 ? – user3101259 Apr 08 '15 at 17:12
  • 1
    Yes, same rules apply to time components, like this: 'set {year:dYear, month:dMonth, day:dDay, hours:dHours, minutes:dMinutes, seconds:dSeconds} to aDate' –  Apr 08 '15 at 17:56
  • Thank you. I'll include the full working code – user3101259 Apr 08 '15 at 18:23

2 Answers2

1

Final working code:

set filesToProcess to choose folder with prompt "Select folders:" with multiple selections allowed

repeat with thisFile in filesToProcess
    tell application "Finder"
        try
            set creationDate to creation date of (first file in the entire contents of thisFile whose name ends with "low_r.pdf")
            set formattedCreationDate to quoted form of my stringForDate(creationDate)
            set formattedFolderLocation to quoted form of (POSIX path of thisFile)
            do shell script "SetFile -d " & formattedCreationDate & " " & formattedFolderLocation
        on error errorMsg
            set label index of thisFile to 2
            --display dialog errorMsg
        end try
    end tell
end repeat


on stringForDate(aDate)
    if aDate is "" then return null
    if class of aDate is not date then return null
    set {year:dYear, month:dMonth, day:dDay, hours:dHours, minutes:dMinutes, seconds:dSeconds} to aDate
    set dMonth to dMonth as integer
    if dMonth < 10 then set dMonth to "0" & dMonth
    if dDay < 10 then set dDay to "0" & dDay
    return ((dMonth & "/" & dDay & "/" & dYear & " " & dHours & ":" & dMinutes & ":" & dSeconds) as string)
end stringForDate

Thanks to everyone for contributions and @Zero for sharing the subroutine

0

Try this.

       set filesToProcess to choose folder with prompt "Select folders:" with multiple selections allowed

repeat with thisFile in filesToProcess
    tell application "Finder"
        try

            set theFileDate to (creation date of (first file in folder thisFile whose name ends with "low_r.pdf"))
            set formattedCreationDate to (word 2 of short date string of theFileDate & "/" & word 1 of short date string of theFileDate & "/" & word 3 of short date string of theFileDate) & space & time string of theFileDate
            --need to flip dd/mm/yyyy to be mm/dd/yyyy Ues this line -->>set formattedCreationDate to short date string of theFileDate & space & time string of theFileDate <<-- instead if your country's date format is already mm/dd/yyyy




            my doCommand(formattedCreationDate, thisFile)
        on error fileNotFound
            set label index of thisFile to 2
            display dialog fileNotFound
        end try
    end tell
end repeat
on doCommand(formattedCreationDate, thisFile)
    do shell script "SetFile -d " & (quoted form of formattedCreationDate) & " " & quoted form of (POSIX path of thisFile)
end doCommand
markhunte
  • 6,805
  • 2
  • 25
  • 44