Script: Report Stats

Users asking other users for AppleScripts that work with Bookends.
Post Reply
DrJJWMac
Posts: 145
Joined: Sat Jun 22, 2019 8:04 am
Location: south east USA

Script: Report Stats

Post by DrJJWMac »

This script will report stats for the front library window.

* Blank (missing) attachments --> also colored to a specific label color
* Empty (no) attachments

Code: Select all

(*
Report Statistics
version 2021-06-28
author jjw
- open a library
- run this script
- searches over all publications for blank (missing) attachment and empty (no) attachment
*)

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

-- set this to the label color value to assign for a blank (missing) attachment
-- set to -1 to ignore label color change
property blankLabel : -1

on run {}
	
	-- set values	
	set theListBlank to {}
	set theListEmpty to {}
	
	tell application "Bookends"
		
		activate
		
		-- collect the bibliographies to process
		set thePList to every publication item of front library window
		set nTotal to the number of items in thePList
		
		-- process
		repeat with thePub in thePList
			if the attachment items of thePub is not {} then
				-- process publications that should have attachments
				set theAttachmentFullPath to (the path of the first attachment item of thePub) as text
				if theAttachmentFullPath is "" then
					-- store info when attachment file path is blank
					set thePubDate to the publication date string of thePub as text
					set thePubAuthors to the authors of thePub as text
					copy {pubDate:thePubDate, pubAuthors:thePubAuthors, pubPath:theAttachmentFullPath} to the end of theListBlank
					if blankLabel ≥ 0 then set the label color of thePub to blankLabel
				end if
			else
				-- store info for publications without attachments
				set thePubDate to the publication date string of thePub as text
				set thePubAuthors to the authors of thePub
				set thePubID to the id of thePub as text
				copy the {pubDate:thePubDate, pubAuthors:thePubAuthors, pubID:thePubID} to the end of theListEmpty
			end if
		end repeat
		my reportValues(nTotal, theListBlank, theListEmpty)
	end tell
	
end run

on reportValues(nTotal, theListBlank, theListEmpty)
	
	set nBlank to the number of items in theListBlank
	set nEmpty to the number of items in theListEmpty
	
	set theDialog to "Total: " & nTotal & tab & tab ¬
		& "Blank: " & nBlank & tab & tab ¬
		& "Empty: " & nEmpty
	
	display dialog theDialog buttons {"OK"}
	
end reportValues

DrJJWMac
Posts: 145
Joined: Sat Jun 22, 2019 8:04 am
Location: south east USA

Re: Script: Report Stats

Post by DrJJWMac »

This updated script has additional features.

* You can control whether or not to run without dialogs by setting the Dialog booleans. Turning off the dialogs can be useful once you know what the script is doing.

* You can control whether or not to label a publication item when it has an ORPHANED attachment. This combines what is otherwise two steps in the current implementation in Bookends: all publication items with orphans are found (the first context menu click) and each publication item with orphans is labeled (a second context menu click after orphans are "selected" from the first step).

* You can control whether or not to label each FOUND attachment with a specific Finder index value. This is somewhat equivalent to the MAROONED attachments in Bookends but with a twist. Those files in the Finder that are truly attachment files for the library are designated with the label index (e.g. 1 is ORANGE). You can use this *directly at the Finder level* to review (categorize/group) files that are true attachments versus those that are not (for the library that was reviewed). By example, when you have only one library and all files supposedly stored in the root Attachments folder, any file in the Attachments folder that is not designated with an ORANGE label after running this script is not an attachment in your library.

* You get a CSV report across the full library at the end with Year, First Author, Title, Full Path to Attachment, and Attachment Level (Exists as value N, NONE, or ORPHANED). You can open this for example in Numbers to review such information as whether the attachment for a publication item is not present or orphaned and where attachments may be (e.g. scattered throughout various folder locations at the Finder level rather than all in one folder at the Finder level).

This code does not change anything stored in the library database or rename files at the Finder level.

Code: Select all

(*
Report on Statistics of Library
version 2021-07-17
author jjw
- open a library
- run this script
- searches over all publications
*)

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

-- set these to true to be prompted for dialogs (setup and start)
property setupDialog : true
property startDialog : true
-- set the default label value in Bookends (use -1 to keep current label intact)
property errLabel : 1
-- set the default label value for a file (use 0 to not tag)
property tagAttachment : 1

on run {}
	
	if setupDialog is true then
		set theAction to (display dialog "Label missing (-1 is no change)?" buttons {"Do", "Cancel"} default button "Do" cancel button "Cancel")
		if the button returned of theAction is "Cancel" then end
		set errLabel to the (text returned of theAction) + 0
	end if
	
	set theAction to ""
	
	tell application "Bookends"
		activate
		set LName to the (name of the front library window) as text
		set theLibraryName to (text items 1 thru -5 of LName) as text
		set theRootAttachmentFolder to the default attachments path as text
		
		set NTotalAttachments to 0
		set NTotalExists to 0
		set NTotalOrphaned to 0
		
		set thePList to every publication item of front library window
		set nTotalEntrees to the number of items in thePList
		
		if startDialog is true then
			set theAction to (display dialog "Ready to process " & nTotalEntrees & "?" buttons {"Do", "Cancel"} default button "Do" cancel button "Cancel")
			if the button returned of theAction is "Cancel" then end
		end if
		
		set theReport to "Year,First Author,Title,Attachment,Action" & return
		
		-- process
		repeat with thePub in thePList
			
			set thePubYear to the publication date string of thePub as text
			set thePubTitle to ("\"" & the title of thePub & "\"")
			set theAuthors to the author names of thePub as list
			if the number of items in theAuthors > 1 then
				set theFirstAuthor to ("\"" & the first item of theAuthors & "\"") as text
			else
				set theFirstAuthor to ("\"" & theAuthors & "\"") as text
			end if
			set theAReport to ""
			
			-- check for attachments
			if the attachment items of thePub is not {} then
				
				-- process publications that should have attachments
				
				set NAttached to 0
				
				repeat with theAttachment in the attachment items of thePub
					set theAttachmentName to the name of theAttachment as text
					set theAttachmentFullPath to (the path of theAttachment) as text
					if theAttachmentFullPath is "" then
						-- store info when attachment file path is blank
						set the label color of thePub to errLabel
						set theAction to "ORPHANED"
						set NTotalOrphaned to NTotalOrphaned + 1
					else
						-- store info about attachment
						if tagAttachment > 0 then
							set theFile to the POSIX file theAttachmentFullPath as alias
							tell application "Finder" to set the label index of theFile to tagAttachment
						end if
						set theAttachmentFullPath to "\"" & theAttachmentFullPath & "\"" as text
						set theAction to "EXISTS as " & NAttached as text
						set NTotalExists to NTotalExists + 1
					end if
					set NAttached to NAttached + 1
					set NTotalAttachments to NTotalAttachments + 1
					set theAReport to theAReport & (thePubYear & "," & theFirstAuthor & "," & thePubTitle & "," & theAttachmentFullPath & "," & theAction & return) as text
				end repeat
			else
				-- store info for publications without attachments
				set theAction to "NONE"
				set theAReport to theAReport & (thePubYear & "," & theFirstAuthor & "," & thePubTitle & ", NONE," & theAction & return) as text
			end if
			set theReport to theReport & theAReport as text
		end repeat
	end tell
	set reportName to my writeValuestoFile(theReport, theRootAttachmentFolder, theLibraryName)
	set theDisplayText to "Report is written as " & reportName & return & ¬
		"Total Attachments: " & NTotalAttachments & tab & "OK: " & NTotalExists & tab & " Orphaned: " & NTotalOrphaned
	display dialog theDisplayText buttons "OK"
end run

on writeValuestoFile(theReport, theRootAttachmentFolder, theLibraryName)
	
	set theDate to do shell script "date '+%Y%m%d%H%M'"
	set theFileName to theRootAttachmentFolder & "report_" & theLibraryName & "_" & theDate & ".csv" as string
	set theOpenedFile to open for access POSIX file theFileName with write permission
	write theReport to theFileName starting at eof
	close access theOpenedFile
	return theFileName
	
end writeValuestoFile

Post Reply