Skip to content

Instantly share code, notes, and snippets.

@timpulver
Created September 17, 2015 12:49
Show Gist options
  • 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);
}
});
}
@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