AppleScript to file document based on tags

Hi All
I am new to devonthink and AppleScript. I use hazel rules to tag documents and then move them to DT3’s Global Inbox

Every file I move to DT3 has at least 2 tags:
A # handler (such as #archive) which tells me which database a file should go into
A 1234-012 | tag (such as 1111-001 | personal documents) which tells me into which folder a document should be filed into

I would like to automate the filing of documents based on this logic. I suppose I can do this using AppleScript. I tried smart rules, but I would need a smart rule for each combination of tags with would require hundreds of rules

The script should read the document’s tags and move into the correct folder inside the correct database

Can someone help me?

Seems straightforward
"Not clear on how these tags are stored "” (answered Devonthink)

Untested sample code is listed below and shows reading the document’s tags

on performSmartRule(theRecords)
   tell application id "DNtp"
      repeat with theRecord in theRecords
         set theTags to tags of theRecord
         set theFilingGroup to my IdFilingGroup(theTags)
         set theRecord to move record theRecord to theFilingGroup
      end repeat
   end tell
end performSmartRule

on idFilingGroup(theTags)
   repeat with theTag in theTags
    ...
   end repeat
end idFilingGroup

they are devonthink tags

If you don’t need the tags for anything else, you could move the documents into the correct DT groups directly from Hazel, without tagging them first.
That would save one script.

I’ve thought about it, but I’ve found that that second revision regularly changes my mind on certain tags.

But will keep that in mind!

Thank you! This is great. Will test and come back if I find any problems!

1111-001 | personal documents

Is this the full name of the group or is personal documents a subgroup of 1111-001?

1111-001 | personal documents is a group and it is unique inside a database (although there could be one such folder inside each database)

My logic is:
The #XXXXX tells me which database a document should go to
The “1111-001 | personal documents” kind of tag tells me which group to place it into inside the above database

The caveat is that a document might have other tags, albeit none with the same structure as the above ones

Not knowing your specific situation and the reasons for your naming convention, have you ever considered changing the approach to make 1111-001 the group and personal documents a subgroup? Or even…

1111
   > 001
     > personal documents

?

1 Like

I actually use a Johny decimal approach

The structure goes something like this

1000 - Home stuff
1100 - individuals
1110 - Dad
1111 - Documents
1111-001 | personal documents
1111-002 | travel documents