Script: Copy Devonthink textbundle/markdown folder to Obsidian folder

This script copies .textbundle folders located in DevonThink into your Obsidian folder. It renames the internal text.markdown file in the textbunlde to the name of the record with .md. It also renames the ...textbundle folder to remove the extension.

The script has run and batch invocation handlers.

I am using this script to move textbundle annotations generated on PDFs through the chain Highlights → (share/export as textbundle markdown) Devonthink → (copy folder with assets + md file) Obsidian.

(*
copy textbundles with markdown files from Devonthink to Obsidian
version 2021-07-28
author jjw
*)

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

-- define your Obsidian path
property ObsidianPath : "Volumes/Databases/Obsidian/inbox"
-- define a prefix for the markdown file
property AnnotationFilePrefix : "Annotations_"

on run {}
	tell application id "DNtp" to set theRecords to the selection
	my performSmartRule(theRecords)
end run

on performSmartRule(theRecords)
	set theCount to 0
	tell application id "DNtp"
		repeat with theRecord in theRecords
			set Success to false
			set theName to the name of the theRecord as text
			set theDestinationFolderName to (ObsidianPath & "/" & theName)
			try
				set theDestinationFolder to POSIX file theDestinationFolderName as alias
				set DoIt to false
			on error
				set DoIt to true
			end try
			if DoIt is true then
				set theFilePathName to the path of theRecord as text
				set theCmd to "cp -r " & (the quoted form of theFilePathName) & " " & (the quoted form of ObsidianPath)
				try
					do shell script theCmd
					set Success to true
				end try
				if Success is true then
					set theCount to theCount + 1
					set theCurrentMDFileName to (ObsidianPath & "/" & theName & ".textbundle/text.markdown")
					set theNewMDFileName to (ObsidianPath & "/" & theName & ".textbundle/" & AnnotationFilePrefix & theName & ".md")
					set theCmd to "mv " & (the quoted form of theCurrentMDFileName) & " " & (the quoted form of theNewMDFileName)
					do shell script theCmd
					set theCurrentMDFileName to (ObsidianPath & "/" & theName & ".textbundle")
					set theCmd to "mv " & (the quoted form of theCurrentMDFileName) & " " & (the quoted form of theDestinationFolderName)
					do shell script theCmd
				end if
			end if
		end repeat
		display alert "Successfully copied " & theCount & " records to your Obsidian destination."
	end tell
end performSmartRule

I welcome comments or improvements.


JJW

1 Like