Skip to content

Instantly share code, notes, and snippets.

@timpulver
Created September 17, 2015 12:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timpulver/3fdd2dd0732ef7de8de6 to your computer and use it in GitHub Desktop.
Save timpulver/3fdd2dd0732ef7de8de6 to your computer and use it in GitHub Desktop.
Read Mac OS X Finder file tags in Node.js
var exec = require('child_process').exec;
// Outputs the Mac OS X Finder tags of a file
// in the same directory as the script.
// Depends on tag (https://github.com/jdberry/tag)
var filename = "some_file_with_tags.md";
readTags(filename);
function readTags ( filename ) {
var cmdArr = ["tag", "--list", "--no-name", filename];
var cmd = cmdArr.join(" ");
exec ( cmd, function(error, stdout, stderr ) {
if ( error ) { console.error ( error ); }
if ( stderr ) { console.log( stderr ); }
if ( stdout ) {
var tags = stdout.toString().split(",");
console.log("Tags in file \"" + filename + "\": " + tags);
}
});
}
@whyboris
Copy link

whyboris commented Apr 12, 2020

This script seems to require that tag, some command-line tool, likely this one, needs to be installed for this to work 🤔

Here's a fix for line 12 that will work:

    const cmdArr = ['mdls', '-raw', '-name', 'kMDItemUserTags', '"' + filename + '"'];

Note it's using mdls which seems to be native to Mac OS -- I found the solution here 😁

@timpulver
Copy link
Author

Hi @whyboris,
I mention the tag dependency in line 5. But good that you could make it work for yourself.

@whyboris
Copy link

😅 🙇 so sorry I completely missed the comment -- I'm unsure how 🤦 ... thank you for the gist -- it helped me out 🙇

@bentorkington
Copy link

This will break on a filename with spaces. Best to get rid of var cmd = cmdArr.join(" ") and pass the argument array directly to execFile("tag", args, …) instead

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment