Advice needed: is it crazy to try and create a script to auto move files to folders based on prefix/name?

Ok, so ill start by saying i have very minimal coding skills, but using chatgpt in recent months has helped alot :slight_smile:

before i embark on this i thought id check first if this is a stupid/unfeasible idea with the community here :slight_smile:

so i have been using a Johnny decimal like system for a few years now and pretty happy with it. here is an example

im trying to automate the process of filing from inbox as much as possible, and thus for files i know belong to a specific folder (ie 31.01 software) i prepend 31.01-- to the file when i save it to DEVONthink. ie 31.01--2025-02-16-New Obsidian Notes from Shortcuts.pdf

im thinking to try and create a script for a smart rule that would look at new inbox files and if there is the XX.YY-- prepended would auto file it to the corresponding folder ie take 31.01--2025-02-16-New Obsidian Notes from Shortcuts.pdf and put it in the 31.01 software.

Does that make sense? is this achievable? whats the best way to do so? a script (applescript, js), with just DEVONthink smartrules?

would really appreciate everyone’s thought in this

best

Z

How exactly do you save files to DEVONthink? Is the prefix added before or after adding items to the inbox? But a smart rule triggered On Import that executes a script is probably the best way.

thx @cgrunenberg !

yes i will add the prefix when i save the file to a folder DEVONthink monitors and moves to its inbox.
Do you suggest an applescript or js approach for this?

thx

Z

That’s a matter of preferences but we write AppleScripts, generally speaking.

And some others write JavaScript scripts. There’s no fundamental difference in what you can do with either language, so it’s mostly a matter of personal preference.

I still consider myself a beginner in AppleScript and scripting DT, but I felt this was a reasonable challenge and learning opportunity.

I would suggest looking at chrillek’s approach here: Script: Move records to groups according to tag.

However… I don’t know how many groups you have in your JD system, or how often you create new ones. It could be annoying to enter and update them all by hand, though it’s probably more performant.

Assuming you only have one group in the JD hierarchy with a name beginning with XX.XX, it seems easier to use the search command.

I don’t use JD, so I just created a few mockup groups to test. The following works, but I don’t know how well it scales:

Smart rule script (written for DT3):

-- File to Johnny.Decimal
on performSmartRule(theRecords)
	tell application id "DNtp"

		-- Get parent group or database of your Johnny Decimal system
		set JD_dir to get record with uuid "x-devonthink-item://..."
		
		repeat with theRecord in theRecords
			set theName to name without extension of theRecord as string
			
			-- Get decimal value (XX.XX at beginning of record name)
			set JD_rec to characters 1 thru 5 of theName
			
			-- Get name without `XX.XX--`
			set newName to characters 8 thru end of theName as string
			
			-- Find Johnny.Decimal group matching the file
			-- by searching and getting top result (there should only be one)
			set theResults to search "name:<" & JD_rec & " type:group" in JD_dir
			
			-- If there is a match, move file
			if theResults ≠ {} then
				set JD_group to the first item in theResults
				move record theRecord to JD_group
				-- Update filename
				set name without extension of theRecord to newName
			else -- If not, add message to log
				log message info "Johnny.Decimal location \"" & JD_rec & "\" does not exist" record theRecord
			end if
		end repeat
	end tell
end performSmartRule

wow @troejgaard this is so amazing!

this seems to work well apart from 2 issues:

  1. i keept getting an error each time the script launches, though the file is indeed filed correctly. here is the error

  2. it seems that if launched manually, it only processes 1 file at a time? is that by design?

thx so much, this is so great!

Z

Hmm. I get no errors and can’t reproduce what you’re seeing. Looks like it’s the renaming that produces an error.

  • Just to be sure, did you change the JD_dir item link to the relevant location on your system?
  • Does DEVONthink have Full Disk Access?
  • Is the setting General > Appearance > Show filename extensions on or off? I had it on. Changing it doesn’t alter my result, but either way, the script should probably use name without extension instead of name. Does that make a difference?
  • What version of DEVONthink are you running this in? (I’m still using DT3, so I haven’t really looked at changes in the scripting dictionary of DT4.)

No. What exactly are you doing?

I forgot that it’s best to include “Perform the following actions: On Demand” in smart rules. But even without it, I have no problem manually applying the smart rule to multiple items at the same time.