-
All replies
-
Helpful answers
-
Nov 19, 2013 2:02 PM in response to thomasbestby red_menace,The first label works the same (to keep backwards compatibility), but tags are implemented as an extended file attribute. Ars Technica has a decent writeup on the implementation in their Mavericks Review article, but basically you are looking at using the xattr and plutil shell utilities to add or modify a property list attribute.
-
Nov 19, 2013 11:25 PM in response to red_menaceby thomasbest,Thanks for your answer. Unfortunately, it doesn't work for me. The exact same code worked before mavericks, but doesn't under 10.9.
tell application "Finder"
set name of myfile to (myname & mytext & \".\" & suffix) as string
set label index of myfile to 2 --red label
end tell
LIne 2 works (The file "myfile" is renamed)
Line 3 doesn't work under 10.9 (no tag is set to the renamed file)
Any ideas?
-
Nov 19, 2013 11:50 PM in response to thomasbestby red_menace,It works for me in Mavericks, although it only sets the label (such as they are) - to set a tag programmatically you will need to use xattr.
-
Jun 16, 2014 7:48 AM in response to thomasbestby julesforum,There's a great litte 3rd party command line tool called 'tag' which makes querying and manipulating tags very easy. I've used it to create an Automator Folder Action that tags files automatically when dropped into the folder.
https://trac.macports.org/browser/trunk/dports/sysutils/tag/Portfile
An easy way to download and install it is through Macports. If you have Macports already installed then run the following command in terminal:
sudo port install tag
To add the tag 'footag' to a file or directory:
tag -a "footag" /path/to/filename
List the tags for a file,etc
tag -l /path/to/filename
Further help:
tag -h
-
Mar 17, 2015 1:23 PM in response to julesforumby L. Mariachi,Thank you! I had no idea that utility existed.
-
Mar 17, 2015 2:58 PM in response to thomasbestby VikingOSX,-- tag colors in Mavericks or later
property labelColor : {none:0, orange:1, red:2, yellow:3, blue:4, purple:5, green:6, gray:7}
set myPF to POSIX path of ((path to desktop) as text) & "filename.ext"
tell application "Finder"
set label index of (POSIX file myPF as alias) to blue of labelColor
end tell
-
Apr 12, 2016 3:34 AM in response to thomasbestby T2YUKI,Hi,
I'm reading Everyday AppleScriptObjC 3rd Edition by Shane Stanley and I've just found how to get tags of a file (in Chapter 14, 'One For the Files'). We can use the -getResourceValue:forKey:error: method of the NSURL class, like the following:
on tagsOfFileAtPath:POSIXPath
set theURL to current application's class "NSURL"'s fileURLWithPath:POSIXPath
set {theResult, theTags, theError} to theURL's getResourceValue:(reference) forKey:(current application's NSURLTagNamesKey) |error|:(reference)
if theResult as boolean is false then
error (theError's localizedDescription() as text)
else
set theTags to theTags as list
if theTags = {missing value} then set theTags to {}
return theTags
end if
end tagsOfFileAtPath:
The NSURL class has also the -setResourceValue:forKey:error: method and we can use this method to add tags to a file, like the following:
on addTag:aList toTheFileAtPath:POSIXPath
set newTagList to (its tagsOfFileAtPath:POSIXPath) & aList
set newTag to current application's NSArray's arrayWithArray:newTagList
set theURL to current application's class "NSURL"'s fileURLWithPath:POSIXPath
set {theResult, theError} to theURL's setResourceValue:newTag forKey:(current application's NSURLTagNamesKey) |error|:(reference)
if theResult as boolean is false then
error (theError's localizedDescription() as text)
else
return (its tagsOfFileAtPath:POSIXPath)
end if
end addTag:toTheFileAtPath:
In case of Preview.app, we can use those scripts by the following statements:
use AppleScript version "2.4"
use framework "Foundation"
set thePOSIXPath to path of front document of application "Preview"
try
its addTag:{"Tag1", "Tag2"} toTheFileAtPath:thePOSIXPath
on error errMess
-- handle the error
end try