hmmmm…I’m hoping this is one of those “stupid” questions I’m so famous for. But I’m working up a set of scripts that I can reuse as library scripts. On of the libraries has sorting algorithms. The following script works fine if you just run it:
set newList to {}
set theList to {6, 5, 4, 3, 2, 1}
set newList to bubbleSort(theList)
return newList
on bubbleSort(theList)
script bs
property alist : theList
end script
set theCount to length of bs's alist
if theCount < 2 then return bs's alist
repeat with indexA from theCount to 1 by -1
repeat with indexB from 1 to indexA - 1
if item indexB of bs's alist > item (indexB + 1) of bs's alist then
set temp to item indexB of bs's alist
set item indexB of bs's alist to item (indexB + 1) of bs's alist
set item (indexB + 1) of bs's alist to temp
end if
end repeat
end repeat
return bs's alist
end bubbleSort
However, when I try to call it from another program, I get WEIRDNESS (sound of B Sci-FI movie weird music, “oooo-EEEEEE-oooo”). Here’s the calling program:
property myList : {}
property newList : {}
--load the library
set scriptPath to (path to scripts folder from user domain) & "Script Library:"
set sortLib to load script ((scriptPath as text) & "sortLib.scpt") as alias
--choose a sort
set sortList to {"Bubble", "Bubble Swap", "Insertion", "Merge", "Quick"}
set sortName to choose from list sortList without multiple selections allowed and empty selection allowed
--ask for list length
display dialog "Sort how many items?" default answer "1000"
set maxItems to (text returned of the result) as number
--ask for data distribution
display dialog "Random or partially ordered items?" buttons {"Cancel", "Partially Ordered", "Random"} default button "Random"
if the button returned of the result is "Random" then
repeat with myNum from 1 to maxItems
set myList to myList & (random number from 1 to maxItems with seed myNum)
end repeat
else
beep
set partitionValue to (maxItems div 2)
repeat with myNum from 1 to maxItems
if myNum < partitionValue then
set myList to myList & (random number from 1 to maxItems with seed myNum)
else
set myList to myList & (random number from partitionValue to maxItems with seed myNum)
end if
end repeat
end if
set beginTime to current date --save the starting time
if sortName is "Bubble" then --pick the sort and do it
set newList to sortLib's bubbleSort(myList)
else if sortName is "Bubble Swap" then
set newList to sortLib's bubbleSwapSort(myList)
else if sortName is "Insertion" then
set newList to sortLib's insertSort(myList)
else if sortName is "Merge" then
set newList to sortLib's mergeSort(myList)
else
set newList to sortLib's quickSort(myList)
end if
set endTime to current date --get the end time
--report the results
display dialog (maxItems as text) & " items sorted in " & (endTime - beginTime) & " seconds using " & sortName & " sort."
return newList
If I say I want to bubble sort 10 random items, I get back this ridiculous mess:
{6, 4, 4, 5, 7, 7, 7, 9, 8, 10, 6, 4, 4, 5, 7, 7, 7, 9, 8, 10, 6, 4, 4, 5, 7, 7, 7, 9, 8, 10, 6, 4, 4, 5, 7, 7, 7, 9, 8, 10, 6, 4, 4, 5, 7, 7, 7, 9, 8, 10, 9, 10, 4, 5, 7, 7, 7, 4, 6, 8, 9, 10, 4, 5, 7, 7, 7, 4, 6, 8, 9, 10, 4, 5, 7, 7, 7, 4, 6, 8}
My other library files and their associated calling scripts don’t seem to have this problem. In fact, if I use another calling program and just give it a static list that I hardcode in myself, like this:
--load the library
set scriptPath to (path to scripts folder from user domain) & "Script Library:"
set sortLib to load script ((scriptPath as text) & "sortLib.scpt") as alias
set newList to {}
set theList to {6, 5, 4, 3, 2, 1}
set newList to sortLib's bubbleSwapSort(theList)
return newList
I get the proper result. I’m at my wit’s end, I’ve tried copying the large calling program’s code to another new file, thinking it was polluted somehow. I’ve checked the unsorted file passed to the library handler, and it’s just a list of unsorted digits (10 of them if I ask for 10 of them)
Am I missing something here?