"Sort by random" in OSX Finder setting kMDItemFinderComment to a random hash for every file in a folder?

993 views Asked by At

Finder allows you to sort files by many different attributes.

In the OSX filesystem, there is an attribute on every file called "comments" (com.apple.metadata:kMDItemFinderComment) which allows you to add any arbitrary string data as metadata for that file.

Finder exposes this "comment" attribute in the GUI and you can "sort" by it. I thought I could abuse this attribute to fill in random data for the each files "comments", and then sort by those random comments.

tldr; I'm trying to create "sort by random" functionality (in Finder) with the help of a BASH script and some python.

this does work to achieve that (sort of):

find $1 -type f -print0 | while IFS= read -r -d $'\0' file; #get a list of files in the dir
  do
  if [[ $file == *.wav ]]
    then
        hash=$(openssl rand -hex 12); #generate a random hash
        osxmetadata --set findercomment "$hash" $file; #set the comment
    fi
done

here i'm using the osxmetadata python utility to do the heavy lifting.

and while it works as intended, but it's really slow:

https://i.stack.imgur.com/d7exk.gif

i'm trying to do this operation on folders with many items, and would frequently be "re-seeding" the files with random comments.

can anyone suggest an optimization i can try to make this faster? i tried using xattrs but that doesn't seem reindex the comments in finder when they update.

1

There are 1 answers

3
Tobias Fendin On

I'd wrap the then-clause in a (...)& and add a wait after the loop. Then it will do every file in parallel.