0

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

DJpotato
  • 17
  • 7

1 Answers1

0

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
vadian
  • 274,689
  • 30
  • 353
  • 361
  • That works very well, the only problem is that it evaluates non-number folders as well and will return the folder name "latest" instead of "2.9", so I added ` if aValue is not integer then if aValue > high_amount then set high_amount to contents of aValue end if` I was wondering if there is a revised version of my number-checking part that will limit it only to "3.0" and not include ones just named "3"? – DJpotato Jul 31 '15 at 01:31
  • I updated the post to consider only values which contain a dot and are convertible to a floating point number – vadian Jul 31 '15 at 04:21
  • New problem: I added a feature in which the folder names can now be e.g. "3.0" or "3.0.1". Obviously 3.0.1 isn't a number as it has two decimal points, so is there any way to include it in the highnum() handler, as well as allow adding of e.g. "1.0.3" to "3.0.1" to get "4.0.4"? – DJpotato Aug 01 '15 at 05:12
  • Actually the `highnum()` handler considers also "numbers" with more than one decimal point, because they are strings anyway. Adding two version strings is possible but needs a lot of code in case the number of places are different. – vadian Aug 01 '15 at 07:30
  • thank you that perfectly adds them together. But since the program can now create multi-decimals, is there any way to make the highnum() handler evaluate multi-decimals so that it will recognise "1.1.1" as being higher than "1.1"? Right now the highnum() handler will only return single-decimal numbers even when there are multi-decimal folders present. Sorry for all the questions – DJpotato Aug 02 '15 at 10:15
  • you could delete the lines `try`, `aValue as real` and `end try`, but then all values which contain at least one dot will be considered as valid – vadian Aug 02 '15 at 10:26
  • could I add a line which says that any thing with a dot in it but also contains letter does not get included? Or will that have the same issue? – DJpotato Aug 02 '15 at 10:43
  • for example is there a "contains anything except" function within an if statement, like "contains" or "exists"? (e.g. "if x contains anything except y then...") – DJpotato Aug 02 '15 at 10:53
  • And now there are no problems, that solves everything. Thank you so much for your help, I want to credit you in the app, what do you to be credited as? – DJpotato Aug 02 '15 at 11:41
  • you're welcome, for those few lines of code credit is not needed – vadian Aug 02 '15 at 11:46
  • Thank you nonetheless, your help has been crucial to my project – DJpotato Aug 02 '15 at 12:14