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
Sorting Citations (from Dimensions.ai) in User14
-
- Posts: 17
- Joined: Sun Dec 01, 2013 4:43 am
Re: Sorting Citations (from Dimensions.ai) in User14
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.
Jon
Sonny Software
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
Sonny Software
-
- Posts: 17
- Joined: Sun Dec 01, 2013 4:43 am
Re: Sorting Citations (from Dimensions.ai) in User14
Thanks Jon. I'll give it a go. Bob
Re: Sorting Citations (from Dimensions.ai) in User14
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
JJW