AppleScript: Don't let the user continue until the value is greater than 0

Viewed 73

I am trying to make the user to input the amount of X things, but these things can't be lower than 0, and in the case the user input more than 32 the code should wanrn the user but still allow to continue if chose to.

This is the code I have so far:

set aerender_clone to 0
set USER_instances to 0

repeat until USER_instances ≥ 1
    set USER_instances to the text returned of (display dialog "Render instances" default answer 1 with icon note buttons {"Cancel", "Continue"} default button "Continue")
    if USER_instances < 0 then
        display dialog "Atleast 1 instance must be selected" with icon note buttons {"sure, ok"} default button "sure, ok"
    else if USER_instances > 32 then
        display dialog "We recomend a maximum of 32, still want to continue?" with icon note buttons {"No", "Yes"} default button "No"
        set USER_instances to 0
    else
        display dialog "Code is working!"
        set aerender_clone to USER_instances
    end if
end repeat

The thing is that If I input a number like 4, 8, 6, the code keeps warning me that my number is greater than 32, but if I input 12 the code continues.

I am a bit lost here.

2 Answers

You'll want to set your USER_instances variable to an actual number value with as number:

set USER_instances to the text returned of (display dialog "Render instances" default answer 1 with icon note buttons {"Cancel", "Continue"} default button "Continue") as number

The comparison should work after adding as number to the end of the declaration.

If anyone need it, this is, now, the correct code, thanks to l'L'l answser.

set aerender_clone to 0 as number
set USER_instances to 0 as number

set UserOptions to {yes, no}
set USERcanContinue to no

repeat until USERcanContinue contains yes
    repeat until USER_instances is greater than 0
        set USER_instances to (the text returned of (display dialog "Render instances" default answer 1 with icon note buttons {"Cancel", "Continue"} default button "Continue")) as number
        if USER_instances is less than 1 then
            display dialog "Atleast 1 instance must be selected" with icon note buttons {"sure, ok"} default button "sure, ok"
        else
            -- skip
        end if
    end repeat

    if USER_instances is greater than 32 then

        display dialog "We recomend a maximum of 32, still want to continue?" with icon note buttons {"No", "Yes"} default button "No"
        if button returned of result = "No" then
            set USERcanContinue to no
            set USER_instances to 0 as number
        else
            if button returned of result = "Yes" then
                set USERcanContinue to yes
            end if
        end if
    else
        set USERcanContinue to yes
    end if
end repeat

set aerender_clone to USER_instances
Related