How to prefix part of the text content of a particular tag to the file name

I wonder if applescript can perform the following operations on the selected files in batches?

  1. Read the tags of the selected file (the selected file may need to have same tags ), and display all tags in a pop-up window
  2. After selecting a tag, edit text
  3. Add the text to the name of the file as prefix


  1. Read the tags of the selected file and get the text of the tag whose name started with a specific number
  2. Edit the text
  3. Add the text to the name of the file as a prefix


  1. Read the tags of the selected file, and get the text of the last tag arranged by name
  2. Edit text
  3. Add the text of the tag to the name of the selected file as a prefix

About text editing:

tag:03 / XXX
prefix:XXX / file name

I don’t know which one is simpler? Thank you all here first.

Why are you trying to prefix tags into the filename?

Yes, that’s possible

Well I don’t know if my current document management system is reasonable, but I designed it like this:

  • I unchecked the Exclude group from tagging option
  • Use ordinary tags to describe the group (that is, only to tag the group)
  • Use the group to describe the file (that is, to tag ordinary files with group tag)

For example:

  • Group name: “Harry Potter”
  • Tag: Movie

The files in this group will be described by a group tag named “Harry Potter”

But sometimes there will be different groups with the same name such as:

  • Group name: “Harry Potter”
  • Tag: Novel

Because the classify function cannot display tag information of the group, so I want to add it to the name of the group as a prefix. Although it can be done in the way of smart rules, but I must add smart rules to all tags , The workload is a bit heavy!

That’s great! There are so many talents in this community!

Emmmm…Your question really made me think about whether this requirement is really necessary. I don’t know if DT has any way to display the Tag information of the search content? If so, I don’t think it is necessary to add a prefix :wink:

You can Control-click the headers of the search result and toggle headers on and off.

wow! I haven’t found it before! Thank you very much~It would be great if See Also & Classify could also display tag information!

You’re welcome.

See Also & Classify could also display tag information!

No promises, but the request is noted.

If anyone (Maybe no one :stuck_out_tongue_winking_eye:) has the same needs as me, please see here~ Add this script to smart rule

tag name: nn XXX
file name: YYY
result: XXX / YYY

on performSmartRule(theRecord)
	tell application id "DNtp"
		repeat with thisRecord in theRecord
			set thisName to name of thisRecord 
			set theTag to tags of thisRecord
			repeat with thisTag in theTag
				set theTagname to characters 1 thru 2 of thisTag -- get the tag's number, 1 and 2 are the starting and ending positions of the number in the name
				if (theTagname as text) = "23" then -- Here can be changed to your number
					set theprefix to thisTag as string
				end if
			end repeat
			set theprefix to characters 4 thru -1 of theprefix -- Get part of the tag's name that does not contain the number. 4 and -1 are the starting and ending positions of the name
			set thisPrefix to (theprefix as text) & space & "/" & space -- Edit the connection method of prefix and file name
			if thisPrefix is not in thisName then
				set name of thisRecord to thisPrefix & thisName
			end if
		end repeat
	end tell

end performSmartRule

One thing to change: Remember to not use foward slashes in filenames. Use hyphens, underscores, or spaces.

:scream: What are the consequences of this… Many of my files are named like this…

Many of my files are named like this…

Named where - in DEVONthink?


Look at the Path section in the Info inspector. What do you see?

The slash is used as a path separator in Unix, which is the base of Mac OS (loosely spoken). So “/Users/yourname/Documents/xxx” is the full name of the file xxx located in the Documents folder of you home directory. Renaming this file to “xxx/yyy” is not even possible in the Finder, for obvious reasons.


Excuse my shouting, but I wanted to get the message across.

What is the purpose of storing (parts of) the tag in the file name, anyway? Duplicating information like this shouldn’t be necessary, I think.

I don’t know if I understand what you mean, as shown in the picture~

See how the forward slash has been changed to a hyphen in the actual filename? That’s because using the forward slash in a filename is not possible.

It could be used in the name of a group created in DEVONthink as the groups don’t exist in the filesystem, but forward slashes shouldn’t be used in filenames

Thank you for your kind reminder, but… emmm… I don’t know why I can use yyy/xxx to name the folder in finder… :thinking:

As I said in my previous reply, some files have the same file name but different tags. Adding the tag information to the file name allows me to indirectly get the tag information when using classify and see also function.

This is a screenshot of the notion database when linking. I was a notion user before, so I kept some usage habits…I don’t think notion is better than dt here, but it’s very intuitive to be able to see the tag column when linking files~

Just to give an example, it only represents personal opinion~ :stuck_out_tongue: