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();
})();
```