Polle Polle - 1 month ago 18
AppleScript Question

Applescript TRY and IF statements

Ok, so I'm having some trouble getting this to work.
It all works fine, as long as varNumberCR2 is greater than varNumberXMP.

With the values set to the following, I'm getting these results:

varNumberCR2: 10

varNumberXMP: 2

Results: CR2: 10 / XMP: 2 / Ratio: 5,0

varNumberCR2: 10

varNumberXMP: 0

Results: CR2: 10 / XMP: No XMP files / Ratio: Not possible to calculate

varNumberCR2: 0

varNumberXMP: 0

Results: CR2: No CR2 files / XMP: No XMP files / Ratio: Not possible to calculate

However, with the following values the varNumberCR2 does not get set to "No CR2 files"

varNumberCR2: 0

varNumberXMP: 5

Results: CR2: 0 / XMP: 5 / Ratio: 0,0

What's wrong?

set varNumberCR2 to {"0"}
set varNumberXMP to {"4"}

try
set varRatio to ((round ((varNumberCR2 / varNumberXMP) * 10)) / 10)
on error
if varNumberCR2 < "1" then
set varNumberCR2 to {"No CR2 files"}
end if
if varNumberXMP < "1" then
set varNumberXMP to {"No XMP files"}
end if
set varRatio to {"Not possible to calculate"}
end try

display dialog {"CR2: " & varNumberCR2 & return & "XMP: " & varNumberXMP & return & "Ratio: " & varRatio}

Answer

Remember to follow basic rules of math. You cannot divide by zero. You may need to add an exception for an entry of just zero.

Comments