applescripthandlers

Applescript: Getting a list of numbered folders from a shell script and identifying the highest value


I have two different handlers, one which gets the names (which are numbers like "1.0" and "1.1") of folders within a folder, then converts that to an applescript list. Then it passes that list to another handler, which evaluates the list and supposedly identifies the highest number (I got it from http://www.macosxautomation.com/applescript/sbrt/sbrt-03.html ).

on set_values(project_path)
do shell script "ls " & project_path
get words of result
set allvalues to (result)
return allvalues
end set_values

Then I turn result into a variable for the next handler:

set values_list to result

And this is the handler the get the highest number from the list, courtesy of macosxautomation:

on highnum(values_list)
set the high_amount to ""
repeat with i from 1 to the count of the values_list
    set this_item to item i of the values_list
    set the item_class to the class of this_item
    if the item_class is in {integer, real} then
        if the high_amount is "" then
            set the high_amount to this_item
        else if this_item is greater than the high_amount then
            set the high_amount to item i of the values_list
        end if
    else if the item_class is list then
        set the high_value to highnum(this_item)
        if the the high_value is greater than the high_amount then ¬
            set the high_amount to the high_value
    end if
end repeat
return high_amount
end highnum

The problem is that the second handler only ever gives off a null response, and i can't figure out why. Any help appreciated.

The point of the application is to simplify the creation of other applications, by allowing the creation of new 'projects', importing existing apps into a new 'project' and to allow editing of a 'project' with ease. In the event that you choose to edit a project, you can choose "minor update" (effectively adding a '0.0.1' to your latest version) or a number of other options i have already finished. The multi-decimal addition i can solve using text item delimiters, but I am not sure how to obtain a multi-decimal number from the highnum() handler, which is the critical part of the process


Solution

  • the highnum() handler compares real and integer values but the shell script returns string values.

    AppleScript has a built-in option to compare numeric strings.

    Edit: all values which contain at least one dot and do not contain any letter are considered.

    on highnum(values_list)
        set high_amount to "0.0"
        considering numeric strings
            repeat with aValue in values_list
                set {TID, text item delimiters} to {text item delimiters, "."}
                set aValueItems to text items of aValue
                if (count aValueItems) > 1 then
                    set text item delimiters to ""
                    try
                        aValueItems as text as integer
                        if aValue > high_amount then set high_amount to contents of aValue
                    end try
                end if
                set text item delimiters to TID
            end repeat
        end considering
        return high_amount
    end highnum
    

    this is a simpler version of the set_values() handler

    on set_values(project_path)
        return paragraphs of (do shell script "ls " & project_path)
    end set_values
    

    to add version strings you could use this handler, it considers strings with different "decimal places".

    set newVersionString to addVersionStrings("1.1.1", "2.0") --> 3.1.1
    
    on addVersionStrings(string1, string2)
        set {TID, text item delimiters} to {text item delimiters, "."}
        set textItems1 to text items of string1
        set textItems2 to text items of string2
        if (count textItems1) < (count textItems2) then
            set minCount to count textItems1
            set _sum to textItems2
            set _add to textItems1
        else
            set minCount to count textItems2
            set _sum to textItems1
            set _add to textItems2
        end if
        repeat with i from 1 to minCount
            set item i of _sum to (get (item i of _sum as integer) + (item i of _add as integer)) as text
        end repeat
        set theResult to _sum as text
        set text item delimiters to TID
        return theResult
    end addVersionStrings