Script: Report Stats

Users asking other users for AppleScripts that work with Bookends.
Post Reply
DrJJWMac
Posts: 343
Joined: Sat Jun 22, 2019 8:04 am
Location: Alabama 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
--
JJW
DrJJWMac
Posts: 343
Joined: Sat Jun 22, 2019 8:04 am
Location: Alabama 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
--
JJW
DrJJWMac
Posts: 343
Joined: Sat Jun 22, 2019 8:04 am
Location: Alabama USA

Re: Script: Report Stats

Post by DrJJWMac »

This update has a few improvements.

Code: Select all

(*
Report on Statistics of Library
version 2022-05-07
- puts report in default library for attachments
version 2021-11-24
- cleaned up reporting
version 2021-07-17
author jjw
- open a library
- run this script
- searches over all publications
- reported errors are ORPHANED or NO ATTACHMENT
*)

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 : false
property startDialog : false
-- set the global error label value in Bookends (use -1 to keep current label intact)
property errLabel : 3
-- set a Finder tag index on an attachment file when it is processed (use 0 to not tag at Finder level)
property tagAttachment : 1

on run {}
	
	if setupDialog is true then
		set theAction to (display dialog "How to label orphans in BE (-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 theAttachmentsSubFolder to (the library attachments subfolder path of the front library window) 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,Status" & 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 theStatus 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 theStatus to "ATTACHMENT " & 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 & "," & theStatus & return) as text
				end repeat
			else
				-- store info for publications without attachments
				set theStatus to "NO ATTACHMENT"
				set theAReport to theAReport & (thePubYear & "," & theFirstAuthor & "," & thePubTitle & ", NONE," & theStatus & return) as text
			end if
			set theReport to theReport & theAReport as text
		end repeat
	end tell
	set reportName to my writeValuestoFile(theReport, theAttachmentsSubFolder, theLibraryName)
	set theDisplayText to "Report is written as " & reportName & return & ¬
		"Processed: " & nTotalEntrees & " total references" & return & ¬
		"Attachments: " & NTotalAttachments & tab & "OK: " & NTotalExists & tab & " Orphaned: " & NTotalOrphaned
	display dialog theDisplayText buttons "OK"
end run

on writeValuestoFile(theReport, theAttachmentsSubFolder, theLibraryName)
	
	set theDate to do shell script "date '+%Y%m%d%H%M'"
	set theFileName to theAttachmentsSubFolder & "/ Library 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
--
JJW
DrJJWMac
Posts: 343
Joined: Sat Jun 22, 2019 8:04 am
Location: Alabama USA

Re: Script: Report Stats

Post by DrJJWMac »

This version is redesigned to open with a multi-choice dialog. You only need to pre-define two properties: errLabel is the label number to use to designate references with orphaned attachments and tagAttachment is the tag color number to use at the Finder level. Pre-setting each to -1 will avoid labelling the orphans and tagging attachments regardless of your dialog selection.

Code: Select all

(*
Report on Statistics of Library
version 2023-04-04
reports statistics on number of orphaned attachements
when desired, will also ...
- generate a written csv report on each reference in library
- label orphaned attachments in library
- tag attachements at Finder
*)

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

-- set the global error label value in Bookends (use -1 to keep current label intact)
property errLabel : 5
-- set a Finder tag index on an attachment file when it is processed (use 0 to not tag at Finder level)
property tagAttachment : 1

on run {}
	
	set SaveReport to 1
	set ActionList to {"Just Report", "Save Report", "Label Orphans in Bookends", "Tag Found at Finder"}
	
	choose from list ActionList with prompt "Choose actions (report always given)" default items {"Just Report"} with multiple selections allowed and empty selection allowed
	if result is equal to false then
		return
	else
		set theResult to result
	end if
	
	if theResult does not contain "Save Report" then set SaveReport to 0
	if theResult does not contain "Label Orphans in Bookends" then set errLabel to -1
	if theResult does not contain "Tag Found at Finder" then set tagAttachment to -1
	
	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 theAttachmentsSubFolder to (the library attachments subfolder path of the front library window) 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
		
		set theReport to "Year,First Author,Title,Attachment,Status" & 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
						if (errLabel ≥ 0) then set the label color of thePub to errLabel
						set theStatus 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 theStatus to "ATTACHMENT " & 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 & "," & theStatus & return) as text
				end repeat
			else
				-- store info for publications without attachments
				set theStatus to "NO ATTACHMENT"
				set theAReport to theAReport & (thePubYear & "," & theFirstAuthor & "," & thePubTitle & ", NONE," & theStatus & return) as text
			end if
			set theReport to theReport & theAReport as text
		end repeat
	end tell
	if (SaveReport > 0) then
		set reportName to my writeValuestoFile(theReport, theAttachmentsSubFolder, theLibraryName)
		set theDisplayText to "Report is written as " & reportName & return & ¬
			"Processed: " & nTotalEntrees & " total references" & return & ¬
			"Attachments: " & NTotalAttachments & tab & "OK: " & NTotalExists & tab & " Orphaned: " & NTotalOrphaned
		set theAction to display dialog theDisplayText buttons {"Open Report", "Exit"} default button "Exit"
		if the button returned of theAction is "Open Report" then
			set theFile to the POSIX file reportName as alias
			tell application "Numbers"
				activate
				open theFile
			end tell
		end if
	else
		set theDisplayText to "Report" & return & ¬
			"Processed: " & nTotalEntrees & " total references" & return & ¬
			"Attachments: " & NTotalAttachments & tab & "OK: " & NTotalExists & tab & " Orphaned: " & NTotalOrphaned
		set theAction to display dialog theDisplayText buttons {"Exit"} default button "Exit"
	end if
end run

on writeValuestoFile(theReport, theAttachmentsSubFolder, theLibraryName)
	
	set theDate to do shell script "date '+%Y%m%d%H%M'"
	set theFileName to theAttachmentsSubFolder & "/ Library 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
--
JJW
Post Reply