Veritrope

Do Good Work

Navigation
  • Home
  • About…
    • Support
    • Contact
    • Consulting
  • Code
    • Bug Reporter
    • Customized Tools
  • Twitter
  • Facebook
  • LinkedIn
  • Instagram
  • Google+
  • RSS

Get a List of Tags in OS X

Description

Here's a proof-of-concept to demonstrate how you might read a file's tags in Apple's OS X operating system using AppleScript!

To use it, run the script and select a tagged file. (Note – This draft of the script only works with one file at a time and has no error checking!)

I've set up the script to return the tags as either an AppleScript list (useful to pipe those tags into other apps) or as a comma-separated text list. You can select which one you want with a switch that appears at the beginning of the script. You can also toggle whether or not to display the comma-separated text list in a dialog.

The Code

(*
◸ Veritrope.com
Get Tags in OS X as a List
VERSION 1.0
June 26, 2013

// UPDATE NOTICES
     ** Follow @Veritrope on Twitter, Facebook, Google Plus, and ADN for Update Notices! **

// SUPPORT VERITROPE!
     If this AppleScript was useful to you, please take a second to show your love here:
    http://veritrope.com/support

// SCRIPT INFORMATION AND UPDATE PAGE
     http://veritrope.com/code/get-a-list-of-tags-osx

// REQUIREMENTS
     More details on the script information page.

// CHANGELOG
    1.0  INITIAL DRAFT

// TERMS OF USE:
     This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

     To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.


*)


(*
======================================
// USER SWITCHES (YOU CAN CHANGE THESE!)
======================================
*)


-- SET THIS TO "OFF" TO GET RESULT AS APPLESCRIPT LIST
-- (USEFUL FOR USING TAGS WITH OTHER APPLICATIONS)
property text_List : "ON"

-- SET THIS TO "OFF" TO REMOVE DIALOG BOX FROM TEXT LIST RESULT
property display_ON : "ON"

(*
======================================
//  PROPERTIES (USE CAUTION WHEN CHANGING)
======================================
*)

property tagItems : {}

(*
======================================
// MAIN PROGRAM
======================================
*)

--RESET TEXT ITEM DELIMITERS
set AppleScript's text item delimiters to ""

--GET THE FILE
set theFile to quoted form of (POSIX path of (choose file without invisibles))

-- GET THE TAG VALUES VIA SHELL SCRIPT
set userTags to (do shell script "mdls -name kMDItemUserTags  " & theFile) as text

-- SET UP THE DELIMITER VALUES TO PULL OUT TAG VALUES…
set theDelim to "("
set finalDelim to ")"

-- SAVE THE OLD TEXT ITEM DELIMITERS
set oldDelim to AppleScript's text item delimiters

-- PROCESS THE KEY INTO A LIST OF TAGS
set AppleScript's text item delimiters to theDelim
set userTags to (userTags as text)
set preProc to (every word of userTags) as string
set procItems to words 3 thru end of preProc
set finalLine to last item of procItems
set AppleScript's text item delimiters to finalDelim
set finalProc to first text item of finalLine
set last item of procItems to finalProc
set AppleScript's text item delimiters to oldDelim

--  CREATE A COMMA-DELIMITED TAG STRING
if text_List is "ON" then
    set AppleScript's text item delimiters to ", "
    set procItems to (procItems as text)
    set AppleScript's text item delimiters to oldDelim
   
    -- DISPLAY AS DIALOG, IF SELECTED
    if display_ON is "ON" then display dialog procItems
else
    -- … OR CREATE A CLEAN LIST OF TAGS
   
    -- REMOVE THE COMMAS…
    repeat with procItem in procItems
        set the_String to procItem
        if (the last character of (the_String) is ",") then
            set newItem to (text 1 thru ((length of the_String) - 1)) of the_String as string
            copy newItem to the end of tagItems
        else
            set newItem to (text 1 thru (length of the_String)) of the_String as string
            copy newItem to the end of tagItems
        end if
    end repeat
   
    -- FINAL LIST
    return tagItems
end if
Tweet Follow @Veritrope
  • June 26, 2013
  • Justin Lancy
  • Comments off.
  • FinderMetadataOS XTags

Justin Lancy

Writer. Technologist.

See All Posts by Justin Lancy

Other Links for Justin

  • Justin's Website
  • Justin on Twitter
  • Justin on Facebook
  • Justin on Instagram
  • Justin on LinkedIn
  • Justin on App.net
  • Justin on Google+
Evernote — Create Note and Get Note Link
Save Chrome Tabs to OmniFocus

Script Information

Current Version: 1.0

Last Modified: September 4, 2014

Nice People

It takes a lot of time, money, and care to run this site. I'm so very grateful to the readers below who've shown their support for Veritrope with a donation.

isaac david
thanks again for the macmail script
Malone Guthrie
I have just used this script, which is a miracle for me as I have no idea about any sort of programing. Thank you so much. It worked perfectly 1st time!
Elizabeth Hannan
Thanks for creating and sharing this script - "Chrome tabs to Evernote dump" it has been very helpful! My guy says that I have a "tab" problem and yes, I have found the end of Chrome... its around 99 tabs.. a day. But I own an agency and consume over 600 webpages every 24 hours. Proud co-dependant :-) My guy can just marvel at my greatness! If there was a way to match the tab-save to a labeled-notebook that pulled the name of my device that would be great too! Kinda like a tab-hook on the auto dump. I average 4 devices a day across window and Mac. Thanks for saving me hours! -E
Simon Kennedy
The rest of the donation sorry.....
Simon Kennedy
You guys are genius, thank you all so much for the great time savings apps and support.
Donate Here!

(PayPal Donations Also Accepted on About=>Support Page)

// WANT TO EDIT THIS?

Open in Script Editor

// HAVING TROUBLE?

Submit Bug Report

// NEW TO APPLESCRIPT?

Learn More…

// WANT IT CUSTOMIZED?

Hire Me!

// EMAIL UPDATES

Join the List!

// FACEBOOK UPDATES

Like Veritrope

// TWITTER UPDATES

Follow @Veritrope

Nice People

It takes a lot of time, money, and care to run this site. I'm so very grateful to the readers below who've shown their support for Veritrope with a donation.

isaac david
thanks again for the macmail script
Malone Guthrie
I have just used this script, which is a miracle for me as I have no idea about any sort of programing. Thank you so much. It worked perfectly 1st time!
Elizabeth Hannan
Thanks for creating and sharing this script - "Chrome tabs to Evernote dump" it has been very helpful! My guy says that I have a "tab" problem and yes, I have found the end of Chrome... its around 99 tabs.. a day. But I own an agency and consume over 600 webpages every 24 hours. Proud co-dependant :-) My guy can just marvel at my greatness! If there was a way to match the tab-save to a labeled-notebook that pulled the name of my device that would be great too! Kinda like a tab-hook on the auto dump. I average 4 devices a day across window and Mac. Thanks for saving me hours! -E
Simon Kennedy
The rest of the donation sorry.....
Simon Kennedy
You guys are genius, thank you all so much for the great time savings apps and support.
Donate Here!

(PayPal Donations Also Accepted on About=>Support Page)


A Veritrope Website

  • Veritrope.com
  • TravelerTech.com
  • Cultivature.com

All content presented is © 2023 by Veritrope unless otherwise stated.
All Rights Reserved (unless specifically disclaimed).

Veritrope participates in several affiliate advertising programs (including Amazon Services LLC Associates Program and iTunes Affiliate Program). When you purchase an item through these links, I earn a small commission. Your support of Veritrope through these programs is very much appreciated!