InDesign scripting problem with frames

I’ve been pulling snippets of scripts from this forum and the InDesign Scripting forum over at Adobe. I’ve made some tweaks, but I’m getting frustrated with the section where the script defines how many pages to create based on the number of frames needed divided by the number of frames per page. An excerpt of the particular defined variables:


set myNumberOfFrames to (count of items of myFiles) --->36

set myNumberOfRows to 4
set myNumberOfColumns to 4

set myFramesPerPage to (myNumberOfRows * myNumberOfColumns) --->16

set myNumberOfPages to round (myNumberOfFrames / myFramesPerPage) rounding up --->36/16=2.25 --->2

For example, I have a folder of 36 images. The script is instructed to draw a 4 x 4 grid of frames per page which would mean I need 3 pages, 16+16+4. Well, when round is used with rounding up it returns the values 2 instead of 3.

I’ve read in a few other posts about some handlers written by one Nigel Garvey. They allow you round up numbers instead of to the nearest whole number within 0.5. I’m scraping by here and I’m not sure how to introduce these into the script. I’d rather not be required to add extra scripting additions to user workstations. How do these handlers work exactly.

Of note, if I simply add + 1 it will work, but it will work for all cases including returned values that actually round up. As a result I’ll end up with an extra page. On the other hand if I don’t add 1, then the script will error out when it tries to draw a frame on a page that doesn’t exist.

set myNumberOfPages to round ((myNumberOfFrames / myFramesPerPage) + 1) rounding up --->36/16+1=3.25 --->3

I thought that maybe I could have the script look at the first digit after the decimal and if it’s not zero (0) then to add an extra page. Anyone have a quick snippet for that one.

I’d appreciate any help on this. I’m somewhat desperate as the folks I work with are constantly complaining about things they can no longer do in OS X. In OS 9 we had a script that ran with Quark and a Cumulus database that grabbed all the selected items in Cumulus and built a nice little report. Now that we’re in OS X running InDesign CS, and the original developers of the Cumulus > Quark Catalog are no longer available I’m having to tackle this. Building the report from files in the Finder is my first step. After I get that working, with help from yourselves :? , I’ll hopefully be able to integrate the Cumulus part of it.

Clearly something’s wrong here, however, if I copy your script and run it (substituting the number 36 for myNumberOfFrames since I don’t have InDesign), I get the result of 3.

I can only assume that the rounding up error is specific to the version of AppleScript you’re running. What Mac OS X version are you running (I’m on 10.3.2 here).

It may be that your simplest solution is to upgrade.

However, if you do want to code it yourself, the following should give you s start:

set myNumberOfPages to (myNumberOfFrames / myFramesPerPage)

if myNumberOfPages is not (myNumberOfPages as integer) then set myNumberOfPages to (myNumberOfPages + 1) as integer

The idea here is that you compare myNumberOfPages both as the result of the calculation and when coerced to an integer. If they don’t match, you know you didn’t get a whole number of pages, so you add 1.

Hi, kc. This is one Nigel Garvey. :wink:

I don’t know why ‘rounding up’ isn’t working for you. A couple of things you could try to see if they work are: firstly put the whole of the ‘round’ command in parentheses:

set myNumberOfPages to (round (myNumberOfFrames / myFramesPerPage) rounding up)

Secondly, make sure that the ‘round’ command isn’t within a ‘tell’ block for “InDesign”. I know nothing about that appplication, but maybe there’s a terminology clash with something in InDesign’s dictionary.

My handlers are simply that - handlers. You just copy the ones you need into your script and call them as handlers. No scripting additions required. The one for rounding up to the nearest integer or whole number is:

on rndUp(n)
  tell n div 1
    if it < n then return it + 1
    it
  end tell
end rndUp

This discards the fractional part of the number. If the result is less than the original number, 1 is added; otherwise it isn’t. The process is very fast (since - unlike ‘round’ - it doesn’t require a scripting addition) and also works for negative numbers.

set myNumberOfPages to rndUp(myNumberOfFrames / myFramesPerPage)

-- Or, in a 'tell' block:

tell application "Blah blah blah"
  -- Do some application stuff
  
  set myNumberOfPages to my rndUp(myNumberOfFrames / myFramesPerPage) -- NB. 'my'
  
  -- More application stuff
end tell

I don’t understand what you’re saying about rounding up numbers “instead of to the nearest whole number within 0.5.” My handlers are downloadable from the ScriptBuilders section of this site:

http://files.macscripter.net/scriptbuilders/ScriptTools/aRounderRound.sit

If you think some of them may be useful to you, but you’re still not sure about their use, I’ll be happy to help you out (though I may be away for a few days from tomorrow (Monday)).

Hi, Camelot.

There’s a problem with this. On systems earlier than Panther, ‘as integer’ errors if the number being coerced isn’t a whole number. In Panther itself, I’m told that ‘as integer’ rounds to the nearest whole number (IEEE convention) - which may be either up or down. Your suggestion will work for positive numbers (only) if you replace ‘as integer’ with ‘div 1’.

Thanks for the quick response guys. For the future I’m running 10.3.2 and InDesign CS.

The Bad News… Hey Camelot I tried running that section of script like you did and it did return the proper result:


    set myNumberOfFrames to (count of items of myFiles) --->36 
    set myNumberOfRows to 4 
    set myNumberOfColumns to 4 
    set myFramesPerPage to (myNumberOfRows * myNumberOfColumns) --->16 
    set myNumberOfPages to round (myNumberOfFrames / myFramesPerPage) rounding up --->36/16=2.25 --->3 - works!

However, as soon as I wrapped it within a tell block for InDesign CS:


tell application "InDesign CS"

    set myNumberOfFrames to (count of items of myFiles) --->36 
    set myNumberOfRows to 4 
    set myNumberOfColumns to 4 
    set myFramesPerPage to (myNumberOfRows * myNumberOfColumns) --->16 
    set myNumberOfPages to round (myNumberOfFrames / myFramesPerPage) rounding up --->36/16=2.25 --->2 - doesn't work?

end tell

Kind of odd, I imagine something in InDesign CS is conflicting, since the base of the script apparently worked with InDesign 2.0.2. I had to make other language changes as well since the dictionary changed from 2.0.2 to CS. Even adding the integer instruction didn’t work. :frowning:

The Good News… Nigel your RndUp handler worked beautifully! I had to incorporate my RndUp since it was in a tell block. Question, handlers need to exist outside any tell block and must be before any code that uses them, correct? Here is what I ended up with:


on rndUp(n)
	tell n div 1
		if it < n then return it + 1
		it
	end tell
end rndUp

tell application "InDesign CS"
	code...	
		code...
		set myNumberOfPages to my rndUp(myNumberOfFrames / myFramesPerPage)
		more code...
	more code...
end tell

I had already downloaded the handlers before, but I didn’t know how to use them. I took a 1-day seminar form TECSoft about 4 years ago, and also had the Danny Goodman guidebook, but much of it had left my memory as I haven’t used it in years. Luckily things were somewhat still fresh in my mind, and by tackling all the scripts nowadays (due to original script developers now gone) I had to pick up more complex script writing.

With this forum and the InDesign Scripting forum I really made headway. Special thanks to all those that answered and offered help.

It looks as though “InDesign CS” may have a built-in ‘round’ function that doesn’t round up.

There are some rare instances when a handler has to come before the part of the script that uses it, but usually it can be anywhere in the script - except in a ‘tell’ block.

Handlers ‘belong’ to the script itself. If you put a call to one in a ‘tell’ block, the ‘told’ item tries to interpret that call as one of its own built-in scripting instructions, which it usually can’t do. By placing ‘my’ before the call, the handler call is interpreted by the script rather than the target of the ‘tell’ block. (‘Me’ is the script.) Similarly, I can’t test this for myself, but you may have some success with ‘round’ in the InDesign block if you precede it with ‘my’.

tell application "InDesign CS"

  set myNumberOfPages to my (round (myNumberOfFrames / myFramesPerPage) rounding up)

end tell

This might make InDesign ignore the ‘round’ command. Generally, though, unless it’s going to make your script very untidy, it’s good practice not to put anything in a ‘tell’ block that doesn’t need to be there.


tell application "InDesign CS"
	
	set myNumberOfFrames to 36 -- (count of items of myFiles) --->36 
	set myNumberOfRows to 4
	set myNumberOfColumns to 4
	set myFramesPerPage to (myNumberOfRows * myNumberOfColumns) --->16 
	set myNumberOfPages to my (round (myNumberOfFrames / myFramesPerPage) rounding up) --->36/16=2.25 --->2 - doesn't work? 
	
end tell

Adding my didn’t work either. Not a problem though since your RndUp handler worked well :slight_smile: