Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

The Finder in Mac OS X 10.7 Lion shows a new piece of file metadata, "Date Added," which tracks the date an item was added to a folder. After upgrading to 10.7, none of the items in my ~/Downloads folder have "Date Added" values. I'd like to set all the empty "Date Added" values to match the "Date Modified" values, but I can't figure out how to set the "Date Added" attribute to a specific value.

My first guess was this:

xattr -w com.apple.metadata:kMDItemDateAdded "2012-02-19 16:34:47 +0000" myfile

But that doesn't seem to work (though it doesn't report an error either).

share|improve this question
    
Did you ended up finding a solution? –  erotsppa Oct 15 '13 at 21:07
    
The accepted answer worked when I tried it (as crazy as it is). –  John Siracusa Oct 22 '13 at 0:37

4 Answers 4

up vote 2 down vote accepted

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

  3. Have the workflow run an AppleScript.

Use this AppleScript:

on run {input, parameters}
    do shell script "/usr/sbin/systemsetup -setusingnetworktime Off"
    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 "/usr/sbin/systemsetup -setdate '" & dt & "'"
            do shell script "/usr/sbin/systemsetup -settime ''" & tm & "'"
            do shell script "mv \"" & myfile & "\" /var/tmp/clobber"
            do shell script "mv /var/tmp/clobber \"" & myfile & "\""
        end repeat
    end tell
    do shell script "/usr/sbin/systemsetup -setusingnetworktime On"
    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.

enter image description here

share|improve this answer
2  
That is awful…in a good way, maybe? –  John Siracusa Feb 20 '12 at 20:35
    
Well, it's definitely a hack. But it appears that particular piece of metadata is computed from Spotlight, and without hacking the dark mystery known as /.Spotlight-V100, this might be as good as it gets. But I'd love to see a clean answer. –  Daniel Lawson Feb 20 '12 at 22:14
    
I'm getting a weird error from this script? sh: -c line 0: unexpected EOF while looking for matching `" –  erotsppa Oct 15 '13 at 21:07

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.

share|improve this answer
1  
Run mdls on the files in your Downloads folder and you'll see the kMDItemDateAdded values. That's what the "Date Added" column in the Finder's List view shows. –  John Siracusa Feb 19 '12 at 18:25
    
Incidentally, if you want to show the kMDItemDownloadedDate as plist information without going through HexFiend and TextWrangler, try xattr -p com.apple.metadata:kMDItemDownloadedDate FILENAME_HERE | xxd -r -p | plutil -convert xml1 - -o -. The xxd converts to binary plist data, then the plutil converts to XML plist and prints it. –  Matt Gibson Feb 19 '12 at 18:28
    
OK, I'm in over my head, I'm afraid to say. kMDItemDateAdded isn't listed in xattr -l, and kMDItemDownloadedDate isn't listed with mdls. Curiouser and curiouser. Neither is the Date Added field stored in an xattr for the directory. Where does this metadata live? –  Daniel Lawson Feb 19 '12 at 19:18
1  
Since xattr is a python script, I guess it should be possible to poke around in this script and figure out how to get the binary data of the attribute in binary, rather than hex, so you can feed it directy to plutil. –  Harald Hanche-Olsen Feb 19 '12 at 20:12

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.

share|improve this answer
    
Spotlight! Well done. –  Daniel Lawson Feb 19 '12 at 20:44

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
share|improve this answer
    
Welcome to Ask Different! Instead of writing an answer to edit someone else's answer, simply click the edit or improve this answer button below the post that you wish to improve. –  George Garside Jun 17 at 16:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.