Page 1 of 1

Searching for recently annotated PDFs

Posted: Thu Apr 10, 2025 9:27 am
by Jon
A request was made in the Bookends forum for an SQL search that could do this, but as explained there that isn't possible.

viewtopic.php?p=28699#p28699

However, it should be possible via AppleEvents. I've written an AppleScipt that finds references with attached PDFs that were modified in the last 2 weeks. The results are placed in a new group called "PDFs modified in Last Two Weeks".

The time interval (e.g. last week, last 2 days, etc.) and folder name can be changed in the script to suit your needs.

I'm certainly no AppleScript maven, and I'm sure it can be improved in many, many ways. There is only rudimentary error checking, and because it has to interact with the Finder and System Events, it will be relatively slow. But since it works asynchronously, you can use Bookends while it's running.

Code: Select all

use AppleScript version "2.5" -- macOS 10.11 or later
use scripting additions


tell application "Bookends"
	try -- check for open library window
		set w to first library window
	on error errmsg -- you can display the actual error message if you prefer
		display dialog "No Bookends library window is open."
		return
	end try
	
	tell front library window
		set attGroup to group attachments
		
		set pubsWithFiles to publication items of group attachments -- get all refs with attachments
		
		set allPathNames to {}
		set allRefsWithValidAttachments to {}
		repeat with i from 1 to count of pubsWithFiles
			set aPub to item i of pubsWithFiles
			
			set theattachments to attachment items of aPub
			
			repeat with p from 1 to count of theattachments
				set attachment to item p of theattachments
				set mypPath to path of attachment
				if mypPath contains ".pdf" then -- only search for modified PDFs.
					copy path of attachment to end of allPathNames
					copy aPub to end of allRefsWithValidAttachments
				end if
			end repeat
		end repeat
		
		
		-------------------------
		
		-- Use System Events get modification dates of each attached PDF
		set nowDateTime to (current date)
		set cutOffDate to nowDateTime - 14 * days -- looking for PDFs modified in the last 2 weeks. You can modify this and folder name.
		set refsWithModifiedPDFs to {}
		repeat with i from 1 to count of allPathNames
			set thePath to item i of allPathNames
			tell application "System Events" to set modDate to modification date of file thePath
			if modDate > cutOffDate then
				copy item i of allRefsWithValidAttachments to end of refsWithModifiedPDFs
			end if
		end repeat
		
		---------------
		
		-- create a new group (if necessary) and populate with references where attached PDFs were annotated in the last X days.
		set groupName to "PDFs Modified in Last Two Weeks"
		set myGroups to group items
		set groupExists to false
		repeat with i from 1 to count myGroups
			if groupName = id of item i of myGroups then
				set groupExists to true
				set theGroup to item i of myGroups
				set pubs to publication items in theGroup
				remove pubs from theGroup
				
				exit repeat
			end if
		end repeat
		
		if groupExists is false then
			make new group item with properties {name:groupName}
		end if
		
		add refsWithModifiedPDFs to group item groupName
		
	end tell
end tell
There are a number of ways to run this script. This simplest is to paste it into Script Editor and save it as a Script or Application.

Jon
Sonny Software

Re: Searching for recently annotated PDFs

Posted: Fri Apr 11, 2025 10:05 am
by DrJJWMac
Thanks Jon. I've modified to bring the modifiable things up front and to streamline some of the processing. It may run a bit faster.

Code: Select all

use AppleScript version "2.5" -- macOS 10.11 or later
use scripting additions

property showerrmsg : false
property daysBack : 14
property groupName : "PDF Annotations Modified Recently"

on run
	
	tell application "Bookends"
		
		-- exit if no library window is open
		try
			set w to first library window
		on error errmsg
			if showerrmsg then
				display dialog errmsg
			end if
			return
		end try
		
		tell front library window
			
			-- find all PDF attachments
			set attGroup to group attachments
			set pubsWithFiles to publication items of group attachments
			set thePDFList to {}
			repeat with aPub in pubsWithFiles
				set theattachments to attachment items of aPub
				repeat with theAttachment in theattachments
					set myPath to path of theAttachment
					if myPath contains ".pdf" then
						copy {pathName:path of theAttachment, pubName:aPub} to end of thePDFList
					end if
				end repeat
			end repeat
			
			-- use System Events get modification dates of each attached PDF
			set nowDateTime to (current date)
			set cutOffDate to nowDateTime - daysBack * days
			set refsWithModifiedPDFs to {}
			repeat with theItem in thePDFList
				set thePath to pathName of theItem
				tell application "System Events" to set modDate to modification date of file thePath
				if modDate > cutOffDate then
					copy pubName of theItem to end of refsWithModifiedPDFs
				end if
			end repeat
			
			-- create group			
			if (refsWithModifiedPDFs is not {}) then
				try
					delete group item groupName
				end try
				make new group item with properties {name:groupName}
				add refsWithModifiedPDFs to group item groupName
			end if
			
		end tell
	end tell
	
end run

Re: Searching for recently annotated PDFs

Posted: Fri Apr 11, 2025 10:19 am
by Jon
Thanks, your version is cleaner.

Jon
Sonny Software