# Trying to compare record to a given database

**URL:** https://discourse.devontechnologies.com/t/trying-to-compare-record-to-a-given-database/24727
**Category:** Automation
**Created:** [November 18, 2018, 9:46pm UTC](https://discourse.devontechnologies.com/t/trying-to-compare-record-to-a-given-database/24727 "2018-11-18T21:46:45Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![bjorn](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/b/74df32/32.png) [@bjorn](https://discourse.devontechnologies.com/u/bjorn)
#### Post date: [November 18, 2018, 9:46pm UTC](https://discourse.devontechnologies.com/t/trying-to-compare-record-to-a-given-database/24727/1 "2018-11-18T21:46:45Z")

</div>

Hi all,

I’d like to fasten the filing of records by looking at tags of other records for file naming and tagging. I want for DTPO to ask me for the appropriate database for filling the record and then compare the record to similar records of the chosen database. 😃

I’m struggling to compare a record in the Global Inbox to a chosen database using:

```auto

set theCompareList to compare record theRecord to theDatabase

```

The problem is that theDatabase seems to be ignored, only records of the database where theRecord is located are found. I say this since I’ve tried to move theRecord to another database and the comparison then uses that database instead.

Using the handler to select theDatabase results in:

```auto

database id 4 of application "DEVONthink Pro"

```

Any help would be appreciated.

---

<div class="post-metadata">

### Author: ![cgrunenberg](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/cgrunenberg/32/7172_2.png) [@cgrunenberg](https://discourse.devontechnologies.com/u/cgrunenberg)
#### Post date: [November 19, 2018, 8:54am UTC](https://discourse.devontechnologies.com/t/trying-to-compare-record-to-a-given-database/24727/2 "2018-11-19T08:54:40Z")

</div>

A future release will support See Also/Classify across databases but right now it’s limited to the record’s database. But content can be compared to any database, therefore a workaround could look like this:

```auto

set theCompareList to compare content (plain text of theRecord) to theDatabase

```

---

<div class="post-metadata">

### Author: ![bjorn](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/b/74df32/32.png) [@bjorn](https://discourse.devontechnologies.com/u/bjorn)
#### Post date: [November 20, 2018, 8:27pm UTC](https://discourse.devontechnologies.com/t/trying-to-compare-record-to-a-given-database/24727/3 "2018-11-20T20:27:52Z")

</div>

Hi,

thank you for the quick response. Sorry to say, the result is the same using

```auto

set theCompareList to compare content (plain text of theRecord) to theDatabase

```

or

```auto

set theCompareList to compare record theRecord to theDatabase

```

I get the same records, which are located in the Global Inbox, using both variants. The version of DTPO is 2.10.2, installed on MacOS High Sierra, 10.13.6.

Regards,  
Björn

---

<div class="post-metadata">

### Author: ![cgrunenberg](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/cgrunenberg/32/7172_2.png) [@cgrunenberg](https://discourse.devontechnologies.com/u/cgrunenberg)
#### Post date: [November 21, 2018, 8:08am UTC](https://discourse.devontechnologies.com/t/trying-to-compare-record-to-a-given-database/24727/4 "2018-11-21T08:08:44Z")

</div>

And theDatabase is definitely not the global inbox? E.g. what is its name/path?

---

<div class="post-metadata">

### Author: ![BLUEFROG](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/bluefrog/32/135_2.png) [@BLUEFROG](https://discourse.devontechnologies.com/u/BLUEFROG)
#### Post date: [November 21, 2018, 1:54pm UTC](https://discourse.devontechnologies.com/t/trying-to-compare-record-to-a-given-database/24727/5 "2018-11-21T13:54:16Z")

</div>

```auto
tell application id "DNtp"
	repeat with res in (compare content "dropbox AND sync AND trouble" to database "Japanese")
		name of res
	end repeat
end tell

```

This is working in 10.14 (though I’m not sure why I’m getting hits about sync in a Japanese database 😃

---

<div class="post-metadata">

### Author: ![bjorn](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/b/74df32/32.png) [@bjorn](https://discourse.devontechnologies.com/u/bjorn)
#### Post date: [November 21, 2018, 8:11pm UTC](https://discourse.devontechnologies.com/t/trying-to-compare-record-to-a-given-database/24727/6 "2018-11-21T20:11:01Z")

</div>

Hi!

Thank you for the interest and quick response, I do appreciate it!

Below, I display the name of the database sent to the handler “compareRecordForTags(theRecord, theDatabase)” so this seems to be correct. I also list the names of the found records, as you can see. I’ve tried with files located in the Global inbox as well as other inboxes of open databases. However, I always end up with the names of records in the database of the record used for comparison.

This is my humble code, sorry if it’s not that good. N.B.: not all handlers are included, but I think it’s not important for this case, please tell me otherwise and I can upload all of it):

```auto

tell application "DEVONthink Pro"
	-- Get selected record for comparison
	set theRecord to item 1 of (get the selection)
	set allDatabases to every database
	
	set theDatabase to selectDatabase(allDatabases) of me
	set theTagsList to compareRecordForTags(theRecord, theDatabase) of me
	
end tell

(* THESE ARE THE USED HANDLERS *)

-- Handler to compare the record to get suggestion for file naming and tags. Save the records to theCompareList
on compareRecordForTags(theRecord, theDatabase)
	tell application "DEVONthink Pro"
		
		display dialog name of theDatabase as string
		-- set theCompareList to compare record theRecord to theDatabase -- unable to compare the record to a given database. "A future release will support See Also/Classify across databases but right now it's limited to the record's database. But content can be compared to any database, therefore a workaround could look like this:"
		set theCompareList to compare content (plain text of theRecord) to theDatabase
		repeat with i from 1 to length of theCompareList
			display dialog name of item i of theCompareList as string
		end repeat
		set theTagsList to {}
		
		-- Collect tags of all compared files
		repeat with i from 1 to length of theCompareList
			set theRecordTags to tags of item i of theCompareList
			repeat with j from 1 to length of theRecordTags
				set the end of theTagsList to item j of theRecordTags
			end repeat
		end repeat
		
	end tell
	
	-- Get list sorted in decreasing order of frequency (of tags)
	set theTagsList to item 1 of sortListByFrequency(theTagsList)
	return theTagsList
end compareRecordForTags

-- Handler for selecting one (1) database from list of databases. Returns choosen database in listOfDatabases
on selectDatabase(listOfDatabases)
	tell application "DEVONthink Pro"
		-- Initiate list of database names
		set theDatabaseNameList to {}
		
		-- Parameter for keeping the index of the Databases
		set theIndex to 0
		
		-- Extract the names of the databases and save to list theDatabaseNameList
		repeat with i from 1 to length of listOfDatabases
			set the end of theDatabaseNameList to name of item i of listOfDatabases as string
		end repeat
		
		-- Make user choose from the list of databases
		choose from list theDatabaseNameList with title "Select the database for comparison" with prompt "Please select the for comparison." --with multiple selections allowed
		set chosenDatabaseName to the result as string
		
		-- Search for index in theDatabaseNameList
		repeat with i from 1 to length of listOfDatabases
			if name of item i of listOfDatabases is chosenDatabaseName then
				set theIndex to i
			end if
		end repeat
		
	end tell
	return item theIndex of listOfDatabases
end selectDatabase

```

---

<div class="post-metadata">

### Author: ![BLUEFROG](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/bluefrog/32/135_2.png) [@BLUEFROG](https://discourse.devontechnologies.com/u/BLUEFROG)
#### Post date: [November 21, 2018, 8:31pm UTC](https://discourse.devontechnologies.com/t/trying-to-compare-record-to-a-given-database/24727/7 "2018-11-21T20:31:58Z")

</div>

The script won’t work without the handler: sortListByFrequency(theTagsList)

---

<div class="post-metadata">

### Author: ![BLUEFROG](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/bluefrog/32/135_2.png) [@BLUEFROG](https://discourse.devontechnologies.com/u/BLUEFROG)
#### Post date: [November 21, 2018, 8:49pm UTC](https://discourse.devontechnologies.com/t/trying-to-compare-record-to-a-given-database/24727/8 "2018-11-21T20:49:38Z")

</div>

Strange. The results seem inconsistent.

I’m glad you’re taking pains to learn and understand AppleScript. Here is my trimmed down version of what you posted…

```auto
tell application id "DNtp"
	log (id of current database as integer) -- Get the ID of the current database
	
	set theRecord to item 1 of (get the selection)
	
	choose from list (name of databases as list) with title "Select the database for comparison"
	set chosenDatabaseName to the result as string
	
	log (id of database chosenDatabaseName as integer) -- Get the ID of the chosen database.
	
	compare content (plain text of theRecord) to database chosenDatabaseName
	
	-- Check the returns to see what DB the results came from.
end tell

```

And returns…

 ![compare.png](https://devontech-discourse.s3.dualstack.us-east-1.amazonaws.com/uploads/original/2X/0/0c4cdd7eff8a0241577d0abf360a9d19c9046011.png)So, the results are from the current database, not the chosen one.

---

<div class="post-metadata">

### Author: ![bjorn](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/b/74df32/32.png) [@bjorn](https://discourse.devontechnologies.com/u/bjorn)
#### Post date: [November 21, 2018, 9:32pm UTC](https://discourse.devontechnologies.com/t/trying-to-compare-record-to-a-given-database/24727/9 "2018-11-21T21:32:40Z")

</div>

> [@BLUEFROG](#):
>
> The script won’t work without the handler: sortListByFrequency(theTagsList)

Yes, I know. That handler, inte turn, calls a handful of other handlers for sorting tags found in order of frequency using bubble sort and stuff like that. What I really aim for is using iOS to set filenames and tags using a nomenclature I use. As of now, I need to be on my Mac in order to do it. If I could use iOS in combination with Siri Shortcuts, iCloud Sync and DTTG, I’d be a happy man. 😃

But, anyhow, I get the same result as you indicate using this code (comparison not made to wanted database). Any idea of how to proceed (except for rationalising the script?) 😊 :

```auto

tell application id "DNtp"
	log (id of current database as integer) -- Get the ID of the current database
	
	set theRecord to item 1 of (get the selection)
	
	choose from list (name of databases as list) with title "Select the database for comparison"
	set chosenDatabaseName to the result as string
	
	log (id of database chosenDatabaseName as integer) -- Get the ID of the chosen database.
	
	set theRecords to compare content (plain text of theRecord) to database chosenDatabaseName
	
	set theNames to {}
	repeat with i from 1 to length of theRecords
		set the end of theNames to name of database of item i of theRecords
	end repeat
	
	get theNames
	
	-- Check the returns to see what DB the results came from.
end tell

```

---

<div class="post-metadata">

### Author: ![BLUEFROG](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/bluefrog/32/135_2.png) [@BLUEFROG](https://discourse.devontechnologies.com/u/BLUEFROG)
#### Post date: [November 22, 2018, 4:45am UTC](https://discourse.devontechnologies.com/t/trying-to-compare-record-to-a-given-database/24727/10 "2018-11-22T04:45:41Z")

</div>

> [@](#):
>
> If I could use iOS in combination with Siri Shortcuts, iCloud Sync and DTTG, I’d be a happy man.

iOS is nowhere near as capable as a Mac for this kind of thing.

---

<div class="post-metadata">

### Author: ![bjorn](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/b/74df32/32.png) [@bjorn](https://discourse.devontechnologies.com/u/bjorn)
#### Post date: [November 22, 2018, 5:12am UTC](https://discourse.devontechnologies.com/t/trying-to-compare-record-to-a-given-database/24727/11 "2018-11-22T05:12:24Z")

</div>

I know iOS isn’t as capable. I havent Figured out the details just yet. But I’m thinking of something in line with this:

My Always on Mac mini would do the ocr for incoming files to an iCloud hot folder and add to thefile to the global inbox. Then use the iOS device or the Mac to select what database to associate the record with. The mini would then do the comparison of the record and save a tag list containing candidates for author, kind of document (receipt, application, article, …), other tags, creation date (will likely be using Hazel before adding the DTPO. Save this data in a formatted way to the records comments. Use Siri Shortcuts to look into the comments and choose stuff from above and rename the record automatically and accordingly.

Something like that. I think it’s definitely doable. Wouldn’t you agree?

Regards, Björn

---

<div class="post-metadata">

### Author: ![cgrunenberg](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/cgrunenberg/32/7172_2.png) [@cgrunenberg](https://discourse.devontechnologies.com/u/cgrunenberg)
#### Post date: [November 22, 2018, 7:54am UTC](https://discourse.devontechnologies.com/t/trying-to-compare-record-to-a-given-database/24727/12 "2018-11-22T07:54:51Z")

</div>

> [@bjorn](#):
>
> My Always on Mac mini would do the ocr for incoming files to an iCloud hot folder and add to thefile to the global inbox. Then use the iOS device or the Mac to select what database to associate the record with. The mini would then do the comparison of the record and save a tag list containing candidates for author, kind of document (receipt, application, article, …), other tags, creation date (will likely be using Hazel before adding the DTPO. Save this data in a formatted way to the records comments. Use Siri Shortcuts to look into the comments and choose stuff from above and rename the record automatically and accordingly. Something like that. I think it’s definitely doable. Wouldn’t you agree?

A future release will make this much easier so these tasks can be accomplished without iOS, Siri or third-party software.

---

<div class="post-metadata">

### Author: ![bjorn](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/b/74df32/32.png) [@bjorn](https://discourse.devontechnologies.com/u/bjorn)
#### Post date: [November 22, 2018, 6:12pm UTC](https://discourse.devontechnologies.com/t/trying-to-compare-record-to-a-given-database/24727/13 "2018-11-22T18:12:53Z")

</div>

Ok, i think, in the meantime I’ll have to chose the database for the comparison, move the record and do the comparison.

Will try to play a little. If I get the time and a result, or run into trouble, I’ll check back in to the forums.

Thanks for the quick response time and support.

All the best,  
Björn

---

<div class="post-metadata">

### Author: ![pete31](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/p/d26b3c/32.png) [@pete31](https://discourse.devontechnologies.com/u/pete31)
#### Post date: [July 15, 2020, 4:15am UTC](https://discourse.devontechnologies.com/t/trying-to-compare-record-to-a-given-database/24727/14 "2020-07-15T04:15:59Z")

</div>

> [@BLUEFROG](#):
>
> the results are from the current database, not the chosen one.

I tried to compare text to a database but only get results from the current database. Is comparing to a given database possible?

Edit: This workaround does what I’m looking for

```
tell application id "DNtp"
	try
		-- choose from list ...
		--> Database_Name
		
		open window for record root of database Database_Name
		activate
		
		set theResults to compare content theText
		
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
	end try
end tell

```

---

<div class="post-metadata">

### Author: ![bjorn](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/b/74df32/32.png) [@bjorn](https://discourse.devontechnologies.com/u/bjorn)
#### Post date: [July 27, 2020, 9:00pm UTC](https://discourse.devontechnologies.com/t/trying-to-compare-record-to-a-given-database/24727/15 "2020-07-27T21:00:50Z")

</div>

This, I have to try. Thanks for sharing 😀

---

<div class="post-metadata">

### Author: ![cgrunenberg](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/cgrunenberg/32/7172_2.png) [@cgrunenberg](https://discourse.devontechnologies.com/u/cgrunenberg)
#### Post date: [July 28, 2020, 7:21am UTC](https://discourse.devontechnologies.com/t/trying-to-compare-record-to-a-given-database/24727/16 "2020-07-28T07:21:13Z")

</div>

> [@pete31](#):
>
> I tried to compare text to a database but only get results from the current database. Is comparing to a given database possible?

By default the current database is used, the `to` parameter can be used to specify the desired one.

---

<div class="post-metadata">

### Author: ![pete31](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/p/d26b3c/32.png) [@pete31](https://discourse.devontechnologies.com/u/pete31)
#### Post date: [July 28, 2020, 1:09pm UTC](https://discourse.devontechnologies.com/t/trying-to-compare-record-to-a-given-database/24727/17 "2020-07-28T13:09:07Z")

</div>

> [@cgrunenberg](#):
>
> the `to` parameter can be used to specify the desired one.

This doesn’t work over here. Without my workaround (first setting the current database by opening a new root window) the current database is used. Perhaps I’m missing something? It seems @bjorn and @BLUEFROG got this result too, see this old post

> [@BLUEFROG](#):
>
> And returns…
> 
> ![compare.png](https://devontech-discourse.s3.dualstack.us-east-1.amazonaws.com/uploads/original/2X/0/0c4cdd7eff8a0241577d0abf360a9d19c9046011.png)
> 
> So, the results are from the current database, not the chosen one.

---

<div class="post-metadata">

### Author: ![cgrunenberg](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/cgrunenberg/32/7172_2.png) [@cgrunenberg](https://discourse.devontechnologies.com/u/cgrunenberg)
#### Post date: [July 28, 2020, 4:59pm UTC](https://discourse.devontechnologies.com/t/trying-to-compare-record-to-a-given-database/24727/18 "2020-07-28T16:59:11Z")

</div>

> [@pete31](#):
>
> Without my workaround (first setting the current database by opening a new root window) the current database is used.

How did the script trying to use the `to` parameter look like?

---

<div class="post-metadata">

### Author: ![BLUEFROG](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/bluefrog/32/135_2.png) [@BLUEFROG](https://discourse.devontechnologies.com/u/BLUEFROG)
#### Post date: [July 28, 2020, 5:20pm UTC](https://discourse.devontechnologies.com/t/trying-to-compare-record-to-a-given-database/24727/19 "2020-07-28T17:20:52Z")

</div>

Here is the script I initially posted…

```auto
tell application id "DNtp"
	log (id of current database as integer) -- Get the ID of the current database
	
	set theRecord to item 1 of (get the selection)
	
	choose from list (name of databases as list) with title "Select the database for comparison"
	set chosenDatabaseName to the result as string
	
	log (id of database chosenDatabaseName as integer) -- Get the ID of the chosen database.
	
	compare content (plain text of theRecord) to database chosenDatabaseName
	
	-- Check the returns to see what DB the results came from.
end tell

```

And the results…

 ![image](https://devontech-discourse.s3.dualstack.us-east-1.amazonaws.com/uploads/original/3X/a/9/a925f419e709f6a95e3dc66d14d078876bafeb53.jpeg)

---

<div class="post-metadata">

### Author: ![pete31](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/p/d26b3c/32.png) [@pete31](https://discourse.devontechnologies.com/u/pete31)
#### Post date: [July 28, 2020, 5:24pm UTC](https://discourse.devontechnologies.com/t/trying-to-compare-record-to-a-given-database/24727/20 "2020-07-28T17:24:35Z")

</div>

```
tell application id "DNtp"
	try
		set theDatabase to database "_temp"
		set theResults to compare content "test" to theDatabase
		
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
	end try
end tell
```

[Next page](https://discourse.devontechnologies.com/t/trying-to-compare-record-to-a-given-database/24727.md?page=2)
