Amending the "Download bibliographic metadata" script - help

I’m finally getting around to using the very helpful built-in smart rule for downloading bibliographic metadata. However, I am not a coding person and I’d like to amend the script without breaking anything.

My academic papers are named:
Multi author = [First author last name] et al [Year] [Paper title]
Two authors = [First author last name] & [Second author last name] [Year] [Paper title]
One author = [First author last name] [Year] [Paper title]

The default script currently renames the file “[Paper title]”.

My ideal scenario is that I can set the script to follow my naming convention exactly, but I don’t know how feasible this is. My second option, and perhaps the more realistic, is that I stop the default script from renaming the file and leave it as is. Currently (and ever since I started using DT) I’ve been naming these files manually. I wasn’t looking to automate that when I started this bit of experimenting this morning and I’m fine if I can just get it to stop renaming my files.

Is anyone able to explain what bits of the script I need to amend, and how, for either scenario? I looked at the script but I was wary of changing anything when I only have a very rudimentary understanding of the instructions being parsed. I think it’s something to do with this line of the script?
set theRecord's name to (|title| of json_record) as string.
I read this as “Update the file’s name to the title provided”, is that right?

Yes, that would be the line to remove or comment out.

You correctly identifed the line renaming the file, so we’re off to a great start :smiley:

I think the script already has all the parts you need.
(First, it’s a good idea to duplicate the script and edit a copy.)

This is the relevant section:

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

(* >> ADD STUFF HERE << *)

The script downloads metadata from DOI and puts it in the variable json_record.

  • ‌|title| of json_record is the paper/reference title.
  • The citation variable holds author last names – already formatted to your specification.
  • The pubyear variable is the year.

You probably want some delimiter between author/year/title? It’s not clear to me from your notation. Here are two examples:

-- Example A
set nameStr to citation & " (" & pubyear & ") " & |title| of json_record
--> lastName1 & lastName2 (2026) Paper title

-- Example B
set nameStr to citation & " " & pubyear & ". " & |title| of json_record
--> lastName1 & lastName2 2026. Paper title

-- Then, to rename the file:
set theRecord's name to nameStr

It took me some time to figure out which script you were referring to (since I do not use bibliographic data at all). So, the two relevant lines are these:

set the json_record to download JSON from doi
set theRecord's name to (|title| of json_record) as string

The first one gets the data for a DOI from a DOI server in JSON format. That is basically a structure assigning values to keys. One of these keys is title, which is used in the next line to set the record’s name.

Now, you want to add the author(s) names to this name. For this, you need to look at the JSON returned by a DOI server as shown here.
The sample line there looks like that:

"author" : [ { "family" : "Frank", "given" : "H. S."} ],

Here, author is the key and [ { "family" : "Frank", "given" : "H. S."} ] is the value. That is an array (a list in AppleScript parlance), denoted by the opening and closing brackets []. In this case, it contains a single object (a record in AppleScript parlance), denoted by the opening and closing curly braces {}. I think we can safely assume that in the case of several authors, this will look something like

"author" : [ { "family" : "Frank", "given" : "H. S."}, 
  { "family": "Curie", "given": "Marie"} ],

So, in a tentative AppleScript, I’d try to do something like this:

set authorList to (author of json_record) as list
set firstAuthorRecord to (first item of authorList) as record
set author to (family of firstAuthorRecord) as string
if ((count of authorList) = 2) then
	set secondAuthorRecord to (item 2 of authorList) as record
	set author to author & " " & (family of secondAuthorRecord)
else if ((count of authorList) > 2) then
	set author to author & " et al"
end if

-- author should now contain what you specified before

set issued to (issued of json_record) as record
try
	set dateParts to (|date-parts| of issued) as list
	set pubDate to (first item of (first item of dateParts)) as string
on error number -1728
	try
		set pubDate to characters 1 thru 4 of ((|raw-date| of issued) as string)
	on error
		set pubDate to ""
	end try
end try

set the record's name to author & " " & pubDate & (title of json_record as string)

I’m a bit pressed for time, so please forgive me that his code is not tested at all. It seems to be syntactically correct (as in ScriptEditor compiles it without errors), but if it does what it should do – I don’t know.
Also, I can’t explain the pubDate stuff right now. Checkout CSL-JSON — citeproc-js 1.1.73 documentation for the gory details – suffice it to say that this way to use JSON is a bit murky, imo.

And now I see that @troejgaard beat me. And they are apparently more knowledgable as I’m in this context. So, go with that.

I can’t tell if you’ve seen too many of my forum posts and know I’m about to edit things without making copies. When I was tinkering this morning I absolutely was in the original file even though :laughing:

Thank you for the answer and your detailed explanation. I was able to follow the logic step-by-step and I’ve implemented it (in a copied script!) and it’s worked first time! I really appreciate you taking the time and now I can rename my files by magic!

Thank you for sharing your code and explanation too. It’s very similar to @troejgaard’s and I followed it all to see where you differed slightly and why that is. I don’t think I will be a scripting wizard any time soon but I understand how this script works now at least! Thank you for explaining how it works!