Script: Change the search scope of a search window

This script changes the search scope of a search window (on database level). It will open the database if necessary.

The variable theDatabaseFolder is the path to the folder in which you store your DEVONthink databases. To set the search scope back to all databases set the variable theDatabaseName to “Datenbanken” (should be “Databases” in english). I use it in a Keyboard Maestro Palette, but it should work with a shortcut assigned to DEVONthinks script menu too.

Hope it’s useful 8)

property theDatabaseName : "Test"
property theDatabaseFolder : "/DEVONthink Datenbanken/"

activate application id "DNtp"
tell application "System Events"
	tell process "DEVONthink Pro Office"
		try
			click pop up button 1 of window 1
			select (first menu item of menu 1 of pop up button 1 of window 1 whose name is theDatabaseName)
			key code 36
		on error number error_number
			if the error_number is -1719 then
				key code 36
				tell application id "DNtp" to open database theDatabaseFolder & theDatabaseName & ".dtBase2"
				click pop up button 1 of window 1
				select (first menu item of menu 1 of pop up button 1 of window 1 whose name is theDatabaseName)
				key code 36
			end if
		end try
	end tell
end tell

Are you intending to target a database that isn’t active?

Sure, if i understand you right. I first used “display dialog” to inform me that the database i’ve chosen isn’t open already. But it quickly became annoying to open a not already opened database manually, so I changed the script to open the database for me.

Perhaps I wasn’t clear enough. In a Keyboard Maestro palette I have a macro for every DEVONthink database (= a version of the script, only difference is the database name). I use the global search window all the time, but often i know in which database i want to search. Then I us one of these macros to limit the search scope to one database.

Updated for DEVONthink 3

If the keyboard focus is not on the result list although it should please let me know.

(Fixed unwanted adding of spaces and added option to set scope to all inboxes)

-- Change search scope of a DEVONthink 3 search
-- This script opens the database if necessary. It replaces a search scope or adds one. To do so it removes the last part of the query (DEVONthink 3 help: "When used, scope: should be the last parameter in the query.")
-- set theDatabaseName to "inboxes" to search open inboxes
-- Use it e.g. in a Keyboard Maestro palette, one macro per database.
-- Optionally set the keyboard focus to the result list (while running focus needs to be temporarily in the search field but you'll probably want to go on in the results)
-- You can use this as a template for other changes you'd like to apply to your query, e.g. adding or removing astericks.

property theDatabaseName : "" -- "inboxes" for all open inboxes
property theDatabaseFolder : "/DEVONthink Datenbanken/" -- path to finder folder
property focusOnResultList : true

tell application id "DNtp"
	set theDatabaseNames to name of databases
	if theDatabaseName is not in theDatabaseNames then
		open database theDatabaseFolder & theDatabaseName & ".dtBase2"
	end if
end tell

activate application "DEVONthink 3"
tell application "System Events"
	tell process "DEVONthink 3"
		set theQuery to value of text field 1 of group 3 of toolbar 1 of window 1
	end tell
end tell

if theQuery = "" then return

if theQuery contains "scope:" then set theQuery to characters 1 thru ((character offset of "scope:" in theQuery) - 2) in theQuery as string

if theDatabaseName is not "inboxes" then
	set theQueryWithNewScope to theQuery & space & "scope:\"" & theDatabaseName & "\""
else
	set theQueryWithNewScope to theQuery & space & "scope:" & theDatabaseName
end if

activate application "DEVONthink 3"
tell application "System Events"
	tell process "DEVONthink 3"
		set focused of text field 1 of group 3 of toolbar 1 of window 1 to true
		set value of text field 1 of group 3 of toolbar 1 of window 1 to theQueryWithNewScope
		
		if focusOnResultList = true then
			try
				set focused of table 1 of scroll area 1 of group 1 of splitter group 2 of splitter group 1 of window 1 to true
			on error
				try
					set focused of table 1 of scroll area 1 of group 1 of splitter group 1 of splitter group 1 of window 1 to true
				on error
					set focused of scroll area 1 of splitter group 1 of splitter group 1 of window 1 to true
				end try
			end try
		end if
		
	end tell
end tell

This version takes into account that not everbody uses the default DEVONthink 3 toolbar

	-- Change search scope of a DEVONthink 3 search window
	-- Use it e.g. in a Keyboard Maestro palette, one macro per database.
	-- Optionally set the keyboard focus to the result list (while running focus needs to be temporarily in the search field but you'll probably want to go on in the results).
	-- You can use this as a template for other changes you'd like to apply to your query, e.g. adding or removing astericks.

	property theDatabaseName : "" -- "inboxes" for all open inboxes
	property theDatabaseFolder : "/DEVONthink Datenbanken/" -- path to finder folder
	property focusOnResultList : true

	tell application id "DNtp"
		set theDatabaseNames to name of databases
		if theDatabaseName is not in theDatabaseNames then
			open database theDatabaseFolder & theDatabaseName & ".dtBase2"
		end if
	end tell

	activate application id "DNtp"
	tell application "System Events"
		tell process "DEVONthink 3"
			repeat with i from 1 to 10
				try
					set theSearchField to text field 1 of group i of toolbar 1 of window 1
					set theQuery to value of theSearchField
					exit repeat
				end try
			end repeat
		end tell
	end tell

	if theQuery = "" then return

	if theQuery contains "scope:" then set theQuery to characters 1 thru ((character offset of "scope:" in theQuery) - 2) in theQuery as string

	if theDatabaseName is not "inboxes" then
		set theQueryWithNewScope to theQuery & space & "scope:\"" & theDatabaseName & "\""
	else
		set theQueryWithNewScope to theQuery & space & "scope:" & theDatabaseName
	end if

	activate application id "DNtp"
	tell application "System Events"
		tell process "DEVONthink 3"
			set focused of theSearchField to true
			set value of theSearchField to theQueryWithNewScope
			key code 36
			
			if focusOnResultList = true then
				try
					set focused of table 1 of scroll area 1 of group 1 of splitter group 2 of splitter group 1 of window 1 to true
				on error
					try
						set focused of table 1 of scroll area 1 of group 1 of splitter group 1 of splitter group 1 of window 1 to true
					on error
						set focused of scroll area 1 of splitter group 1 of splitter group 1 of window 1 to true
					end try
				end try
			end if
			
		end tell
	end tell

Thanks for posting these scripts! But it’s recommended to use tell application id “DNtp” to make scripts compatible to all editions/versions, e.g. future ones.

Yes I know, but UI Browser doesn’t :confused: I just asked the developer to consider a preference for the id (and changed my TextSoap Cleaner…)