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
◸ 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