set d to text item delimiters
set text item delimiters to ASCII character 10
set l to paragraphs of (do shell script "echo " & quoted form of (l as text) & "| sort") ---rn
set text item delimiters to d
In all my practice runs I’ve gotten it to work with no problem. Now, when I run it in the real world it gives an error “The command exited with a non-zero status.” The list I’m running now is much longer than my sample lists. Is it possible the size of the list is the issue? If so, what is a good way around that problem?
on bubblesort(array)
repeat with i from length of array to 2 by -1 --> go backwards
repeat with j from 1 to i - 1 --> go forwards
tell array
if item j > item (j + 1) then
set {item j, item (j + 1)} to {item (j + 1), item j} -- swap
end if
end tell
end repeat
end repeat
return array
end bubblesort
The problem is almost certain to be related to the length of the list, Shai. More accurately, there’s a limit to the length of a command (that’s the text of the command, about 40 bytes of overhead - and any environment variables). The current limit is 262,144 bytes (although, in practice, this may be more like 261,000 bytes - depending on environment settings).
To check the current limit:
do shell script "sysctl kern.argmax"
In most cases, the main cause of hitting this limit is an attempt to feed inline data to the command. The best solution, as Jacques has suggested, is to write the data to a file and read it from there, instead.
To avoid having to clean up afterwards, you might also consider writing the file to temporary items:
set p to POSIX path of (path to temporary items) & "tempsort.txt"
set f to open for access POSIX file p with write permission
-- etc...
Thanks so much for the suggestions. As it turns out, you were correct. My problem was related to the “amount” of data I was working with. Now that I’ve taken the data from a file it works like a champ!