Set path property of a record


I keep my pdf files organized by subject in folders and I want to recreate the same group hierarchy in DT. The following script is triggered when an indexed item is selected for the first time and it would try to move the item simultaneously in the DT database and the file system. The only problem is that the path property for a record is read-only (line with a @). Is there any way around this? If not would you consider allowing it in a future release?


property MyDatabase : "chosen_database"
property IndexedFolder : "path_to_indexed_folder"

on triggered(theRecord)
	tell application id "DNtp"
		set theGroup to display group selector for database MyDatabase
		move record theRecord to theGroup
		set destFolder to IndexedFolder & the location of theGroup & the name of theGroup
		set fullDestName to destFolder & "/" & name of theRecord
		set fullOrigName to path of theRecord
@		set theRecord's path to destFolder
		set cmdStr to "if [[ ! -d " & quoted form of destFolder & " ]]; then mkdir -p " & quoted form of destFolder & "; fi; mv " & quoted form of fullOrigName & " " & quoted form of fullDestName
		do shell script cmdStr
		set theRecord's attached script to ""
	end tell
end triggered

You can’t locate an imported file in any location outside the internal structure of the database, therefore you can’t change the path.

Have you indexed a folder in the Finder as well as files outside it? Can you clarify what you’re specifically doing? Thanks!

Note: If you indexed a Finder folder and put imported files into the indexed group in your database, you can right-click the group and choose Move to External Folder and the imported items will be moved to the Finder folder as indexed files.


It is not an imported, it is an indexed file. I have solved the issue by exporting the pdf and re-indexing it from its new location. Not very elegant but it works.

I read and annotate my research articles on a large eInk reader (Sony DPT-RP1). I have all files organized in a folder structure according to main subject in a google drive folder. I sync this folder with the DPT-RP1.
When I find a new article I import it to Bookends. A folder-action script i[/i] automatically imports it to the inbox of a predefined DT library. When the article is selected in DT a triggered script i[/i] forces me to classify it to a group and then creates the corresponding folder on the disk and moves the pdf. If I want to move an article within the DT library I use a script i[/i] from the script menu of DT to do this. SCRIPT_4 is a helper script.

The scripts also set the metadata of the PDF file using exiftool and create URL cross-references between Bookends and DT.

Edit 1: Minor change to SCRIPT_4 for handling single quotes in exiftool arguments


property MyDatabase : "name_of_database_to_index_into"
property FilingScript : "path_to_script_2"

on adding folder items to this_folder after receiving added_items
		if (count of added_items) is greater than 0 then
			tell application id "DNtp" to launch
			local theFileName
			local thePath
			repeat with theItem in added_items
					set thePath to theItem as text
					tell application "System Events" to set theFileName to name of disk item thePath
					-- The next line evaluates if the file is of format "a (d) - t." or "a - t. (d)"  ¬
					-- which I use for books and articles respectively.
					-- This avoids indexing files that are not properly named
					set shellScript to "[[ " & quoted form of theFileName & " =~ (^.*\\([[:digit:]]{4}\\)[[:space:]]-[[:space:]].*)|(^.*[[:space:]]-[[:space:]].*\\([[:digit:]]{4}\\))\\.pdf$ ]]; printf $?"
					set isMatch to "0" = (do shell script shellScript)
					if isMatch then
						tell application id "DNtp"
							set theImportedItem to indicate thePath to incoming group of database MyDatabase
							set theImportedItem's attached script to FilingScript
						end tell
						display notification "Offending file: " & quoted form of theFileName with title "File not imported to DevonThink" subtitle "Wrong format."
					end if
				on error errStr number errorNumber
					error errStr number errorNumber
				end try
			end repeat
		end if
	end try
end adding folder items to


property MoveRecordsScript : "path_to_script_3"

on triggered(ignore_me)
	run script MoveRecordsScript
end triggered


property MyDatabase : "name_of_database_to_index_into"
property IndexedFolder : "the_path_to_the_watched_folder"
property queryBookendsScript : "path_to_script_4_(use an AppleScript path with semicolons"

on run
	tell application id "DNtp"
		set theGroup to display group selector for database MyDatabase
		set destFolder to IndexedFolder & the location of theGroup & the name of theGroup
		set selectedItems to selection
		repeat with a from 1 to length of selectedItems
			-- set the variables
			set theRecord to item a of selectedItems
			set theFileName to name of theRecord
			set destPath to destFolder & "/" & theFileName
			set origPath to theRecord's path
			set refNo to (load script file queryBookendsScript)'s queryBookends(theFileName, origPath)
			-- do the work
			export record theRecord to destFolder
			tell application "System Events"
				delete alias (destFolder & "/DEVONtech_storage")
				delete alias origPath
			end tell
			delete record theRecord
			set theReImportedRecord to indicate destPath to theGroup
			set theReImportedRecord's unread to false
			set refURL to theReImportedRecord's reference URL
			if refNo is not "" then
				set theReImportedRecord's URL to "bookends://" & refNo
				tell application "Bookends"
					«event ToySSFLD» refNo given «class FLDN»:"keywords", string:name of theGroup
					«event ToySSFLD» refNo given «class FLDN»:"url", string:refURL
				end tell
			end if
		end repeat
	end tell
end run


on queryBookends(theFileName, thePath)
	-- * ? + [ ( ) { } ^ $ | \ . must be escaped in Bookends SQL
	-- I do not escape . as it is not needed :)
	-- Add more characters to escape if you come across them in your filenames
	-- Use the "ASCII-only names" option in Bookends to avoid other special characters 
	set fileNameRegExp to replaceText("(", "\\(", theFileName)
	set fileNameRegExp to replaceText(")", "\\)", fileNameRegExp)
	set fileNameRegExp to replaceText("[", "\\[", fileNameRegExp)
	set fileNameRegExp to replaceText("]", "\\]", fileNameRegExp)
	-- I have found no way to escape single quote in Bookends SQL search
	-- I substitute it with . (<any character>)
	set fileNameRegExp to replaceText("'", ".", fileNameRegExp)
	set fileNameRegExp to quoted form of fileNameRegExp
	set RegExp to "attachments REGEX " & fileNameRegExp
	tell application "Bookends" to set RefNo to «event ToySSQLS» RegExp
	if RefNo is not "" then
		tell application "Bookends"
			set pdfTitle to quoted form of («event ToySRFLD» RefNo given string:"title")
			set pdfAuthor to quoted form of («event ToySRFLD» RefNo given string:"authors")
			set pdfDate to quoted form of («event ToySRFLD» RefNo given string:"thedate")
		end tell
		-- exiftool to write PDF metadata, may fail for various reasons
			do shell script "/usr/local/bin/exiftool -overwrite_original -Title=" & pdfTitle & " -Author=" & pdfAuthor & " -Copyright=" & pdfDate & " " & quoted form of thePath
		end try
		display notification "Offending file: " & quoted form of theFileName with title "Not found in Bookends Library"
	end if
	return RefNo
end queryBookends

on replaceText(find, replace, someText)
	set localOldDelims to AppleScript's text item delimiters
	set text item delimiters of AppleScript to find
	set someText to text items of someText
	set text item delimiters of AppleScript to replace
	set someText to "" & someText
	set text item delimiters of AppleScript to localOldDelims
	return someText
end replaceText