Tuesday, September 27, 2022

#1 2022-08-12 10:19:29 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2524

Quick Text Sorting Handler

Following script is designed for the cases when the text is not saved in the text file.

Applescript:


set theText to "ARG_MAX indeed limits the total size of
the command line and the environment,
but you're facing an additional limitation:
one argument must not be longer than MAX_ARG_STRLEN
(which is unfortunately hard-coded to be 131072).
See https://unix.stackexchange.com/questions/120642/what-defines
-the-maximum-size-for-a-command-single-argument"


-- or:
-- set theText to (get the clipboard as text)

repeat 8 times -- making big text
   set theText to theText & theText
end repeat
-- return length of theText --> 1388544 (about 1,4 MB) with 12 times repeat loop

my sortText(theText as text)

on sortText(theText as text)
   script o
       use AppleScript version "2.4" -- Yosemite (10.10) or later
       use framework "Foundation"
       use scripting additions
       on AsObjCsort(theList)
           -- convert list to Cocoa array
           set theArray to current application's NSArray's arrayWithArray:theList
           -- sort the array using a specific function
           set theArray to theArray's sortedArrayUsingSelector:"localizedStandardCompare:"
           -- return the sorted array as an AppleScript list
           return theArray as list
       end AsObjCsort
       on sortText(theText as text)
           set ATID to AppleScript's text item delimiters
           set AppleScript's text item delimiters to return
           try
               set sortedTextList to paragraphs of (do shell script "echo " & quoted form of theText & " | sort")
               set AppleScript's text item delimiters to ATID
               return sortedTextList
           on error -- the text is too big for shell sorting.
               set AppleScript's text item delimiters to ATID
               return o's AsObjCsort(get paragraphs of theText)
           end try
       end sortText
   end script
   o's sortText(theText)
end sortText

Last edited by KniazidisR (2022-08-12 10:28:29 am)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#2 2022-08-12 10:34:28 am

robertfern
Member
Registered: 2011-11-29
Posts: 195

Re: Quick Text Sorting Handler

So why not use th ASObjC version all the time?

Applescript:

use scripting additions
set theText to "ARG_MAX indeed limits the total size of
the command line and the environment,
but you're facing an additional limitation:
one argument must not be longer than MAX_ARG_STRLEN
(which is unfortunately hard-coded to be 131072).
See https://unix.stackexchange.com/questions/120642/what-defines
-the-maximum-size-for-a-command-single-argument"


repeat 8 times -- making big text
   set theText to theText & theText
end repeat
-- return length of theText --> 1388544 (about 1,4 MB) with 12 times repeat loop

my sortText(theText as text)

on sortText(theText as text)
   script o
       use AppleScript version "2.4" -- Yosemite (10.10) or later
       use framework "Foundation"

       on AsObjCsort(theList)
           -- convert list to Cocoa array
           set theArray to current application's NSArray's arrayWithArray:theList
           -- sort the array using a specific function
           set theArray to theArray's sortedArrayUsingSelector:"localizedStandardCompare:"
           -- return the sorted array as an AppleScript list
           return theArray as list
       end AsObjCsort
   end script
   return o's AsObjCsort(paragraphs of theText)
end sortText

I'm assuming so it would work on Versions older than Yosemite?

Last edited by robertfern (2022-08-12 10:49:55 am)

Offline

 

#3 2022-08-12 10:48:05 am

robertfern
Member
Registered: 2011-11-29
Posts: 195

Re: Quick Text Sorting Handler

Also

Here is a modified version of your code with the second "on sortText(theText as text)" subroutine moved out of the script object. (one less recursive call)

Applescript:

use scripting additions
set theText to "ARG_MAX indeed limits the total size of
the command line and the environment,
but you're facing an additional limitation:
one argument must not be longer than MAX_ARG_STRLEN
(which is unfortunately hard-coded to be 131072).
See https://unix.stackexchange.com/questions/120642/what-defines
-the-maximum-size-for-a-command-single-argument"


-- or:
-- set theText to (get the clipboard as text)

repeat 8 times -- making big text
   set theText to theText & theText
end repeat
-- return length of theText --> 1388544 (about 1,4 MB) with 12 times repeat loop

my sortText(theText as text)

on sortText(theText as text)
   script o
       use AppleScript version "2.4" -- Yosemite (10.10) or later
       use framework "Foundation"
       on AsObjCsort(theList)
           -- convert list to Cocoa array
           set theArray to current application's NSArray's arrayWithArray:theList
           -- sort the array using a specific function
           set theArray to theArray's sortedArrayUsingSelector:"localizedStandardCompare:"
           -- return the sorted array as an AppleScript list
           return theArray as list
       end AsObjCsort
   end script
   set ATID to AppleScript's text item delimiters
   set AppleScript's text item delimiters to return
   try
       set sortedTextList to paragraphs of (do shell script "echo " & quoted form of theText & " | sort")
       set AppleScript's text item delimiters to ATID
       return sortedTextList
   on error -- the text is too big for shell sorting.
       set AppleScript's text item delimiters to ATID
       return o's AsObjCsort(get paragraphs of theText)
   end try
end sortText

Last edited by robertfern (2022-08-12 10:51:14 am)

Offline

 

#4 2022-08-12 11:07:03 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2524

Re: Quick Text Sorting Handler

robertfern wrote:

Here is a modified version of your code with the second "on sortText(theText as text)" subroutine moved out of the script object. (one less recursive call)


Thank you. The change you made is helpful.
.

robertfern wrote:

So why not use th ASObjC version all the time?
I'm assuming so it would work on Versions older than Yosemite?


Not for this reason. As I checked, the shell sorting (3 ms) is little faster then AsObjC sorting (10 ms).

Last edited by KniazidisR (2022-08-12 11:19:32 am)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)