How to set "date added" metadata in Mac OS X 10.7 Lion

OK, new approach here. Caution: I don't have a system upgraded to Lion (my computer came with Lion installed) so I can't test this. Untested code; back up before trying this code!

My previous answer was based on the sort order used by the Downloads stack in the Dock. The Date Added field in the Finder appears to be based on Spotlight information, which is difficult to hack. It also isn't accessible via AppleScript. But, there does seem to be a workaround.

  1. Create a new Workflow in Automator.

  2. Set the workflow to accept files or folders from the Finder by adding the “Ask for Finder items” action.

  3. Have the workflow run an AppleScript by adding the “Run AppleScript” action.

Use this AppleScript:

on run {input, parameters}
    do shell script "sudo /usr/sbin/systemsetup -setusingnetworktime Off" with administrator privileges
    tell application "Finder"
        repeat with x in input
            set myfile to POSIX path of x
            set nm to name of x
            set d to modification date of x
            set yr to (character 3 of (year of d as string)) & (character 4 of (year of d as string))
            set mth to (month of d as number) as string
            if length of mth is 1 then set mth to "0" & mth
            set dy to day of d as string
            if length of dy is 1 then set dy to "0" & dy
            set h to hours of d as string
            if length of h is 1 then set h to "0" & h
            set m to minutes of d as string
            if length of m is 1 then set m to "0" & m
            set s to seconds of d as string
            if length of s is 1 then set s to "0" & s
            set dt to mth & ":" & dy & ":" & yr as string
            set tm to h & ":" & m & ":" & s as string
            do shell script "sudo /usr/sbin/systemsetup -setdate '" & dt & "'" with administrator privileges
            do shell script "sudo /usr/sbin/systemsetup -settime '" & tm & "'" with administrator privileges
            do shell script "mv \"" & myfile & "\" /var/tmp/clobber"
            do shell script "mv /var/tmp/clobber \"" & myfile & "\""
        end repeat
    end tell
    do shell script "sudo /usr/sbin/systemsetup -setusingnetworktime On" with administrator privileges
    return input
end run

Select the files that do not already have a Date Added (sort by Date Added in Finder, then select the part of the list without a Date Added) and run this service.

screenshot of the workflow in Automator


When I run xattr -l on items in my Downloads folder, I get a field that looks something like this:

com.apple.metadata:kMDItemDownloadedDate:
00000000  62 70 6C 69 73 74 30 30 A1 01 33 41 B4 83 4D BF  |bplist00..3A..M.|
00000010  4C 4F E3 08 0A 00 00 00 00 00 00 01 01 00 00 00  |LO..............|
00000020  00 00 00 00 02 00 00 00 00 00 00 00 00 00 00 00  |................|
00000030  00 00 00 00 13                                   |.....|
00000035

This is a binary plist. When I use HexFiend to create a file with those bytes (yes, I manually entered them; blast from the past like entering assembler code out of a magazine into my Apple ][GS), then save it as a .plist file, I opened the file in TextWrangler and got the following uncompiled xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <date>2011-11-28T05:03:59Z</date>
</array>
</plist>

That said, while Apple seems to store the dates in compiled XML, plain text seems to work.

In other words, if you can get the file's modified date in string form, you can run the command xattr -w com.apple.metadata:kMDItemDownloadedDate "2012-02-19 16:34:47 +0000" file to change the "downloaded date", which appears to be the field actually sorted on, not actual Date Added.

Finally you got no error when adding the (unused) kMDItemDateAdded field because, as I learned in this article, xattr will happily set whatever metadata field you want, used or unused.

That's the core of the answer. I'll work on writing an AppleScript to get the modified date for each file, check to see if kMDItemDownloadedDate is set, and if it isn't, set kMDItemDownloadedDate to the modified date, but I wanted to get the core of the answer posted.


I can't find a way to set the "Date Added" shown in the Finder.

I believe you're correct that it's retrieved from the Spotlight index's kMDItemDateAdded metadata attribute. However, Spotlight appears to derive this itself in some way.

I've tried setting up an extended file attribute called com.apple.metadata:kMDItemDateAdded to a date value in one of several different formats, including the format used by kMDItemDateAdded and none of them were picked up by the Spotlight index, i.e. no matter what the value shown by xattr, the value shown by mdls wasn't changed.

I would guess, though I don't know for sure, that Spotlight simply sets this date based on the first time it indexes a file in a particular location, and doesn't check any other metadata in order to generate it. If you mv a file out of Downloads and back in, the Date Added updates to when it was moved back in, but none of the file metadata seems affected, only the Spotlight metadata.

So, in summary, I reckon Date Added is only stored somewhere in the rather cryptic guts of /.Spotlight-V100, and unless someone can come up with a way of telling Spotlight to update a metadata entry to an arbitrary value, I can't see a way of doing this.


Thanks to Daniel Lawson for the solution! It still works well, even two years later.

I have two additions:

1) Please note that there's a small error in the code of the accepted answer.

This line:

do shell script "/usr/sbin/systemsetup -settime ''" & tm & "'"

...has an extra apostrophe, triggering an "unexpected EOF" error. It should read:

do shell script "/usr/sbin/systemsetup -settime '" & tm & "'"

2) More important, starting with Mavericks 10.9.2, systemsetup requires administrator rights. So every call to shell script should follow this formula:

do shell script "sudo /usr/sbin/systemsetup -setusingnetworktime Off" with administrator privileges

Here's the full modified version of the AppleScript, confirmed to work in 10.9.3:

on run {input, parameters}
    do shell script "sudo /usr/sbin/systemsetup -setusingnetworktime Off" with administrator privileges

    tell application "Finder"
        repeat with x in input
            set myfile to POSIX path of x
            set nm to name of x

            set d to modification date of x
            set yr to (character 3 of (year of d as string)) & (character 4 of (year of d as string))
            set mth to (month of d as number) as string
            if length of mth is 1 then set mth to "0" & mth
            set dy to day of d as string
            if length of dy is 1 then set dy to "0" & dy
            set h to hours of d as string
            if length of h is 1 then set h to "0" & h
            set m to minutes of d as string
            if length of m is 1 then set m to "0" & m
            set s to seconds of d as string
            if length of s is 1 then set s to "0" & s

            set dt to mth & ":" & dy & ":" & yr as string
            set tm to h & ":" & m & ":" & s as string
            do shell script "sudo /usr/sbin/systemsetup -setdate '" & dt & "'" with administrator privileges
            do shell script "sudo /usr/sbin/systemsetup -settime '" & tm & "'" with administrator privileges

            do shell script "mv \"" & myfile & "\" /var/tmp/clobber"
            do shell script "mv /var/tmp/clobber \"" & myfile & "\""
        end repeat
    end tell

    do shell script "sudo /usr/sbin/systemsetup -setusingnetworktime On" with administrator privileges

    return input
end run