Applescript help needed "Contents of Current Database"

Hi all (hi hi Jim!)

I am a bit confounded here :slight_smile:

I have an rtf file with a list of records, like so:

1978 Mentally disabled people 1v_Green_IMG_4011_exported_20220701
1978 Industry and Technic_Green_IMG_0501_exported_20211218
1978 Landscapes_Green_IMG_9351_exported
1978 Men's world Handball Championships_Green_IMG_3034_exported_20220125
1978 Sports Editions_Green_IMG_9578_exported_20210621
1978 The 75th Anniversary of the Deutsche Museum in Munich_Green_IMG_9710_exported_20210707
1978 Young Philatelists_Green_IMG_0010_exported_20211208
1978 Linnes resort 6v_Green_IMG_1977_exported_20220106
1978 Perrault Fables_Green_IMG_4547_exported_20220706
1978 R.A. Schroder 1v_Green_IMG_3847_exported_20220620

And I wanted to be able to find the corresponding records in database “FOO”

I did create an AS starter that reads (doesn’t iterate yet) the paragraphs of the rtf and stores them in a variable. Then I tell DT to return the contents of database “FOO” which match that variable.

Here’s is the (work in progress totally) script:

tell application “DEVONthink”
set searchPhrase to paragraph 1 of rich text of item 1 of (get selection)
return contents of current database whose filename is searchPhrase
end tell

this doesn’t return anything.

However, if I manually copy the first line of the rtf file and set searchPhrase to it, it does return the correct matching record. Also, I know that the line “set searchPhrase” also works correctly, and captures that rtf first line.

I pasted these values in BBEdit with invisibles toggled on, to see if there were any different hidden characters, but no, they are exactly the same.

At this point, I am lost… Any ideas?

The filename property includes the extension, your list doesn’t. That’s most likely the culprit.

Thanks Christian - I added “.webp” to line 1 of the rtf, still doesn’t return anything.

#1: “searchPhrase” gets populated OK

#2 return contents doesn’t find anything

#3 Copy value in #1 and modify AS line to return contents of record whose name is “1978…” works.

  1. You don’t need the rich text for this. Use plain text.
  2. Check for and use content record.
  3. Use the record property name without extension.
  4. You could also use a search instead of contents of current database.

You haven’t said what you intend to do with what you find.

Here is a simple proof-of-concept…

tell application id "DNtp"
	if not (exists (content record)) then return
	
	set src to plain text of content record
	if (src is not "") then -- Not necessary in this case but not a bad habit to have.
	repeat with theLine in (paragraphs of src)
		set theLine to (theLine as string)
		if theLine is not "" then
			set matchedDoc to (contents of (current database) whose (name without extension is theLine))
			if matchedDoc is not {} then
				beep 2 -- Or do whatever
			end if
		end if
	end repeat
    end if
end tell
1 Like

Thanks Jim, works beautifully!

Yesterday I had concocted a script that also works - but is not as elegant :slight_smile:

tell application "DEVONthink"
	set listItems to number of paragraphs of rich text of item 1 of (get selection)
	repeat with i from 1 to listItems
		set searchPhrase to paragraph i of rich text of item 1 of (get selection)
		set theResult to search searchPhrase in root of database "STAMPS"
		repeat with imageItem in theResult -- find the webp image
			set matchKind to kind of imageItem as rich text
			if matchKind is "WebP Image" then set matchReferenceURL to reference URL of imageItem
			matchReferenceURL
		end repeat
		set URL of paragraph i of rich text of item 1 of (get selection) to matchReferenceURL
		set newURL to URL of paragraph i of rich text of item 1 of (get selection)
	end repeat
end tell

You asked why I wanted it:

My stamps blog (which is still under gestation) contains thousands of stamp images - all with an assortment of tags. That’s how I am building my blog stories. A year ago or so you helped me with building smart groups for story / country / topic etc categories, and all is working beautifully - couldn’t be done without DEVONthink.

A month or so back, I realized I should convert all the png and jpg to a more web friendly format, and our dear @troejgaard built an in-situ converter using a brew package, so all images are now webp.

As it happens, I have a Numbers spread that is a master file, with all stories, links to DT stamp images, and columns for category, comments, post type, etc. It was easier to do it in a spreadsheet format (though by training I would have loved to use a relational database).

So after having hundreds of stories = spreadsheet rows I realized that all those links pointed to DT id’s that didn’t exist anymore - each new webp image was a record with a new id. Duh…

So being very lazy, I had to programatically select each line in the spreadsheet, find the new webp, and copy / paste its link. Well, Numbers is horrible for Applescript, really. So I had to go through hoops (won’t explain here for being too tedious) to export the Number contents to Excel, save a TSV, paste in BBEdit, use regex to extract some parts via \1 groups, and so on, until I had a clean rtf with just a list of stamp image filenames.

And then I needed this script.

But all worked, I rebuilt the Numbers spread, and am very happy :slight_smile:

And a big thanks to you, @troejgaard and @cgrunenberg !!

3 Likes