Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Script to get Tag List




Hi @VikingOSX


You have been very helpful, and then a find another script at https://discussions.apple.com/thread/8013540?answerId=31991695022

I have tried the script, it does generate the Tag List, but with loads missing. These tags do appear in the Finder All Tags view, plz see screenshot

The Tag list I get is {"Yellow", "Green", "Important", "Gray", "Blue", "Orange", "Home", "TFP", "Purple", "Red", "Work"}

Plz suggest what I am doing

Thanks in advance


PS: the PDF script is working great

Posted on Mar 9, 2021 10:32 AM

Reply
Question marked as Best reply

Posted on Mar 9, 2021 11:49 AM

There are seven predefined tag colors with color-specific names (including the transparent None tag). These are "system" font colors. If you have no custom tag names defined, and drop a file on your drive that does have a custom tag name from another machine, or create your own new custom tag name, two things will happen.

  1. The new .plist file /Users/username/Library/SyncedPreferences/com.apple.finder.plist will be created, and the new tag name added to it.
  2. A new custom tag name entry will be placed in the Finder Preferences > Tags panel. It may need you to activate it though.


The key to my script finding all available user-defined and system tag names is that their right-most column in the Finder Preferences > Tags panel must have some indication [√-] that it is activated. I just ran this script on macOS 11.2.3 and it found all of my tags save for one that was not activated.


In the script, the allTags variable is a list of all found tag names, and when used in the choose from list allTags, that creates the following neat list display of found tag names:


If all you want is a list of currently defined tag names, then the following AppleScript will produce that list in the display dialog:


(*

	Generate an inclusive list of standard and user defined tag names
	
	Tested: macOS 11.2.3
	Author: VikingOSX, 2021-03-09, Apple Support Communities, no warranties
*)

use framework "AppKit"
use scripting additions

property NSWorkspace : a reference to current application's NSWorkspace

set tagfile to POSIX path of ((path to home folder as text) & "Library:SyncedPreferences:com.apple.finder.plist") as text
set keyPath to "values:FinderTagDict:value:FinderTags"
set AWKCMD to " | awk '/n/ {print substr($0, index($0, $3))}'"

-- if custom tag names were created in Finder this file will exist
if exists ((POSIX file tagfile) as alias) = true then
	set allTags to paragraphs of (do shell script "/usr/libexec/PlistBuddy -c \"print " & keyPath & "\" " & tagfile's quoted form & AWKCMD)
else
	-- no custom tag names exist, so get default Finder tag names
	set allTags to NSWorkspace's sharedWorkspace()'s fileLabels() as list
end if

if exists (allTags) is true then
	set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, return}
	display dialog (items of allTags) as text with title "User Defined Tag Names"
	set AppleScript's text item delimiters to TID
end if
return


Which produces this:


13 replies
Question marked as Best reply

Mar 9, 2021 11:49 AM in response to one208

There are seven predefined tag colors with color-specific names (including the transparent None tag). These are "system" font colors. If you have no custom tag names defined, and drop a file on your drive that does have a custom tag name from another machine, or create your own new custom tag name, two things will happen.

  1. The new .plist file /Users/username/Library/SyncedPreferences/com.apple.finder.plist will be created, and the new tag name added to it.
  2. A new custom tag name entry will be placed in the Finder Preferences > Tags panel. It may need you to activate it though.


The key to my script finding all available user-defined and system tag names is that their right-most column in the Finder Preferences > Tags panel must have some indication [√-] that it is activated. I just ran this script on macOS 11.2.3 and it found all of my tags save for one that was not activated.


In the script, the allTags variable is a list of all found tag names, and when used in the choose from list allTags, that creates the following neat list display of found tag names:


If all you want is a list of currently defined tag names, then the following AppleScript will produce that list in the display dialog:


(*

	Generate an inclusive list of standard and user defined tag names
	
	Tested: macOS 11.2.3
	Author: VikingOSX, 2021-03-09, Apple Support Communities, no warranties
*)

use framework "AppKit"
use scripting additions

property NSWorkspace : a reference to current application's NSWorkspace

set tagfile to POSIX path of ((path to home folder as text) & "Library:SyncedPreferences:com.apple.finder.plist") as text
set keyPath to "values:FinderTagDict:value:FinderTags"
set AWKCMD to " | awk '/n/ {print substr($0, index($0, $3))}'"

-- if custom tag names were created in Finder this file will exist
if exists ((POSIX file tagfile) as alias) = true then
	set allTags to paragraphs of (do shell script "/usr/libexec/PlistBuddy -c \"print " & keyPath & "\" " & tagfile's quoted form & AWKCMD)
else
	-- no custom tag names exist, so get default Finder tag names
	set allTags to NSWorkspace's sharedWorkspace()'s fileLabels() as list
end if

if exists (allTags) is true then
	set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, return}
	display dialog (items of allTags) as text with title "User Defined Tag Names"
	set AppleScript's text item delimiters to TID
end if
return


Which produces this:


Mar 9, 2021 12:30 PM in response to VikingOSX

@ VikingOSX


Yep, these additional tags were not ticked in the Finder Preferences, After Check (✓)..they appear in the Search !


Your Script at https://discussions.apple.com/thread/8013540?answerId=31991695022 works a treat


Hope you dont mind, is it possible to get Unchecked Tags in Finder Preference by Script into Tag List?


Thanks for your help & quick answer



Mar 9, 2021 4:13 PM in response to one208

The following AppleScript produces this dialog. Not for the person with hundreds of tags or the dialog will bury itself beneath the Dock:



(*

	Generate an inclusive list of standard and user defined tag names, and additionally
	inactive tag names.
	
	Tested: macOS 11.2.3
	Author: VikingOSX, 2021-03-09, Apple Support Communities, no warranties
*)
use framework "Foundation"
use framework "AppKit"
use scripting additions

property NSWorkspace : a reference to current application's NSWorkspace
property NSSet : a reference to current application's NSSet

set aTags to {}
set iTags to {}
set tagfile to POSIX path of ((path to home folder as text) & "Library:SyncedPreferences:com.apple.finder.plist") as text
set keyPath to "values:FinderTagDict:value:FinderTags"
set AWKCMD to " | awk '/n/ {print substr($0, index($0, $3))}'"

-- if custom tag names were created in Finder this file will exist
if exists ((POSIX file tagfile) as alias) = true then
	set allTags to paragraphs of (do shell script "/usr/libexec/PlistBuddy -c \"print " & keyPath & "\" " & tagfile's quoted form & AWKCMD)
	set inactive_tags to paragraphs of (do shell script "/usr/libexec/PlistBuddy -c \"print " & keyPath & "\" " & tagfile's quoted form & " | awk '
	
BEGIN { delete ary[0] }

/n/ {tagname = substr($0, index($0, $3))
getline
getline
if ($0 ~ /v/)
    ary[x++] = tagname
else
{
    # hopefully remove the last pushed element as it active
    delete ary[-1]
}
}
END { for (i = 0; i < length(ary); i++)
    print ary[i]
}'")
else
	-- no custom tag names exist, so get default Finder tag names
	set allTags to NSWorkspace's sharedWorkspace()'s fileLabels() as list
end if

set {aTags, iTags} to my exclusive_array(allTags, inactive_tags) as list

if exists (allTags) is true then
	set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, return}
	display dialog ("-----------" & return & "Active tags" & return & "-----------" & return & (items of aTags) as text) & return & "-------------" & return & "Inactive Tags" & return & "-------------" & return & (items of iTags) as text with title "User Defined Tag Names"
	set AppleScript's text item delimiters to TID
end if
return

on exclusive_array(ar1, ar2)
	
	set activeSet to (NSSet's alloc()'s initWithArray:ar1)'s mutableCopy()
	set inactiveSet to NSSet's alloc()'s initWithArray:ar2
    # remove any inactive tag name from the active tag set
	activeSet's minusSet:inactiveSet
	set A to activeSet's allObjects() as list
	set B to inactiveSet's allObjects() as list
	
	return {A, B}
	
end exclusive_array


Mar 9, 2021 6:59 PM in response to one208

I created a new Automator application, in which I added a sole Run AppleScript action. I then erased all default code in the action, and then copy/pasted the script that I just posted earlier this evening. I clicked the hammer icon in the action to compile the AppleScript and then ran the Automator application without any errors. It produced the same active/inactive list of tags as I posted this evening.


Mar 9, 2021 4:48 PM in response to VikingOSX

Many Thanks for Script, as always works, displays list of tags both active & inactive

If have used the the Tag list as " allTags" variable of all found tag names, in automator script

Apologies cant make this work :(

error :cant make {"aaaaa","bbb",....} into string type


set {aTags, iTags} to my exclusive_array(allTags, inactive_tags) as list

set tagList to {}

set tagList to (choose from list {aTags, iTags} with title "Tags Selection" with prompt lprompt default items "" with multiple selections allowed without empty selection allowed)

if tagList is false then

display alert "User pressed cancel, quitting." as critical giving up after 10

end if

Mar 10, 2021 7:23 AM in response to one208


one208 wrote:

Many Thanks for Script, as always works, displays list of tags both active & inactive
If have used the the Tag list as " allTags" variable of all found tag names, in automator script
Apologies cant make this work :(
error :cant make {"aaaaa","bbb",....} into string type

set {aTags, iTags} to my exclusive_array(allTags, inactive_tags) as list
set tagList to {}
set tagList to (choose from list {aTags, iTags} with title "Tags Selection" with prompt lprompt default items "" with multiple selections allowed without empty selection allowed)
if tagList is false then
display alert "User pressed cancel, quitting." as critical giving up after 10
end if

The AppleScript choose from list syntax only supports one list, and you are attempting to provide it with two. Either you want the aTags list allowing you to select from aTags, or iTags allowing you to choose inactive tags, but not both.


If you wanted to concatenate the inactive tag names to the end of the active tag names, then you could create a single list from:


set newList to aTags & iTags


Script to get Tag List

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.