Sorting Citations (from Dimensions.ai) in User14

Users asking other users for AppleScripts that work with Bookends.
Post Reply
BobMitcham
Posts: 17
Joined: Sun Dec 01, 2013 4:43 am

Sorting Citations (from Dimensions.ai) in User14

Post by BobMitcham »

Updating user14 to 5 digits

I am using User14 to record the number of citations, from Dimensions.ai and elsewhere, for each of my references. These are then displayed in a user14 column which I can sort.

The problem is that all my user14 entries don't have the same number of digits and therefore don't sort well.

It would be a great timesaver to automate the process to have an Applescript which could check the number of numerals in user14 to make sure there were 5 digits and update by adding extra zeros to the front if needed.

Anyone help?

Thanks , Bob
Jon
Site Admin
Posts: 10073
Joined: Tue Jul 13, 2004 6:27 pm
Location: Bethesda, MD
Contact:

Re: Sorting Citations (from Dimensions.ai) in User14

Post by Jon »

Here's a little script I put together. It's rudimentary, doesn't do any error checking, and can certainly be optimized. But it works in my tests.

Code: Select all

tell application "Bookends"
	
	tell front library window
		
		set pubList to every publication item
		set myList to {}
		repeat with i from 1 to count of pubList
			set aPub to item i of pubList
			if user14 of aPub is not "" then
				copy aPub to end of myList
			end if
		end repeat
		
		repeat with i from 1 to count of myList
			set aPub to item i of myList
			
			set s to user14 of aPub
			
			set thelength to (get length of (s as text))
			if thelength < 5 then
				repeat until length of (s as text) = 5
					set s to "0" & s
				end repeat
				set user14 of aPub to s
			end if
			
		end repeat
		
	end tell
end tell
Jon
Sonny Software
BobMitcham
Posts: 17
Joined: Sun Dec 01, 2013 4:43 am

Re: Sorting Citations (from Dimensions.ai) in User14

Post by BobMitcham »

Thanks Jon. I'll give it a go. Bob
DrJJWMac
Posts: 348
Joined: Sat Jun 22, 2019 8:04 am
Location: Alabama USA

Re: Sorting Citations (from Dimensions.ai) in User14

Post by DrJJWMac »

Here is a more compact version.

Code: Select all

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

on run
	set theReturns to {} -- included for debugging only
	tell application "Bookends" to tell the front library window
		set pubList to every publication item
		repeat with aPub in pubList
			set theReturn to user14 of aPub
			if theReturn is not "" then
				set theReturn to pad_u14(theReturn, 5) of me
				-- uncomment next line to allow script to make changes in user14
				-- set user14 of aPub to theReturn
				copy theReturn to end of theReturns -- included for debugging only
			end if
		end repeat
	end tell
end run

on pad_u14(inStr, nDigits)
	set thePad to ""
	set strLen to length of inStr
	if strLen < nDigits then
		set strLenDiff to nDigits - strLen
		repeat with ic from 1 to strLenDiff
			set thePad to "0" & thePad
		end repeat
	end if
	return thePad & inStr
end pad_u14

--
JJW
ComplexPoint
Posts: 9
Joined: Sun Jun 26, 2016 7:19 pm

Re: Sorting Citations (from Dimensions.ai) in User14

Post by ComplexPoint »

Here, FWIW is a JavaScript for Automation version which aims for a little more flexibility by:

1. Allowing you to specify the target field name at the top of the script,
2. deciding how many zeros are needed at the start (target width) by looking for the highest digit count seen in the data,
3. leaving unchanged any non-numeric data found in the target field (where non-numeric status is defined by JS isNaN)


To test in Script Editor on dummy data, set the language selector at top left to JavaScript rather than AppleScript

Code: Select all

(() => {
    "use strict";

    // Left-pad specified numeric field with zeros
    // (for consistent width and numeric sortability)

    const fieldName = "user14";


    // Rob Trew @2023

    // MAIN :: IO()
    const main = () => {
        const
            libWin = Application("Bookends")
            .libraryWindows.at(0);

        return libWin.exists()
            ? (() => {
                const
                    items = libWin.publicationItems,
                    values = items[fieldName](),
                    fullWidth = values.reduce(
                        (n, v) => isNaN(v)
                            ? 0
                            : Math.max(n, v.trim().length),
                        0
                    );

                return zip(items())(values)
                .flatMap(
                    ([item, value]) => isNaN(value)
                        // Any non-numeric value left unchanged.
                        ? []
                        : (
                            // EFFECT
                            // Added leading zeros
                            item[fieldName] = value.padStart(fullWidth, 0),

                            // Or pruned leading zeros, if required
                            // item[fieldName] = value.replace(/^0+/u, ""),

                            // Resulting VALUE returned as a sanity check.
                            [item[fieldName]()]
                        )
                )
                .join("\n");

            })()
            : "No library window open in Bookends";
    };


    // --------------------- GENERIC ---------------------

    // zip :: [a] -> [b] -> [(a, b)]
    const zip = xs =>
    // The paired members of xs and ys, up to
    // the length of the shorter of the two lists.
        ys => Array.from({
            length: Math.min(xs.length, ys.length)
        }, (_, i) => [xs[i], ys[i]]);


    // MAIN ---
    return main();
})();
Post Reply