Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Question:

Question: How to get random numbers to be in order

Hi, I got a big chunk of numbers written in Pages and I need them to be in ascending order. Is there some way of doing this automatically? Thank you for your help.


User uploaded file

MacBook Pro with Retina display, macOS Sierra (10.12.5)

Posted on

Reply
Question marked as Solved
Answer:
Answer:

This seems to have worked. Notes below.

23,24,24,35,44,51,52,53,54,55,61,74,74,90,90,91,93,104,105,110,113,114,122,124,1 28,128,130,131,132,132,136,143,149,150,151,152,153,154,156,157,157,158,160,161,1 64,165,166,167,168,181,181,183,190,191,192,193,194,196,198,200,203,203,204,205,2 05,208,208,209,211,211,212,212,214,214,215,216,218,218,219,219,219,220,226,231,2 32,232,235,236,237,238,249,255,256,263,267,268,270,271,272,273,276,277,279,280,2 81,283,284,286,287,287,287,288,288,292,292,296,296,305,306,308,309,309,310,313,3 14,319,320,323,324,326,330,342,352,356,357,358,362,365,370,377,380,385,389,391,4 18,418,419,420,420,420,420,422,422,424,425,425,427,427,428,429,429,436,436,437,4 38,443,443,444,445,445,445,449,450,452,453,454,455,457,458,464,465,466,468,469,4 70,474,476,482,482,485,490,493,496,498,501,501,502,503,504,521,522,525,528,529,5 30,530,530,533,535,536,536,536,537,538,538,539,540,542,542,542,543,545,546,550,5 51,553,558,559,570,573,573,574,577,582,582,585,589,589,593,594,596,596,597,601,6 03,603,605,609,610,611,621,635,641,646,647,648,649,649,649,651,653,657,657,658,6 59,694,697,701,704,705,705,708,709,723,728,731,743,743,745,746,748,752,753,757,7 59,760,761,761,762,764,771,771,773,773,774,775,776,797,804,811,812,832,844,852,8 55,856,869,877,881,887,892,894,895,897,908,909,915,917,922,924,924,924,924,924,9 24,924,925,926,1099


Copied from your post.

Pasted into a new TextEdit document as Plain text.

Set to wrap to page.

Deleted newline characters forcing line breaks inside numbers, and those forcing new lines not at the right edge of the page.

Deleted stray spaces.

Used Find/Replace to replace all commas with returns.

Copied file content, now a list on the numbers, one per line, separated by returns.

Opened a new Numbers document, selected cell B2 (top left non-header cell) and Pasted

Selected column B and sorted ascending.

Copied the sorted list.

Pasted back to new Textedit document.

Used Find/Replace to replace the returns with commas.

Copied.

Pasted here.


EDIT: The split numbers at the ends of some rows do not appear in the editor when composing the message. You may have to manually trim them after copying to the destination.


Regards,

Barry

Posted on

Question marked as Helpful

Mar 13, 2018 10:26 PM in response to HoncPe In response to HoncPe

Given the selected block of numbers that are formatted as you have shown in your updated post, the following AppleScript will sort these numbers, and write them over the existing selection. By default, it will not allow duplicates, but it is commented in the AppleScript where this occurs, and if you want duplicates, you simply comment out the unique line, and uncomment the duplicates line.


Tested with Pages v5.6.2, and v6.3.1.


Make sure that you have your original number information saved in the comma-separated formatting before you run this script.


The following is scrollable text, so click in the top left of the recessed, scrollable content, and drag until you reach the last line (return) of the script. This will be a click, and drag downward operation. Sometimes, without releasing the click, you may have to bump the bottom edge of the scrollable region to force a continue of the selection process.


Copy and paste into the Script Editor (Launchpad : Other : Script Editor), click compile button, and then with an open Pages document with the block of numbers selected, click the Run button in the Script Editor.


Before After (no duplicates)

User uploaded fileUser uploaded file

-- sorted_numbers.applescript -- sort and replace in situ, a selection of comma-delimited numbers in Pages Body Text. -- Revision 1.1: Tested with Ruby 2.2.0p648 (El Capitan 10.11.6) Ruby 2.3.3p222 (High Sierra 10.13.3) -- Ruby 2.3.6 (El Capitan 10.11.6) Ruby 2.4.3 (El Capitan 10.11.6) -- Ruby 2.4.3 (High Sierra 10.13.3) -- Author: VikingOSX, 2018-03-13, Apple Support Communities, No warranty expressed or implied. set sortedList to {} -- allow duplicate numbers if present in original selection -- set ruby_syntax to "\"v = ARGV.first.scan(/\\d+/);puts v.sort_by(&:to_i);\" " -- no duplicate numbers set ruby_syntax to "\"v = ARGV.first.scan(/\\d+/).uniq;puts v.sort_by(&:to_i);\" " tell application "System Events" tell application process "Pages" set frontmost to true keystroke "c" using command down -- copy selection to clipboard end tell end tell set nbr_text to (do shell script "pbpaste") as text set revised_text to "" repeat with aChar in nbr_text -- filter out any line break characters that may have crept in... if not aChar = return or not aChar = linefeed then set revised_text to revised_text & aChar end if end repeat set sortedList to paragraphs of (do shell script "ruby -e " & ruby_syntax & revised_text's quoted form) -- now put the sorted number list back into Pages tell application "System Events" tell application process "Pages" set foremost to true -- Add a space after each comma for legibility. This will also force a comma at line's end. set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ", "} -- replace selected text in Pages with this sorted output keystroke sortedList as text set AppleScript's text item delimiters to TID end tell end tell return

There’s more to the conversation

Read all replies

Mar 13, 2018 12:10 PM in response to HoncPe In response to HoncPe

The first step is to normalize your data to use a single type of separator (e.g. comma, comma with trailing space, or just a space). Presently, your data has a mixture of all of these separators, and worse, whole numbers are split with newlines, creating an additional number that isn't supposed to be there.


Once you get the data normalized to a single separator, without numbers split by newlines — post it back as plain text, not as an image. Then, we will have something to split into a list of numbers that can be sorted with two-lines of AppleScript/Objective-C code. This is not a job for Pages, or Finder AppleScript dictionaries, or the third-party WordService tool.

Mar 13, 2018 12:10 PM

Reply Helpful

Mar 13, 2018 1:47 PM in response to VikingOSX In response to VikingOSX

Thank you for your effort. Those are data from 20 different clients that's the reason why they are so messed up. But I normalized them by doing CMD+F in Pages:


91,232,263,313,528,542,573,610,646,658,659,657,776,93,832,924,926,122,128,132,27 2,377,380,452,454,455,529,540,542,124,273,287,501,589,585,597,603,605,609,731,74 6,748,752,753,855,887,922,44,443,449,469,482,543,574,603,635,641,657,773,804,811 ,844,908,925,136,143,149,196,308,370,924,24,90,450,493,490,553,694,877,894,205,2 14,249,425,436,498,522,74,437,869,128,130,131,132,198,226,270,271,287,330,385,44 4,445,536,649,651,653,797,812,74,205,214,218,24,181,283,420,466,151,152,153,156, 157,158,194,277,292,305,306,35,356,357,358,457,458,464,468,501,533,546,550,551,7 05,757,759,761,762,771,852,924,90,204,218,255,256,418,427,429,438,465,502,503,50 4,536,537,539,545,559,589,704,743,745,761,764,774,856,881,892,895,917,924,235,31 0,314,445,474,530,535,593,594,705,723,728,760,897,51,52,53,54,55,208,220,236,237 ,238,286,287,418,420,436,445,530,596,601,647,648,649,909,915,924,160,161,164,165 ,166,167,168,191,192,193,211,212,219,279,288,309,319,352,419,420,422,424,425,427 ,428,429,470,482,530,538,558,582,621,649,773,775,924,190,181,183,209,215,216,284 ,309,320,150,157,154,276,292,326,362,365,420,525,538,542,105,110,113,200,203,231 ,232,267,296,389,391,453,476,496,521,536,570,577,582,596,611,697,701,708,709,743 ,771,924,61,208,219,23,104,114,1099,203,296,268,422,485,573,211,212,219,280,281, 288,323,324,342,443


I don't know how to prevent numbers from splitting at the end of the line. This is just plain text so it happens automatically. Again, thank you.

Mar 13, 2018 1:47 PM

Reply Helpful
Question marked as Solved

Mar 13, 2018 10:25 PM in response to HoncPe In response to HoncPe

This seems to have worked. Notes below.

23,24,24,35,44,51,52,53,54,55,61,74,74,90,90,91,93,104,105,110,113,114,122,124,1 28,128,130,131,132,132,136,143,149,150,151,152,153,154,156,157,157,158,160,161,1 64,165,166,167,168,181,181,183,190,191,192,193,194,196,198,200,203,203,204,205,2 05,208,208,209,211,211,212,212,214,214,215,216,218,218,219,219,219,220,226,231,2 32,232,235,236,237,238,249,255,256,263,267,268,270,271,272,273,276,277,279,280,2 81,283,284,286,287,287,287,288,288,292,292,296,296,305,306,308,309,309,310,313,3 14,319,320,323,324,326,330,342,352,356,357,358,362,365,370,377,380,385,389,391,4 18,418,419,420,420,420,420,422,422,424,425,425,427,427,428,429,429,436,436,437,4 38,443,443,444,445,445,445,449,450,452,453,454,455,457,458,464,465,466,468,469,4 70,474,476,482,482,485,490,493,496,498,501,501,502,503,504,521,522,525,528,529,5 30,530,530,533,535,536,536,536,537,538,538,539,540,542,542,542,543,545,546,550,5 51,553,558,559,570,573,573,574,577,582,582,585,589,589,593,594,596,596,597,601,6 03,603,605,609,610,611,621,635,641,646,647,648,649,649,649,651,653,657,657,658,6 59,694,697,701,704,705,705,708,709,723,728,731,743,743,745,746,748,752,753,757,7 59,760,761,761,762,764,771,771,773,773,774,775,776,797,804,811,812,832,844,852,8 55,856,869,877,881,887,892,894,895,897,908,909,915,917,922,924,924,924,924,924,9 24,924,925,926,1099


Copied from your post.

Pasted into a new TextEdit document as Plain text.

Set to wrap to page.

Deleted newline characters forcing line breaks inside numbers, and those forcing new lines not at the right edge of the page.

Deleted stray spaces.

Used Find/Replace to replace all commas with returns.

Copied file content, now a list on the numbers, one per line, separated by returns.

Opened a new Numbers document, selected cell B2 (top left non-header cell) and Pasted

Selected column B and sorted ascending.

Copied the sorted list.

Pasted back to new Textedit document.

Used Find/Replace to replace the returns with commas.

Copied.

Pasted here.


EDIT: The split numbers at the ends of some rows do not appear in the editor when composing the message. You may have to manually trim them after copying to the destination.


Regards,

Barry

Mar 13, 2018 10:25 PM

Reply Helpful (1)
Question marked as Helpful

Mar 13, 2018 10:26 PM in response to HoncPe In response to HoncPe

Given the selected block of numbers that are formatted as you have shown in your updated post, the following AppleScript will sort these numbers, and write them over the existing selection. By default, it will not allow duplicates, but it is commented in the AppleScript where this occurs, and if you want duplicates, you simply comment out the unique line, and uncomment the duplicates line.


Tested with Pages v5.6.2, and v6.3.1.


Make sure that you have your original number information saved in the comma-separated formatting before you run this script.


The following is scrollable text, so click in the top left of the recessed, scrollable content, and drag until you reach the last line (return) of the script. This will be a click, and drag downward operation. Sometimes, without releasing the click, you may have to bump the bottom edge of the scrollable region to force a continue of the selection process.


Copy and paste into the Script Editor (Launchpad : Other : Script Editor), click compile button, and then with an open Pages document with the block of numbers selected, click the Run button in the Script Editor.


Before After (no duplicates)

User uploaded fileUser uploaded file

-- sorted_numbers.applescript -- sort and replace in situ, a selection of comma-delimited numbers in Pages Body Text. -- Revision 1.1: Tested with Ruby 2.2.0p648 (El Capitan 10.11.6) Ruby 2.3.3p222 (High Sierra 10.13.3) -- Ruby 2.3.6 (El Capitan 10.11.6) Ruby 2.4.3 (El Capitan 10.11.6) -- Ruby 2.4.3 (High Sierra 10.13.3) -- Author: VikingOSX, 2018-03-13, Apple Support Communities, No warranty expressed or implied. set sortedList to {} -- allow duplicate numbers if present in original selection -- set ruby_syntax to "\"v = ARGV.first.scan(/\\d+/);puts v.sort_by(&:to_i);\" " -- no duplicate numbers set ruby_syntax to "\"v = ARGV.first.scan(/\\d+/).uniq;puts v.sort_by(&:to_i);\" " tell application "System Events" tell application process "Pages" set frontmost to true keystroke "c" using command down -- copy selection to clipboard end tell end tell set nbr_text to (do shell script "pbpaste") as text set revised_text to "" repeat with aChar in nbr_text -- filter out any line break characters that may have crept in... if not aChar = return or not aChar = linefeed then set revised_text to revised_text & aChar end if end repeat set sortedList to paragraphs of (do shell script "ruby -e " & ruby_syntax & revised_text's quoted form) -- now put the sorted number list back into Pages tell application "System Events" tell application process "Pages" set foremost to true -- Add a space after each comma for legibility. This will also force a comma at line's end. set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ", "} -- replace selected text in Pages with this sorted output keystroke sortedList as text set AppleScript's text item delimiters to TID end tell end tell return

Mar 13, 2018 10:26 PM

Reply Helpful (1)

Mar 13, 2018 7:23 PM in response to VikingOSX In response to VikingOSX

Just living proof that there is always a simpler way… 😉


Select the list of numbers in Pages, and run the following AppleScript. It will copy the numbers onto the clipboard, and then use Perl to remove any inadvert white-space or newlines in the number string. It then takes this revised number string, and pulls it into a Perl array, where it is sorted uniq, and the final revised text is then shoved back onto the clipboard. Thus, you can choose another location in your Pages Body Text, and paste the sorted numbers there, without destroying the original number list.


Same output as the first AppleScript/Ruby solution.


set perl_syntax to "'-MList::MoreUtils qw(uniq)' -F',' -lane 'print uniq join \", \", sort {$a <=> $b} uniq @F;' "


tell application "System Events"

tell application process "Pages"

set frontmost to true

keystroke "c" usingcommand down-- copy selection to clipboard

end tell

end tell


set nbr_text to (do shell script "pbpaste | perl -p -i -e 's/[\\s\\n]*//g' ") as text

set revised_text to (do shell script "perl " & perl_syntax & "<<<" & nbr_text)

set the clipboard torevised_text

return

Mar 13, 2018 7:23 PM

Reply Helpful (1)
User profile for user: HoncPe

Question: How to get random numbers to be in order