How is title sanitzed to a filename?

I’m writing a script (outside of DT) where I need to determine the filename based on a DT title, so I’m trying to figure out what the sanitization logic is.

A while ago I wrote a small AppleScript, but I guess the logic is a bit more complex. @cgrunenberg would you be able to give some pointers how DT sanitizes a record name to a file name?

on sanitize(str)
	-- We also exclude # to prevent from unwanted taggingev
	set replaceChars to {"%", "&", "/", "?", "<", ">", "\\", "*", "|", ":", "*"}
	set stripChars to {"#"}
	set theResult to ""
	repeat with char in str
		if char is in replaceChars then
			set char to "-"
		else if char is in stripChars then
			set char to ""
		end if
		set theResult to theResult & char
	end repeat
	if theResult ends with "." then set theResult to characters 1 thru -2 of theResult as string
	return theResult
end sanitize
  • What do you want with the filename?

  • Why don’t you just query it directly from the record?

I’m using Obsidian to edit my MarkdownAnnotation documents; when I change a title argument in the Markdown frontmatter I want to rename the file to the exact filename as DT would rename a document with the same title.

When renaming I don’t have access to DEVONthink (changes are synced later)

That’s difficult to predict as even indexed vs. imported matters. E.g. are there already files/folders with the same filename? Which characters are supported by the filesystem? And is the compatibility option (see Preferences > General > General) enabled?

Thanks. It’s in this situation:

  • For indexed items, macOS filesystem (is that APFS?)
  • Compatibility is enabled
  • No previous item with the same name exist

On newer systems: probably.

Additionally you might want to strip white characters at the beginning/end of the title and replace one or more white characters in the filename with one space as DEVONthink performs these steps currently too.

Thanks @cgrunenberg!