How to create label / tag / prefix script?

Hi,

I’m a relative novice with scripting, but through the help of forum members I’ve applied some trigger scripts. I’m trying to figure out a how to create a script that will do the following

If I tag a file “High” -> label file “High” and add prefix “1_” to the file
If I label a file “High” -> tag file “High” and add prefix “1_” to the file
If I add prefix “1_” to the file -> tag file “High,” and label file “High”

In other works, whether I change the prefix (to “1_”), tag a file ("High), or label a file (“High”), I’d like a file to have all three identifiers. I’d like this to work for prefixes “2_” & “3_” ; tags “Medium” & “Low” ; and, labels " “Medium” & “Low”

Any suggestions? Thank you so much in advance for your help!

I’m going to suggest changing your mind about the approach. If I understand the requirement correctly, you would want a trigger script that you apply to individual files, and the script investigates the state of the tags, the label, and the name of the file. The script then modifies these three elements. And you want the script to evaluate each of the three elements to determine which of them changed, before changing the other two.

The logic here is a bit complex, but I’d suggest the real problem is that applying a number of trigger scripts like this in a database might artificially create some overhead that would eventually diminish the responsiveness of your database. So, I’d suggest that you instead of all these logic checks, you just have three scripts: “make me high”, “make me medium”, and “make me low” – and each script sets all three elements according to your specifications. Here’s an example of the “make me high script”:

property p_label : 1
property p_tag : "High"
property p_prefix : "1_"

tell application id "com.devon-technologies.thinkpro2"
	
	set theSelection to the (first item of (the selection as list))
	if the kind of theSelection is not "Group" then
		set the label of theSelection to p_label
		repeat with c_tag in {"High", "Medium", "Low"} -- need to remove existing priority tags
			set the tags of theSelection to my cleanList(the tags of theSelection, (c_tag as string))
		end repeat
		set the tags of theSelection to (the tags of theSelection & p_tag)
		set the name of theSelection to my replace_chars((the name of theSelection), p_prefix, "") -- need to remove existing prefixes
		set the name of theSelection to p_prefix & (the name of theSelection)
	end if
	
	
end tell

on cleanList(thelist, v_Remove)
	set newList to {}
	repeat with i from 1 to count thelist
		if ({thelist's item i} as string) is not equal to v_Remove then set newList's end to thelist's item i
	end repeat
	return newList
end cleanList

on replace_chars(this_text, search_string, replacement_string)
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to ""
	return this_text
end replace_chars

Most of this code is dedicated to cleaning up the former priority status before applying the new status – so that your document doesn’t have a mix of “high” and “low” tags, for example, or prefixes such as “1_1_1_1_” or “3_2_1_3_2”, etc.

I do a similar thing (different logic for the status) and my script is stored in the “Toolbar” subfolder of the “Scripts” folder. Scripts in that subfolder can be added to the DEVONthink tool bar. You could have three icons on your tool bar and use them to toggle among the priorities. For example.

I suppose I’ve taken this in a direction you didn’t want to go … :unamused:

I think this is almost exactly what I’m seeking. Thanks! If I understand you correctly, the only difference between my request and your suggestion is that you’re suggesting using three scripts whereas I thought to accomplish this in just one - is that correct?

Quick questions…

Ideally, I’d like to apply these triggers to roughly six groups (folders) in my Global Inbox. Is it possible to do so? If so, how would I do so? I thought one could only apply one trigger script per Group. But maybe I’m wrong - or there’s just another way to do this.

Also, I’m assuming that I would just change this section of the script for the Medium and Low labels, tags (and prefixes):

property p_label : 1
property p_tag : “High”
property p_prefix : “1_”

Is that correct?

Thanks so much again…

Oh wait. I misunderstood you. Ok, so…you’re suggesting manually applying scripts to individual files make the changes to labels / tags / prefixes. Is that correct?

Hmmm… Yeah, I see how that makes things better, performance-wise - if that’s indeed what you’re suggesting. Let me know if I’m right.

How about this… Would it be possible (easier) to have a trigger script that would just do the following…

If I tag a file “High” -> label file “High” and add prefix “1_” to the file

?

Thanks!

The thing is, I’m suggesting do not use a trigger script and avoid unnecessary overhead on your database. The script I posted isn’t triggered — it would be run against a selection of documents. So, if you had the three scripts then each of the three scripts would change the status to “high”,“medium”, or “low”, respectively.

Only one triggered script at a time can be applied to a group. If you really need to have a triggered script, then you’d need to trash my suggestion and work through the logic tree that a single, triggered, script would need. Some of what I posted could be refactored in your new script, though.

Right. I get it now. Thanks. And sorry for the misunderstanding. I’m wondering what you thought about the idea of just making the one trigger script I mentioned above (i.e., changes via tags). If you think that’s advisable, that might be the way I’d like to go. I’d also be interested in creating the three other non-trigger scripts. Would I just need to change this section of the script for the Medium and Low labels, tags (and prefixes):

property p_label : 1
property p_tag : “High”
property p_prefix : “1_”

Thanks so much again…

Yes, that’s right.

To combine all the actions into one all-purpose script you have to consider the number of tests your logic would need to make: you have three variables (label, name, and tags) and three possible states for each (“high”, “medium”, “low”), and you have three possible starting conditions for the test == so you’ll need to create a logic tree that performs (at most) (3x3x3)-3 tests for each document each time the script is triggered.

I hesitate to write all this out, because I’m lazy and figured three simple scripts is easier to deal with than one mega script :laughing: