Script: Add query that was initiated via AppleScript to DEVONthink history

Hey there, I seldom use DEVONthink’s search via the toolbar as I use an Alfred workflow that opens search results in a new window.

However sometimes I’d like to redo a search via “Recent searches” in the toolbar drop down …

Recent Searches

… but currently that’s not possible because “Recent searches” doesn’t list searches that were initiated via AppleScript.

So what do you think? Would the possibility to redo AppleScript searches via “Recent searches” be handy?

1 Like

Calling this handler after you’ve set a viewer window's query via AppleScript adds it to DEVONthink’s history.

Calling the handler:

my writeQueryIntoDEVONthinkHistory()

Handler:

on writeQueryIntoDEVONthinkHistory()
	try
		activate application id "DNtp"
		tell application "System Events"
			tell process "DEVONthink 3"
				set theTextFields to text fields of groups of toolbar 1 of window 1
				repeat with theseTextFields in theTextFields
					set theseTextFields to contents of theseTextFields
					if theseTextFields ≠ {} then
						set thisTextField to item 1 of theseTextFields
						set focused of thisTextField to true
						key code 36
					end if
				end repeat
			end tell
		end tell
	on error error_message number error_number
		activate
		if the error_number is not -128 then display alert "Error: Handler \"writeQueryIntoDEVONthinkHistory\"" message error_message as warning
		error number -128
	end try
end writeQueryIntoDEVONthinkHistory

1 Like

Hi @pete31 - Thanks for sharing your script! By chance, do you know where the search history is actually stored in DEVONthink (i.e., whatever is filling the “Recent Searches” field)?

The reason I ask is that I’d like to create an Alfred workflow that gets my search history and allows me to quickly filter through it and rerun past searches (e.g., similar to those for Safari, Chrome, etc.). While the latter part is easy enough to build, I have no idea where to get the search history from.

It’s got to be sitting in a plist file or something similar, right?

Thanks for your help!

You can get it via vanilla AppleScript or AppleScriptObjC.

AppleScript version

AppleScript version
-- Get ToolbarSearchRecents from plist (AppleScript version)

tell application "System Events"
	tell property list file "~/Library/Preferences/com.devon-technologies.think3.plist"
		set theToolbarSearchRecents to value of property list item "ToolbarSearchRecents"
	end tell
end tell

However, you probably notice that it’s a bit slow.

AppleScriptObjC version

A script library with AppleScriptObjC requires a little setup but it’s a lot faster.

AppleScriptObjC version
-- Get ToolbarSearchRecents from plist (AppleScriptObjC version)
-- Save this script in ~/Library/Script Libraries

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

on getToolbarSearchRecents()
	try
		if current application's id = "com.devon-technologies.think3" then
			set theDefaults to current application's NSUserDefaults's standardUserDefaults()
		else
			set theDefaults to current application's NSUserDefaults's alloc()'s initWithSuiteName:"com.devon-technologies.think3"
		end if
		
		set theToolbarSearchRecents to ((theDefaults's dictionaryRepresentation())'s objectForKey:"ToolbarSearchRecents") as list
		
	on error error_message number error_number
		activate
		if the error_number is not -128 then display alert "Error: Handler \"getToolbarSearchRecents\"" message error_message as warning
		error number -128
	end try
end getToolbarSearchRecents

To create a script library:

  • In Finder:

    • press ⇧⌘G
    • paste ~/Library/Script Libraries
    • if no window is opened
      • paste ~/Library/
      • create folder Script Libraries
  • In Script Editor.app

    • create new document
    • paste this script
    • press ⌘S
    • press ⇧⌘G
    • paste ~/Script Libraries/
    • save

To call the script library in your script (replace script "Get ToolbarSearchRecents from plist" with your script library’s name):

set theToolbarSearchRecents to script "Get ToolbarSearchRecents from plist"'s getToolbarSearchRecents()
1 Like

@pete31 I’m not sure what to say, except: You’re the best, truly!

I can’t thank you enough for the thoughtful explanation. As always, it works like a charm. You rock!

1 Like

Direct usage (or even modification) of DEVONthink’s internal defaults is actually highly discouraged. Any update might break this.

1 Like

Not sure what that means in the context of these scripts. What is “direct usage”?

This:

set theDefaults to current application's NSUserDefaults's alloc()'s initWithSuiteName:"com.devon-technologies.think3"

Where‘s the difference to getting a plist via System Events? I‘m only getting values, not setting anything in the plist.

This script might break anytime as it depends on internals which might change in future updates of DEVONthink or macOS.

1 Like