DOI smart rule to add metadata given json_record issue

I am using the smart rule to add metadata to academic articles with the download bibliographic metadata copy smart rule. This had been working well for the past week, but today I am getting error

“The variable json_record is not defined.”

Appreciate any help as to what is going on. Nothing was changed in my setup, and I am not comfortable with writing script so just using the built in scripts.

It’s a bit difficult to comment on an error in a rule or script without seeing said rule or script. And searching in the forum for DOI smart rule returns too many hits …

It’s a default smart rule script, see ~/Library/Application Scripts/com.devon-technologies.think3/Smart Rules

Anyway, a copy of the used document would be useful.

1 Like

So, the problem might arise with this line

set the json_record to download JSON from doi

Probably http://dx.doi.org/ does not return a value for the DOI, so that json_record is not set (and the script doesn’t check for that). Yes, a copy of the document in question would definitely help.

I’m using the built in script.

I can provide copies of documents if you like, however this script worked fine up until yesterday (worked when I transitioned my entire Zotero database over). When it stopped yesterday I went back to previous source files and tried and I get the error now on every file, even the source pdf ones that were already successfully identified so I’m not sure the issue.

Edit: I’m not sure I can actually upload a pdf of articles since they are behind paywall, but I have included an article I just tried that appears free to download that I also get the same error.

I just tried the script here with this PDF and got a lovely json_record (in Script Editor, that is). What happens when you direct your browser to

https://dx.doi.org/10.1056/NEJMoa2204919

I have the suspicion that something with your network connection to dx.doi.org is not as well as it should be.

Just did a test on my iMac (I have only used DT from MacBook but downloaded and thought maybe try from iMAc) and the script runs no issues. Not a network issue.

Also that link works perfectly on both.

So at least we know that it’s not the script that is at fault here.

Do you see anything in DT’s log window on the iMac? Has anything on the iMac changed after yesterday, i.e. from when the script worked and when it stopped working? Oh, and are you perhaps running MacOS Ventura?

Not running the beta. Same os on both (up to date current macOS). Same network. Devon think was installed on the working iMac 3 days ago as compared to a few weeks ago on laptop.

The only thing the log says is the json_record error on every file.

If no other ideas I’m considering deleting and reinstalling the app tonight after work. Not sure what else to do

I’ll append a very slightly modified version of the script. Open it in Script Editor (with the button underneath the code listing), turn on its protocol (Cmd-3), select one of the offending records in DT and then run the script in Script Editor.

Check the “Events” and “Answers” tabs in the Script Editor’s protocol pane at the bottom.

-- Download Bibliographic Metadata
-- (c) Kai Müller May 05, 2019
-- Renamed, simplified, enhanced and updated for smart rules by Christian Grunenberg

-- 	1. Retrieves the digital object identifier (DOI) of the document 
--	2. Downloads bibliographic metadata, renames document to title and sets DOI, ISSN, Link, Date, Journal, Volume, Issue, Publisher, Type, Authors and Citation
--	NOTE: Every downloaded PDF publication (e.g. from Google Scholar or any Journal) might work.

--on performSmartRule(theRecords)
tell application id "DNtp"
	set theRecords to selected records
	show progress indicator "Downloading Bibliographic Metadata" steps (count of theRecords) without cancel button
	try
		repeat with theRecord in theRecords
			set theName to name of theRecord
			step progress indicator theName
			
			try
				set customMetaData to custom meta data of theRecord
				set doi to mddoi of customMetaData
				if doi begins with "http://dx.doi.org/" then
					set doi to (characters 19 thru -1 of doi) as string
				else if doi begins with "https://dx.doi.org/" then
					set doi to (characters 20 thru -1 of doi) as string
				end if
			on error
				set doi to ""
				try
					set doi to digital object identifier of theRecord
					if doi is missing value then error
				on error
					set doi to ""
				end try
			end try
			
			if doi is not "" then
				try
					add custom meta data doi for "doi" to theRecord
					
					set the doi to "http://dx.doi.org/" & doi
					set the json_record to download JSON from doi
					
					set theRecord's name to (title of json_record) as string
					
					try
						set the json_created to created of json_record
						add custom meta data |date-time| of json_created for "date" to theRecord
					end try
					
					try
						if exists (|published-print| of json_record) then
							set pubyear to item 1 of item 1 of |date-parts| of |published-print| of json_record
						else
							set pubyear to item 1 of item 1 of |date-parts| of |published-online| of json_record
						end if
						
						set the authors to author of json_record
						set the citation to ""
						set cnt to count of authors
						
						if cnt < 3 then
							repeat with i from 1 to cnt
								set author to item i of authors
								if i > 1 then
									set citation to citation & " & " & family of author
								else
									set citation to citation & family of author
								end if
							end repeat
						else if cnt > 0 then
							set author to item 1 of authors
							set citation to family of author & " et al."
						end if
						
						add custom meta data (citation & " " & pubyear) for "citation" to theRecord
						
						set the authorsList to ""
						repeat with author in authors
							set authorsList to authorsList & |given| of author
							set authorsList to authorsList & " " & family of author & return -- return ensures that DEVONthink automatically defines this field as text and not as a string
						end repeat
						
						add custom meta data authorsList for "authors" to theRecord
					end try
					
					try
						set the articletype to |type| of json_record
						add custom meta data articletype for "type" to theRecord
					end try
					
					try
						set the link to |URL| of json_record
						add custom meta data link for "link" to theRecord
					end try
					
					try
						set the publisher to publisher of json_record
						add custom meta data publisher for "publisher" to theRecord
					end try
					
					try
						set the issn to item 1 of issn of json_record
						add custom meta data issn for "is?n" to theRecord
					end try
					
					try
						set the journal to |container-title| of json_record
						add custom meta data journal for "journal" to theRecord
					end try
					
					try
						set the volume to volume of json_record
						add custom meta data volume for "volume" to theRecord
					end try
					
					try
						set the issue to issue of json_record
						add custom meta data issue for "issue" to theRecord
					end try
					
					try
						set the page to page of json_record
						add custom meta data page for "page" to theRecord
					end try
				on error error_message number error_number
					log message record theRecord info error_message
				end try
			end if
		end repeat
	end try
	hide progress indicator
end tell
--end performSmartRule

This is working as expected here.

:wink: here too.