3

Can I run an script if a variable is between 4 and 6 characters only ?

e.g

set serialNumber to ""
set theLength to (get length of (serialNumber as text))
if theLength is equal to 1 then
    set serialNumber to "No Serial"
else if theLength > 4 and < 6 then
    ##do this
else if theLength is greater than 6 then
    ## do nothing
end if

This is obviously not working : else if theLength > 4 and < 6 then

3

Change:

else if theLength > 4 and < 6 then

To:

else if theLength > 4 and theLength < 6 then

Or, since you asked "Can I run a script if a variable is between 4 and 6 characters only?", which is equal to 5, you can also use:

else if theLength = 5 then

In place of:

else if theLength > 4 and theLength < 6 then

However, if testing between two numbers that are (one or) more then one, just use the variable twice in the test as shown above.

  • 1
    For completeness—and given that "between" can be inclusive—it's worth including less than or equal to (<= or ); greater than or equal to (>= or ); and not equal to (/= or ). For example, if theLength ≥ 4 and theLength ≤ 6 then... catches theLength when its value is 4, 5 or 6; and if we know that theLength only evaluates to integers between 1 and 6 inclusive, this conditional is equivalent to the last: if theLength ≠ 1 and theLength ≠ 2 and theLength ≠ 3. – CJK Mar 13 '18 at 0:36
  • 1
    @CJK, IMO It's not totally clear what the OP is actually looking for and why I left it as is, however you made some good points and feel free to edit them into the answer if your wish. – user3439894 Mar 13 '18 at 0:43
  • 1
    Although the question is somewhat vague, I think your answer is just fine as it is. I think my additions serve better as a comment, as it's more of an aside, and not a correction or improvement, per se. – CJK Mar 13 '18 at 0:47
  • 1
    Thank you that actually perfect, @user3439894 you right I was wrong in my question "< 6 then" (because I'm not sure of the Length yet, I have to check at the office) but anyway that perfect, exactly what I needed, thank you again. – Kevin Mar 13 '18 at 10:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .