A question on getting the database id of think window 1

I have encountered a bottleneck in identifying the database id of think window 1:

I use the following code to get the database settings of think window 1. The problem is code line 1. If one item is selected, the code gets the database id correctly. If none, only group, or more than 1 item is selected, the code line 1 returns an error: “DEVONthink 3 got an error: Can’t get database of think window 1.”

	set theDBID to id of (database of think window 1)
	set theDBRoot to root of think window 1
	set theDBSel to selection as list

Before using the code line 1 of the above block, I tried using another code (see below) for getting the database id. But some strange behavior happened: In the script, I run this code before telling DNtp to set the root of think window 1 to another database. But the id returned is the id of the database that is switching to, not from. I am quite sure that my code sequence is correct.

Set theDBID to id of current database

Thank you in advance.

Please accept my apology in advance (again). If this question is too technical and may confuse some forum members, I will email this sort of questions to technical support in the future.

I think I am asking the wrong question in post #1. Let me ask a different question:

Base case: I have the global inbox as the viewer window, with no item selected. I run the code as follows:

tell application id "DNtp"
	set a to current database		-- returns database id 1
	set b to root of think window 1 -- parent id 2.147483645E+9 of database id 1
	set a1 to uuid of a		-- returns FD1DADC7-F646-46BB-A847-73B7805E52DA
	set b1 to uuid of b		-- returns FD1DADC7-F646-46BB-A847-73B7805E52DA
	set a2 to id of a		-- returns 1
	set b2 to id of b		-- returns 2.147483645E+9
	set a3 to class of a		-- returns database
	set b3 to class of b		-- returns parent
end tell

I thought a and b are reference to the same record (because their UUIDs are the same, see a1 vs b1). Obviously, there is nuance by the way the same record is called. It seems a and b are treated as two different classes (diff object descriptions in a vs b, diff ids in a2 vs a2 and diff classes in a3 vs b3). It seem DT assigns different classes to the same record when different methods are used (“current database” and “root of think window 1”).

So, if I want to set a variable to store the current root of current active database and use the variable to set the root of the “think window 1” later, which method/code should I use to get and set the variable to?

Another block of code. If 0 item or >1 items are selected, c will return error. If only one item is selected, c to c3 will return the exact value as a to a3. Why?

tell application id "DNtp"
	set c to (database of think window 1)	-- returns database id 1
	set c1 to uuid of c						-- returns FD1DADC7-F646-46BB-A847-73B7805E52DA
	set c2 to id of c						-- returns 1
	set c3 to class of c					-- returns database
end tell

Thanks a million again! (And I promise won’t write any script stupid and post it!)

1 Like

What are you specifically trying to do, i.e., why are you trying to set the root of think window 1 to something?

I’m glad you are posting these here! Yes, its more technical than my basic scripting skills allow, but it’s good to have them in the forum for future research.

I’m not sure why this would be complicated. This works.

tell application id "DNtp"
	set c_database to uuid of the current database
	set c_name to the name of the current database
end tell

I included c_name just to ensure that the script is returning the current(ly selected) database. No reason it would not, but just for fun I look at the name. It’s easy to overthink a scripting problem sometimes. In the case of DEVONthink, the dictionary provides the means to solve 99% of most queries.

Thanks for everyone. I “think” I have sorted it out. The main issue is the difference between how “root of think window 1” and “current database” is extracted in the script. If I use the following script to switch the viewer window from DB1 to DB2 by

set the root of think window 1 to {record} -- previous root of DB2 .

in the later code lines, using “get current database” will somehow return the id of DB1 while using “get root of think window 1” again will get the correct database id of DB2.

For my switcher script to save the previous setting of a database, I need to save the UUID (or id) of all database, and update the root of the think window 1, and the selection in of the think window 1 whenever the script switches the database. The data is saved in list of list that is declared as property (persistent in memory? for tracking the previous settings of all opened databases). I keep saving the mis-match in database id (by using get uuid of current database) and the root of database (by using get root of think window 1), thus creating a lol with mis-match settings and databases,

This observation is just by trial and error, I could be giving the wrong explanation.

This is how I get a matching database id with the root of think window 1. The root of database always point to the correct database shown in the main pane.

		set theDB to database of (root of think window 1)
		set theDBID to id of theDB
		set theDBRoot to uuid of (root of viewer window 1)
		if selection ≠ {} then
			set theDBSel to item 1 of (selection as list)
			set theDBSel to {}
		end if

The script seems working now, and I can switch between different database while retaining the previous setting. The catch: only for list-view and widescreen-view. And no search results view.

I will be running the switcher script for a few more days to make sure that it will work consistently. As @bluefrog mentioned before, using root in a script is more tricky coz it’s kind of messing with UI. I hope the script will work…

Can you pls post the script that is working, would like to try it too. Thanks.

Hi, thanks for asking. It’s for my script that is used to switch among databases while retaining the previously selected group and item. SwitcherV1 (Updated) with limited database context retention

Ahh… Understood.