OS9-Quark 4.11 script not working in Classic

I use to use this code in os9 and Quark 4.11 to find out if a page is Recto or verso (Odd recto even verso). This snipit would tell me yet now it doesn’t work

try
set lmno to 1 / 2 as integer
set lmno to “verso”
on error
set lmno to “recto”
end try
return lmno

In os9 this would return recto yet any number that I used is showing up as verso. Im assuming “as integer” changed.
Thanks

Maybe this will work.

set Imno to my recto_verso(1)

on recto_verso(this_number)
	if this_number mod 2 is not 0 then
		return "recto"
	else
		return "verso"
	end if
end recto_verso

– Rob

Thanks ,
Thats works.
Do you know what happend to “as integer” from 10.2 to 10.3.2? Just wondering.

As of 10.3, AppleScript will coerce a real to an integer so your code, “set x to 1.5 as integer” will not result an an error and you will get all even numbers. Using mod always works.

Jon

Are you sure this is what you used? I don’t think the syntax:

myFloatingPoint as integer

ever worked. However, this hack always has:

{myFloatingPoint} as integer

It has the added benefit of rounding according to IEEE rules. You can also “floor” (round towards zero) a real by using integer divsion:

myFloatingPoint div 1

Nigel Garvey has a very fully featured script library for rounding:
http://files.macscripter.net/scriptbuilders/ScriptTools/aRounderRound.sit

P.S. Why can't these online forums just use <pre></pre> tags, so that exactly what we type is exactly what we see?

The whole point was that AppleScript previously could not make this coercion which is why it was wrapped in the try block. They didn’t want the value as an integer but the result as a boolean as to whether or not the page was odd or even, left or right. When this coercion failed, the boolean was tripped. When Apple changed AS to allow reals to be coerced to integers, this type of boolean test no longer worked because it would never fail and every page would be considered an even page when, obviously, half of them weren’t. This thread really wasn’t about rounding.

Jon

Sorry, I hadn’t quite realized the nature of the problem. In other words, if a given page number is diveded by 2, and then an attempt is made to coerce the result to integer, either it would succeed or fail for a fractional result. As you pointed out, (and I failed to notice the first time around), the mod operator is far more appropriate for this purpose:

if ( pageNum mod 2 = 0 ) -- pageNum is even

Sorry that should have been “wrapped in a try block”, not “tell block” (I edited it above).

Jon

Hi,

This is a bug until they rewrite the AppleScriptLanguageGuide. Here’s an idea without the ‘mod’ operator:

set the_num to 2.1
set imno to “recto”
try
if (the_num as integer) = the_num then set imno to “verso”
end try
imno

gl,

This is not a bug, this is a feature (see the AppleScript 1.9.2 Release Notes, search for “2849518”). Why not use mod? This is guaranteed to work across all versions of AppleScript with a minimum of fuss.

Jon

Hi Jonn,

I haven’t tested this exact thing, but I have noticed when timing things that mathematical operations take longer than anything else. I may be wrong here and haven’t tested it yet but I was thinking that a boolean result may come faster than the result of a mathematical operation. That’s why I said that this is an idea. I may test it out later when I’m in my normal mind.

Have a good one!

I am having a good one.

Use mod (returns the remainder) for petes sake.


5 mod 2 
-- = 1
10 mod 2 
-- = 0

if num mod 2 = 0 then -- even
-- code here
else -- odd
-- code here
end if

I know Pete!