How to let the pictures in the groups become the icons in batches?

I want to set custom icons for groups in batches. Let the pictures in the groups become the icons.

The background is this:
I have collected a lot of movie posters. I want to create a group for each poster to represent the related movie and store videos in them. I have changed a lot of movie posters into groups using the script shared by netizens in this community. So now I have many groups that there is only one picture in each one of them.
I want to add tags named “movie” to groups, not to pictures, because there will be a place to store the replicated items in this way. But the problem is when I search for the tag “movie”, the results are all groups. So I want to make them more eye-catching by customizing the icon, just like some Apps will create a cover for the notes.


I have used Notion before. the Page in Notion can store very rich content such as notes, videos, pictures… and Pages can be connected to each other. But due to network problems, I had to return to local file management. In fact, Group in DTP is very similar to Page in Notion and the Replicate function in DTP is more flexible than Relation in Notion.The only problem is that the files enclosed by the group cannot be seen through. My solution is to use a script to create the item into the group, and add highly visual content, like pictures, as the icon of the group.

My ideal script should look like this:

  1. Create items as a group (create a separate group for each selected item)
  2. Add the tags of item to the group and delete the tags of item
  3. Keep the “modification time” of the group consistent with item
  4. Add the Thumbnails of item in the “as icon” view to the icon of the group
    (Am I too greedy :smile: )

I wonder if anyone in this community has raised similar questions? If so, I’m sorry I didn’t find it. I hope someone can answer this question again. Thank you!

Could you post a link or the script? Are the images PNGs?

I can’t directly paste the link here, but you can search for the keyword “Group each item script”. A netizen called “cgrunenberg” gave the script he wrote.
I’m not very good at using Apple script. This script seems to be able to create a separate group for each selected item, and it can also add the same tags to the group as items. But when this script is running, there will be incomplete tag additions some times.

Try this with duplicated records

-- Group images, copy tags, modification date and thumbnail, remove tags

tell application id "DNtp"
	try
		set theRecords to selection of think window 1
		if theRecords = {} then error "Nothing selected."
		
		repeat with thisRecord in theRecords
			if type of thisRecord = picture then
				set thisName to my recordName(name of thisRecord, filename of thisRecord)
				if thisName contains "/" then set thisName to my replaceString(thisName, "/", "\\/")
				set thisGroup to create location (location of thisRecord) & thisName
				move record thisRecord to thisGroup
				set tags of thisGroup to tags of thisRecord
				set tags of thisRecord to {}
				set modification date of thisGroup to modification date of thisRecord
				set thumbnail of thisGroup to thumbnail of thisRecord
			end if
		end repeat
		
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
	end try
end tell

on replaceString(theText, oldString, newString)
	local ASTID, theText, oldString, newString, lst
	set ASTID to AppleScript's text item delimiters
	try
		considering case
			set AppleScript's text item delimiters to oldString
			set lst to every text item of theText
			set AppleScript's text item delimiters to newString
			set theText to lst as string
		end considering
		set AppleScript's text item delimiters to ASTID
		return theText
	on error eMsg number eNum
		set AppleScript's text item delimiters to ASTID
		error "Can't replaceString: " & eMsg number eNum
	end try
end replaceString

on recordName(theName, theFilename)
	set theSuffix to my getSuffix(theFilename)
	if theName ends with theSuffix then set theName to characters 1 thru -((length of theSuffix) + 2) in theName as string
	return theName
end recordName

on getSuffix(thePath)
	set revPath to reverse of characters in thePath as string
	set theSuffix to reverse of characters 1 thru ((offset of "." in revPath) - 1) in revPath as string
end getSuffix
1 Like

Thank you so much! ! ! ! It’s perfect for pictures! But is it possible to make this script applicable to more formats of files? I guess files of various formats have thumbnails in the “as the icon” view. I wonder if this thumbnail can be used as a group icon?

Sure, just remove or comment out (prefix a line with # or -- )

if type of thisRecord = picture then 

and the closing

end if

awesome! ! Thank you very much! !

I found a small problem! When the script is processing some files without thumbnails, the system will pop up a prompt like “DEVONthink 3 got an error: Can’t set thumbnail of content id XXX of database id 3 to thumbnail of content id XXX of database id 3” .
In fact, even if there is no thumbnail, the group is generated, but the script will stop running. Can this be improved?

That’s because it was initially written to process records with thumbnails :wink:

This should work

-- Group images, copy tags, modification date and thumbnail, remove tags

tell application id "DNtp"
	try
		set theRecords to selection of think window 1
		if theRecords = {} then error "Nothing selected."
		
		repeat with thisRecord in theRecords
			#if type of thisRecord = picture then
			try
				thumbnail of thisRecord as anything
				set thisName to my recordName(name of thisRecord, filename of thisRecord)
				if thisName contains "/" then set thisName to my replaceString(thisName, "/", "\\/")
				set thisGroup to create location (location of thisRecord) & thisName
				move record thisRecord to thisGroup
				set tags of thisGroup to tags of thisRecord
				set tags of thisRecord to {}
				set modification date of thisGroup to modification date of thisRecord
				set thumbnail of thisGroup to thumbnail of thisRecord
			end try
			#end if
		end repeat
		
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
	end try
end tell

on replaceString(theText, oldString, newString)
	local ASTID, theText, oldString, newString, lst
	set ASTID to AppleScript's text item delimiters
	try
		considering case
			set AppleScript's text item delimiters to oldString
			set lst to every text item of theText
			set AppleScript's text item delimiters to newString
			set theText to lst as string
		end considering
		set AppleScript's text item delimiters to ASTID
		return theText
	on error eMsg number eNum
		set AppleScript's text item delimiters to ASTID
		error "Can't replaceString: " & eMsg number eNum
	end try
end replaceString

on recordName(theName, theFilename)
	set theSuffix to my getSuffix(theFilename)
	if theName ends with theSuffix then set theName to characters 1 thru -((length of theSuffix) + 2) in theName as string
	return theName
end recordName

on getSuffix(thePath)
	set revPath to reverse of characters in thePath as string
	set theSuffix to reverse of characters 1 thru ((offset of "." in revPath) - 1) in revPath as string
end getSuffix

Thank you so much!!! But I still found a small problem (this is a bit difficult to solve…) I have many pictures with the same name (prefix + modification time), for these files with the same name, the script seems to put them into one group, and inherit the tag and thumbnails of a random file. (QAQ Numbering files is a little bit troublesome, I wonder if the script can be adjusted?

Sorry! I still have a small request, how about copying the creation time of the file to the group! Otherwise, it looks a little strange now that the creation time is later than the modification time! (My request is even stranger hahaha…

That’s how create location works - it gets a group at a location if it exists, otherwise it creates one.

create record with always creates a new record, so in case of records with same names this is what you want.

-- Group images, copy tags, modification date and thumbnail, remove tags

tell application id "DNtp"
	try
		set theRecords to selection of think window 1
		if theRecords = {} then error "Nothing selected."
		
		repeat with thisRecord in theRecords
			#if type of thisRecord = picture then
			try
				thumbnail of thisRecord as anything
				set thisName to my recordName(name of thisRecord, filename of thisRecord)
				if thisName contains "/" then set thisName to my replaceString(thisName, "/", "\\/")
				#set thisGroup to create location (location of thisRecord) & thisName
				set thisParent to parent 1 of thisRecord
				set thisGroup to create record with {name:thisName, type:group} in thisParent
				move record thisRecord to thisGroup
				set tags of thisGroup to tags of thisRecord
				set tags of thisRecord to {}
				set modification date of thisGroup to modification date of thisRecord
				set creation date of thisGroup to creation date of thisRecord
				set thumbnail of thisGroup to thumbnail of thisRecord
			end try
			#end if
		end repeat
		
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
	end try
end tell

on replaceString(theText, oldString, newString)
	local ASTID, theText, oldString, newString, lst
	set ASTID to AppleScript's text item delimiters
	try
		considering case
			set AppleScript's text item delimiters to oldString
			set lst to every text item of theText
			set AppleScript's text item delimiters to newString
			set theText to lst as string
		end considering
		set AppleScript's text item delimiters to ASTID
		return theText
	on error eMsg number eNum
		set AppleScript's text item delimiters to ASTID
		error "Can't replaceString: " & eMsg number eNum
	end try
end replaceString

on recordName(theName, theFilename)
	set theSuffix to my getSuffix(theFilename)
	if theName ends with theSuffix then set theName to characters 1 thru -((length of theSuffix) + 2) in theName as string
	return theName
end recordName

on getSuffix(thePath)
	set revPath to reverse of characters in thePath as string
	set theSuffix to reverse of characters 1 thru ((offset of "." in revPath) - 1) in revPath as string
end getSuffix

I tested this script in the inbox and it is perfect, but if the file is in the group, there will be some problems when running. The tag of the file will be removed, and the file will be moved from its upper group, and the script will stop in this step. What is the reason for this?

This thread started with a precise question, meanwhile I’ve no clue what your trying to do. I think it’s generally not a good idea to try to use a script that was written for one thing for another thing.

Suggestion: Start a new thread and explain from scratch what you want to do.

My description may not be accurate enough.
I still want to achieve the goal I said at the beginning, which is to convert the file into a group. The Tag, creation time, modification time, and thumbnails are all passed from the file to the group. If this script can be run in any location, it would be great!
The latest problem I encountered is that if the file is in a certain group, there will be problems when running the script, but not if the file is in the inbox.
I can indeed move the file to the inbox to run the script, and then move it back, but this script should be the most frequently used script in the future! Can it be changed better?
(Thank you so much for your script! It really helped me a lot!

That doesn’t really give me any info about that group. Easiest might be to post a screenshot of that group where I can see which record you’ve selected.

Select the database in the navigation sidebar, then (also in the navigation sidebar) drill down to the group, then select the record that you selected when the error occured and take a screenshot.

OK, I will take a screenshot for you tomorrow


You see, if the file is in the inbox (1)
The script will run normally (2)
However, if the file is in the group (3)
The tag of the file will be removed, and the file will be moved from its upper group, and the script will stop in this step (4)
This group named test is newly created, there is nothing special

In fact, I found this problem because, when I executed the script in my hierarchical group, the file was moved from the original structure and I don’t know where it went.

Over here it works as expected.

Did you select only the records that you want to process or the whole structure with groups?

It’s possible that records were moved to another parent group - but this can only happen if you’ve replicated them in the past. However in your screenshot are no replicated records.

Did you do a search?

I only select the records


The file will be saved in the inbox after running the script
It is worth mentioning that after these files come to the inbox, if you re-add the previous tags and execute the script again, it will be normal
But the strange thing is that files in some groups do not have such problems, and I don’t seem to find the difference between these groups.


Ops I find the difference!
The problem seems to be in a particular database
I created a new database, then moved the file from the content of the database that would have problems, and executed the script, and it ran normally (even if the file was moved in and then moved back, the script would run without problems)
This is so confusing :smile:
It seems that I just need to move the file, but I wonder if you know the reason?