A script to randomise item order in a folder

I wonder if it’s possible to create a script that would shuffle items in a folder.

For instance, I collect posters of movies I want to watch someday. I have hundreds of posters in a special DEVONThink folder. If I sort items by name or date, I see the same posters on top, but rarely get to posters that are in the middle.

I want a script that would randomise the order, so that different posters “bubble up” each time, letting the ones I forget about resurface.


That’s how I imagine it could work:

Set Folder Sorting to “Unsorted”
Count items inside a folder (=N)
Assign numbers from 1 to N to each item in a folder (without actually renaming them)
Get a random sequence (1; N)
Rearrange items according to the random sequence


No idea if it’s possible to do. Can anyone help? Thank you!

I don’t think that is going to be possible with a script, but perhaps someone will prove me wrong.

You could create a smart group that may give you what you are looking for. The smart group pictured below will give you all the posters that have not been opened in the last number of days that you determine.
Screen Shot 2018-01-17 at 06.33.42.png

It’s indeed possible by moving the items first to another group and then back to the original group:


-- Randomize children of selected group

tell application id "DNtp"
	try
		set theSelection to the selection
		if (count of theSelection) is not 1 then error "Please select one group."
		set theGroup to item 1 of theSelection
		if type of theGroup is not group then error "Only groups are supported."
		
		set theChildren to children of theGroup
		set theCount to count of theChildren
		if theCount is greater than 1 then
			set theRoot to (root of database of theGroup)
			repeat with theChild in theChildren
				move record theChild from theGroup to theRoot
			end repeat
			
			repeat while theCount is greater than 0
				set n to (random number from 1 to theCount)
				move record (item n of theChildren) from theRoot to theGroup
				set i1 to n - 1
				set i2 to n + 1
				if (i1 > 0 and i2 ≤ theCount) then
					set theChildren to (items 1 thru i1 of theChildren) & (items i2 thru theCount of theChildren)
				else if (i1 > 0) then
					set theChildren to items 1 thru i1 of theChildren
				else if (i2 ≤ theCount) then
					set theChildren to items i2 thru theCount of theChildren
				end if
				set theCount to theCount - 1
			end repeat
		end if
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink Pro" message error_message as warning
	end try
end tell

Well, there you go! :smiley:

Wow! It works like a charm - thank you so much.