Make a tag a subtag to another tag via Applescript

In the Tips, Tricks & Troubleshooting forum I started a thread “Best way to make tag subtag to another tag in long tag list”. Turns out all UI-driven options to so are rather cumbersome (if your list of tags is pretty long)…

So I thought maybe scripting is easier: Is there a way to make a tag a subtag to another tag via Applescript? - Thanks for any pointers!

Is it possible? Likely. However…

  1. This is likely not something that would be routinely done, so scripting it would offer little to no future fruit.
  2. You’d still have to build a list of Tags and choose from a list of them, so you’re essentially in the same boat to some degree.
    I think you’re imagining some prescience in AppleScript that isn’t there (like it knowing “Peter” is your “friend”).
  3. If the hierachy was more than one level deep, you’d have an even more complex report and display task to deal with.

I suggest this is best done manually on an as-need basis.

I’m thinking of a UI like this:

  1. Start script
  2. Prompt for name of tag to move (here peter)
  3. Prompt for name of tag to move it under (here friend)
  4. Script checks for existence and un-ambiguity of tag names
  5. Script moves tag

What are the Applescript commands for moving a tag?

And if you mistyped or forgot the tag you entered?
Also, you’re trading mouse movement and menus for typing (which is very prone to error), so is the trade-off really a good one?

PS: There’s no specific “command for moving a Tag”.

Here is a proof of concept approach, commented for teaching. Again, I’m not advocating this process, but showing it is possible and also may be adapted for other uses.

-- DT Nest Tags_teaching
-- Created by Jim Neumann / BLUEFROG on Tue Jan 08 2019.
-- Copyright (c) 2018 BLUEFROG / DEVONtechnologies, LLC. All rights reserved.
--- Note this is NOT checking for already nested Tags. This is moving a root level Tag into another root level Tag.

tell application id "DNtp"
	
	--- Specify the Tag to move and check for its existence. ---------------------------
	set tagToMove to text returned of (display dialog "What Tag do you want to move?" default answer "")
	-- Ask for the name of the Tag to move.
	
	if tagToMove is not "" then
		--- If the User didn't enter an empty Tag…
		set recordToMove to get record at ("/Tags/" & tagToMove)
		-- Check for the existence of the Tag.
	else --- Otherwise, Issue a warning.
		display alert "You entered a blank Tag." as critical
		return -- A return stops the script as there's no point in continuing.
	end if
	
	if recordToMove = missing value then -- If no Tag was found…
		display alert "The Tag, " & tagToMove & ", could not be found. It doesn't exist or you mistyped it." & return & "Remember: Tags are case-senstive." as critical
		return
	end if
	
	--- Specify what Tag to move the Tag into and check for its existence. ---------------------------
	set tagToReceive to text returned of (display dialog "Where do you want to move the Tag?" default answer "")
	
	if tagToReceive is not "" then
		set recordToReceive to get record at ("/Tags/" & tagToReceive)
	else
		display alert "You entered a blank Tag." as critical
		return
	end if
	
	if recordToReceive = missing value then
		display alert "The Tag, " & tagToReceive & ", could not be found. It doesn't exist or you mistyped it." & return & "Remember: Tags are case-senstive." as critical
		return
	end if
	
	---If neither conditional above errors, the Tags should be found, so go ahead and move the Tag into the other.
	move record recordToMove to recordToReceive
end tell

Looks awesome, Jim!

One question though: How does the script know which database the entered tags should belong to? Does it just use the database selected in the front window?

Yes, it works on the current database only.

Tested the script - and used it already in a real-world scenario. Works great!

Thanks Jim - you are certainly an Applescript wizard. :slight_smile:

You’re welcome. I hope you (and others) learn something from it.