Changing file metadata from CLI that will show in Finder

Solution 1:

mdls uses it's database to display file info, not extended attributes that are set on that file. So in order to make it work, you have to import the file to its database after changing the xattr with:

mdimport /path/to/file

In order to see the update comment in Finder, you can use AppleScript. Here's the code:

#!/bin/bash                                                                                                                                                                                                     

filepath="$1"
comment="$2"

/usr/bin/osascript -e "set filepath to POSIX file \"$filepath\"" \
-e "set theFile to filepath as alias" \
-e "tell application \"Finder\" to set the comment of theFile to \"$comment\""

Use as ./script.sh path_to_file "Comment"

Better version of that script can be found on StackOverflow by user l'L'l.