You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

mdfind - sort by time?

hi,

i'm trying to use mdfind in an applescript i'm working on..


basically, i want to find a recently saved file, by name.. this part is easy and the result comes up very quick (mdfind -name filename)..


the problem i'm having is sometimes i may have multiple files named the same thing and using mdfind -name appears to return the files in a random order..


i'd like them to be sorted by creation time (or modification time or whatever)... is there a way to do that?



what i'm currently playing with is the following:


mdfind 'kMDItemFSName == filename && kMDItemFSCreationDate >= $time.now(-20)'



that works ok for me as the file i'm looking for at this point in the script would have been created less than 20 seconds ago.. the problem with this method however is that it's very slow.. mdfind -name will give me nearly instant results but going in to the metadata takes up to 10 seconds to find the file which is much too slow for the script to work properly..


so, my question is:


is there a way to sort the mdfind items by date?


(if so, i can just get the first item in the list via applescript and go from there)

thanks

Posted on Apr 20, 2012 9:23 AM

Reply
1 reply

Apr 20, 2012 10:37 AM in response to flat5

actually, i might of found a way using:


mdfind -name filename -0 | xargs -0 ls -t



so i guess now the question becomes..

is that the proper way to do what i'm looking for? my tests show that it's working but maybe it's doing something other than i'm asking and it's just a coincidence that my sort order is coming up as expected?


thanks

mdfind - sort by time?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.