Tags and labels with Mavericks

Maybe this short example may help.

use theLib : script "tags Lib"
use scripting additions

set theFile to (choose folder)
theLib's setTags:{"tag1", "tag2", "tag3"} forPath:(POSIX path of theFile)
set tags1 to theLib's returnTagsFor:(POSIX path of theFile)
log tags1 --> (*tag1, tag2, tag3*)
theLib's setTags:(rest of tags1) forPath:(POSIX path of theFile)
set tags2 to theLib's returnTagsFor:(POSIX path of theFile)
log tags2 --> (*tag2, tag3*)
theLib's addTags:{"tabouret"} forPath:(POSIX path of theFile)
set tags3 to theLib's returnTagsFor:(POSIX path of theFile)
log tags3 --> (*tag2, tag3, tabouret*)
theLib's setTags:{item -1 of tags3} forPath:(POSIX path of theFile)
set tags4 to theLib's returnTagsFor:(POSIX path of theFile)
log tags4 --> (*tabouret*)

Yvan KOENIG (VALLAURIS, France) jeudi 27 mars 2014 19:12:36

Do you not see the results as Yvan has posted?

I see it and think I have a vague understanding. Am I correct in assuming this is to assign a number of tags and that tabouret is the only one specified?

Also, I would like to assign a date tag to a newly duplicated file that would signify it’s creation/duplication date and keep getting and error -4960 which seems to refer to the “foundation” portion of the script when I try to apply it in this manner. Any idea what this would mean and how to remedy it?

I’m curious also as to whether the following shell script would be a more concise option and if it could be configured to do what I’d like:

do shell script "xattr -w com.apple.metadata:_kMDItemUserTags '(\"orange\\n6\",\"test\")' ~/desktop/Shell test file"

I’ve been trying to find a breakdown of what things like the “n6” refer to. Please bear with me, I’m not finding a lot consistent of information and am trying to get a handle on it.

Thanks in advance for any insight.

All the named tags were assigned by the script
tag1, tag2, tag3
then tag3 was removed
then tabouret was added
at last tag2 & tag3 were removed.

I thought that it was sufficiently clear.
The Event Log is :

tell application “AppleScript Editor”
choose folder
→ alias “Macintosh HD:Users:¢¢¢¢¢¢¢:Desktop:animaux:renards:”
end tell
(tag1, tag2, tag3)
(tag2, tag3)
(tag2, tag3, tabouret)
(tabouret)

Here (10.9.2 used in French) I didn’t got the error -4960.

Yvan KOENIG (VALLAURIS, France) jeudi 10 avril 2014 23:02:18

I have absolutely no doubt that you find clarity in this. I am merely trying to learn and have not as of yet found a source rudimentary enough for a simpleton such as myself to understand. I would imagine even someone as knowledgable as yourself began somewhere. I truly appreciate your taking the time to offer your input, but please understand this thread is pretty much all the information I have on this subject.

Yes – the command requires a list, and in that case the list has a single item.

Without seeing your code, I have no idea. I assume you’re converting the date to text. Is it too long?

I’m not sure what you mean by concise. It’s less code, but unless you’re putting it in a library, using it means more code. In any event, how concise code is is not often a good guide on what to do.

Hi Shane,
Again thank you for your reply I appreciate your taking the time to explain what I’m assuming is fairly rudimentary- I’ve been toying with this idea for about a week now and just today noticed it works as I wanted it to but only after running it once. In other words I get the -4960 error the first time I run it and then it works perfectly. I wish I knew why it triggers the error and then works, but I can definitely live with it knowing that.

I suspect you’ll find that error only happens in AppleScript Editor, first time after compile – it shouldn’t happen elsewhere. It’s a bug in AppleScript Editor.

I kind of hate to bring this up again, I seemed to annoy some people last time, but I’m still trying to learn. Please bear with me.

When I try to use above mentioned feature script in Yosemite, I get the error:

error “The variable |NSURL| is not defined.” number -2753 from “NSURL”

I’ve been looking for two days now - Is there something that has changed (Possibly the ASObjC portion since everything works up to this point) in Yosemite the makes this portion obsolete? It worked perfectly until switching.

Thank you.

No, nothing changed in Yosemite that would make it stop working.

Open the library script and make sure it says either NSURL or |NSURL|, and not |nsurl|.

I just changed it. It Was |NSURL|, changed it to NSURL. It seems I read somewhere it might make a difference.

Still got the error “The variable |NSURL| is not defined.” number -2753 from “NSURL”

This is the AppleScript/Objective-C Library portion. If I recall correctly, I just changed the name when I started using it.

on returnTagsFor:posixPath -- get the tags
	set aURL to current application's NSURL's fileURLWithPath:(posixPath) -- make URL
	set {theResult, theTags} to aURL's getResourceValue:(reference) forKey:(current application's NSURLTagNamesKey) |error|:(missing value)
	if theTags = missing value then return {} -- because when there are none, it returns missing value
	return theTags
end returnTagsFor:

on setTags:tagList forPath:posixPath -- set the tags, replacing any existing
	set aURL to current application's NSURL's fileURLWithPath:posixPath -- make URL
	aURL's setResourceValue:tagList forKey:(current application's NSURLTagNamesKey) |error|:(missing value)
end setTags:forPath:

on addTags:tagList forPath:posixPath -- add to existing tags
	set aURL to NSURL's fileURLWithPath:posixPath -- make URL
	-- get existing tags
	set {theResult, theTags} to aURL's getResourceValue:(reference) forKey:(current application's NSURLTagNamesKey) |error|:(missing value)
	if theTags ≠ missing value then -- add new tags
		set tagList to (theTags as list) & tagList
		set tagList to (current application's NSOrderedSet's orderedSetWithArray:tagList)'s allObjects() -- delete any duplicates
	end if
	aURL's setResourceValue:tagList forKey:(current application's NSURLTagNamesKey) |error|:(missing value)
end addTags:forPath:

That’s missing the most important line at the start:

use framework "Foundation"

Just added that and checked it, but still getting same error.

Could it have something to do with reinstalling my system? I installed a new drive about a month ago and wanted to do a clean install. A setting I might have changed?

No, there’s no setting involved. Go here:

www.macosxautomation.com/applescript/apps/Script_Libs.html

and download a new copy of FileTagsLib 1.0.0. Follow the instructions.

Wow. That’s perfect! Exactly what I was looking for - thank you.

I’ve come up on another issue I’m wondering if you could help with. When I run the following:

use FileTagsLib : script "FileTagsLib"
use scripting additions

set listOfTags to "DID IT WORK?"

set fileOrPosixPath to POSIX path of (choose file)

--set fileOrPosixPath to "/Users/gavin/Desktop/Desktop on 05.18.16/Screen Shot 2016-05-04 at 7.57.54 AM.png"

display dialog fileOrPosixPath

tell FileTagsLib to addTags:listOfTags forItem:fileOrPosixPath

I get the following error:

“Result:
error “The operation couldn’t be completed. (OSStatus error -8050.)” number -2700 from «script “FileTagsLib”» to item”

In other words, if I specify the location it works fine, if I ask for it it doesn’t work. As far as I can tell with display dialog, it seems to be the same. Could you help me with the phrasing?

As far as I know, listOfTags is supposed to be a list but you define it as a string.

Would be useful to use :
set listOfTags to {“DID IT WORK?”}

Yvan KOENIG running El Capitan 10.11.4 in French (VALLAURIS, France) lundi 9 mai 2016 22:07:24

Yes. I suppose it should be a list as it is referred to that, but I wasn’t looking for it to be a list yet, I just wanted to see if it worked while changing as little of the original code as possible.

I wrote because here, after setting the variable to a list, the script worked flawlessly.

I tested with :

use FileTagsLib : script "FileTagsLib"
use scripting additions

set listOfTags to {"DID IT WORK?"}

set fileOrPosixPath to POSIX path of (choose file)

set oldListOfTags to FileTagsLib's returnTagsFor:fileOrPosixPath
log oldListOfTags
--set fileOrPosixPath to "/Users/gavin/Desktop/Desktop on 05.18.16/Screen Shot 2016-05-04 at 7.57.54 AM.png"

display dialog fileOrPosixPath

tell FileTagsLib to addTags:listOfTags forItem:fileOrPosixPath

set newListOfTags to FileTagsLib's returnTagsFor:fileOrPosixPath

The events log was :

tell application "Script Editor"
	choose file
end tell
(*Violet*)
tell application "Script Editor"
	display dialog "/Users/userName/Desktop/4Shane.scpt"
end tell
Résultat :
{"Violet", "DID IT WORK?"}

And of course, when I tested with the variable defined as a string I got :

tell application "Script Editor"
	choose file
end tell
(*Violet*)
tell application "Script Editor"
	display dialog "/Users/userName/Desktop/4Shane.scpt"
Résultat :
error "L'opération n'a pas pu s'achever. (OSStatus erreur -8050)." number -2700 from «script "FileTagsLib"» to item

Yvan KOENIG running El Capitan 10.11.4 in French (VALLAURIS, France) mardi 10 mai 2016 10:06:19

I see now. That’s embarrassing - like you said, I added the parenthesis and It worked. I didn’t understand you were referring to that aspect of the code. I even tried adding commas to the individual words and adding the parenthesis, but without the quotation marks. Thank you for spelling it out for me.