Recently Devonthink has rewritten the Bookends import script, and it’s really caused me problems. It used to import the name of the reference as author - title. Now it just imports the title, and I’ve had to make a workaround to modify titles to author - title with a script.
Anyway, it seems also have to broken my script that synced with the publication date. I have a Pub.Date custom metadata field that I was able to pull the Bookends publication date for, but it isn’t working anymore. The automatic import that Devonthink does is not good, because it includes month and day. But the month and day (which don’t show up in Bookends itself) Devonthink puts in the months and day that I made the record in Bookends. So for a publication in 2005, for example, I will get 2005-03-06, if it’s a record I added today. I don’t want the superfluous and erroneous month/day included when I sort records by date.
So can anyone either tell me how to make Devonthink import only the year into the Date metadata field? Or how to modify my script so it works again?
on performSmartRule(theRecords)
**tell** *application* *id* "DNtp"
**repeat** **with** theRecord **in** (*selected records*)
**set** recordURL **to** (URL **of** theRecord)
**if** recordURL **begins with** "bookends://" **then**
**set** publicationDate **to** **my** bookendslookup(*word* -1 **of** recordURL)
**if** (publicationDate **is** **not** "") **then**
**add custom meta data** publicationDate for "Pub.Date" to theRecord
**set** comment **of** theRecord **to** (comment **of** theRecord & "Custom date added: " & (short date string **of** (**current date**)) & linefeed)
**else**
**log message** "Publication date not found or Bookends library is not open"
**return**
**end** **if**
**end** **if**
**end** **repeat**
**end** **tell**
end performSmartRule
on bookendslookup(beURL)
**tell** *application* *id* "com.sonnysoftware.bookends2"
**if** (**exists** (*library window* 1)) **then**
**tell** *library window* 1
**set** matchedPub **to** (**first** *publication item* **whose** id = (*word* -1 **of** beURL))
**return** (publication date string **of** matchedPub)
**end** **tell**
**else**
**return** ""
**end** **if**
**end** **tell**
end bookendslookup
ALSO, I just noticed, the new import script also pulls the colour labels from Bookends and puts the same colour labels in the Devonthink record. But I use colour labels for different purposes in the two problems so I don’t want the labels to be duplicated in Devonthink. Is there a way to turn that off?
Please post code included in three backticks live so
```
code goes here
```
1 Like
It took me a while to figure out what you meant about backticks.
on performSmartRule(theRecords)
tell application id "DNtp"
repeat with theRecord in (selected records)
set recordURL to (URL of theRecord)
if recordURL begins with "bookends://" then
set publicationDate to my bookendslookup(word -1 of recordURL)
if (publicationDate is not "") then
add custom meta data publicationDate for "Pub.Date" to theRecord
set comment of theRecord to (comment of theRecord & "Custom date added: " & (short date string of (current date)) & linefeed)
else
log message "Publication date not found or Bookends library is not open"
return
end if
end if
end repeat
end tell
end performSmartRule
on bookendslookup(beURL)
tell application id "com.sonnysoftware.bookends2"
if (exists (library window 1)) then
tell library window 1
set matchedPub to (first publication item whose id = (word -1 of beURL))
return (publication date string of matchedPub)
end tell
else
return ""
end if
end tell
end bookendslookup

There’s always copy/paste. And you can edit your post instead of reposting.
To the problem at hand: is it intentionally that you
- use
word -1 once in the call of the bookends handler and once again in the handler itself?
- return an empty string if no match can be found in the bookends handler?
- never check if the date returned by that handler is the empty string?
Did you try running the script in the script editor, following the AppleEvents there? That would require a slight change: adding a run handler that calls performSmartRule with the records currently selected in DT. But than you could see what’s actually going on.
Thanks for responding. I didn’t do any of that intentionally. I can’t remember where I got the Applescript from. It was probably from someone on these forums. I can’t code in Applescript. I have just enough to have a dim understanding of what each part of the code does.
All I know is that it used to pull the date from the Bookends record and put it in the Pub.Date field. I am pretty sure it stopped working when I updated to Devonthink 4. And Blufrog said they rewrote their script for importing Bookends information. That’s all I know. And I wish I could just have a menu or something where I could choose what data to import from Bookends and where it should go, but apparently I need to use a script. The fact that I don’t really know how to do that, don’t have time to learn, and don’t know what to do when something breaks is why I am asking if anyone else who knows more than me can see an obvious problem. Failing that, I will just do it manually from now on when I import new items.
The script you have posted will pull the publication date from a BE reference and put it into the Pub.Date field in DT. I suspect this script is not the source of your problems. The main problems you are having are more likely because the new method in DT 4 for importing from BE does not import in the same way as the method used in DT 3.
Have you asked DT Support directly for help?
I suspect this is a mistake of the original author of the script. I suspect it does no harm because I suspect that the second call using word - 1 simply returns the primary id string since the first object string from the end of a one object string is the object string itself.
Otherwise, the other failures simply put nothing in the DT record, which, while sloppy programming, is basically harmless.
–
JJW
From the script you posted…

Do you have your Pub.Date set to a Date type in the Data settings? If so, the date will be fully fleshed out, like so…

Also…
- That information comes from Bookends, so if it’s listed as
2012 November that’s what is will tell DEVONthink via the script, e.g.,…


- Bookends is also in development, including its AppleScript capabilities, so things can be affected by their changes too.
Here is a subtle modification of the script that utilizes a bit of local AI to parse the year from the publication date…
-- For DEVONthink 4.x
-- This is used for testing the script in Script Editor
tell application id "DNtp" to my performSmartRule(selected records)
on performSmartRule(theRecords)
tell application id "DNtp"
repeat with theRecord in (selected records)
set recordURL to (URL of theRecord)
if recordURL begins with "bookends://" then
set publicationDate to my bookendslookup(word -1 of recordURL)
if (publicationDate is not "") then
set parsedYear to get chat response for message "Extract and return ONLY the year from this: " & publicationDate engine Ollama model "qwen2.5:0.5b" without tool calls
if (count (words of parsedYear)) is 1 then
add custom meta data parsedYear for "Pub.Date" to theRecord
set comment of theRecord to (comment of theRecord & "Custom date added: " & (short date string of (current date)) & linefeed)
end if
else
log message "Publication date not found or Bookends library is not open"
return
end if
end if
end repeat
end tell
end performSmartRule
on bookendslookup(beURL)
tell application id "com.sonnysoftware.bookends2"
if (exists (library window 1)) then
tell library window 1
set matchedPub to (first publication item whose id is beURL)
return (publication date string of matchedPub)
end tell
else
return ""
end if
end tell
end bookendslookup
Note: I purposefully used a very small local AI model in this script as it’s fast and capable of doing such menial tasks, even on meager machines.