ANIMATIONENCYCLOPEDIA SAMPLE SCRIPT

In our previous blog post we started looking at a script “AnimationEncyclopedia” which is part of the Sample scripts provided by Adobe. This is a fairly lengthy script, but not unreasonably complex, especially if you pick it apart piece by piece. And that is what we attempted to do, up to the point where the script creates a second document page.

Script Problem

In running the script, we encountered a potential error: “Data is out of range.” The problem is simply because we prefer to set the measurement unit for script preferences rather than setting the measurements for the document itself. If you look at the sample script, you will see multiple calls to a handler mySetUnits. This sets the measurement units of the document to either inches or points depending on the value passed as its argument. Should the measurement unit for script preferences be set, you may encounter the error. The fix is to set the measurement unit for script preferences to auto value:

    tell application "Adobe InDesign CC 2017"
	  set measurement unit of script preferences to auto value
    end tell

So now that the script is working, let’s turn our attention to how it creates its magic:

–Page 2

On this page, all rectangles on the page are assigned the Motion Preset “twirl”. For each rectangle the animation is triggered by a different event. The animation for the first four are part of the timing settings for the spread.

The hierarchy for setting up timing settings can be a little confusing. With the exception of buttons, the timing settings for the spread serves as the parent object. Inside the timing settings, there can be timing lists which contain timing groups, and timing settings for individual page objects (for self rollover and self click events).

Spread Timing Settings

If you scroll down the script to the section commented as –page 2 timing section, you will see the timing settings for spread 2 (myPageTwoTimingSettings) creates two timing lists: one for On Page Load and one for On Page Click. Each of these creates a timing group that contains only one page item.

  • “On Page Load Rectangle” – as member of secondSpreadTimingG1 timing group as part of mySecondPageLoadTimingList and triggered by on page load event
  • “On page Click Rectangle” – as member of secondSpreadTimingG2 timing group it is part of mySecondSpreadClickTimingList and triggered by on page click event

Next, the timing settings for individual page items create a timing list that has only one property: a trigger event.

  • “On Self Click Rectangle” – given the trigger event on self click, it is defined in myRectangle3P2TimingList as part of the timing settings for the rectangle
  • “On Self Rollover Rectangle” – given the trigger event on self rollover, it is defined in myRectangle4P2TimingList as part of the timing settings for the rectangle

Buttons

Buttons are independent of the spread’s timing settings as they are triggered directly by some action from the user. The animation for the “Button Click Rectangle” (myRectangle5p2) is triggered when the Play Animation Button is clicked.

    Animation behavior for a button can have any of the following properties (properties for ExtendScript are in parentheses):
  • Animated page item (animatedPageItem) – the page item that responds to the button event
  • Auto reverse on rolloff (autoReverseOnRollOff) – behavior reverses when rolled off
  • Enable behavior (enableBehavior) – true or false
  • Behavior event (behaviorEvent) – mouse down, mouse enter, mouse exit, mouse up (MOUSE_DOWN, MOUSE_ENTER, MOUSE_EXIT, MOUSE_UP)
  • Operation – pause, play, resume, reverse_playback, stop, stop all (PAUSE, PLAY, RESUME, REVERSE_PLAYBACK, STOP, STOP_ALL)

The properties for the animation behavior of this button (myButton) can be written as follows:

AppleScript:

    tell myButton
       make animation behavior with properties ¬
{behavior event:mouse down, enable behavior: true, animated page item:myRectangle5p2,auto reverse on roll off: false, operation: play}
    end tell

JavaScript:

var myPlayAnimationButton = myDocument.pages.item(1).buttons.add({geometricBounds:[5.625,3,6,4], name:"Play Animation Button", fillColor: myGreen, strokeColor: myNoneSwatch});
	var myAnimationBehavior = myPlayAnimationButton.animationBehaviors.add({behaviorEvent:BehaviorEvents.mouseDown, enableBehavior:true, animatedPageItem:myRectangle5P2, autoReverseOnRollOff:false, operation:AnimationPlayOperations.play});

Page 2 animations…Page 2

MULTI-STATE OBJECTS

A multi-state object is a special type of content holder that can display more than one image or text. When the object is created programmatically, it has two states by default. You can add any number of additional states. For each state you need to define its properties (content, color, shape, etc.). You can set up buttons to enable the user to advance through the states.

Simple MSO Script

Most commonly, a script is used to populate a selected rectangle with images found in a pre-defined folder. The following assumes there is a selected page item (rectangle). Images are sized and saved in a folder to be chosen by the user.

  set imagePath to (choose folder with prompt "Select folder of images for MSO") as string
  set fileNames to list folder imagePath without invisibles
   tell application "Adobe InDesign CC 2017"
     set measurement unit of script preferences to points
	set rectRef to item 1 of selection
	set imageBounds to geometric bounds of rectRef
	tell document 1
		set layerRef to layer 1
		set myMSO to make multi state object with properties ¬
               {name:"MyMSO", geometric bounds:imageBounds, item layer:layerRef}
                repeat ((length of fileNames) - 2) times
		   tell myMSO to make state --adds states to accommodate files
               end repeat
	end tell
	repeat with i from 1 to length of fileNames
		set fileAlias to (imagePath & (item i of fileNames)) as alias
		tell state i of myMSO
			if i > 1 then
				set rectRef to make rectangle with properties {geometric bounds:imageBounds}
			end if
			tell rectRef to place fileAlias
		end tell
	end repeat
end tell 

…Simple Multi-State Object

Sample MSO

The Sample script takes a little different approach. Scroll down to where the script is commented –On MSO State Load section. Here the multi state object is created with initially hidden set to true. For each state, a rectangle is created with motion preset for the animation settings set to the value of myMotionPreset (twirl). The rectangle for each state is set to a different color. Two of the three rectangles are converted to a different shape. Notice the slightly different approach for creating the MSO object.

set myMotionPreset to Motion Preset "twirl"
   tell application "Adobe InDesign CC 2017"
      tell document 1
         tell page 2
            set myMSO to make multi state object with properties ¬
           {name:"My Simple MSO", geometric bounds:{6.625, 1, 7, 2}, initially hidden:true}
        end tell
        tell myMSO 
           make state --adds state to make total of 3
           --then for each state a rectangle is created using the myMakeRetangle() handler; second state only shown below
           tell state 2
             set myOvalMSO to myMakeRectangle¬
            (state 2 of myMSO, {6.625, 1, 7, 2}, "Second State Oval", "Purple", "None", 0)
             tell myOvalMSO
               convert shape given convert to oval
            end tell
         end tell 
        ...

All that is left is to create a button that will advance through the MSO states. Both examples below assume that the document has a color swatch named “Green.”

Simple MSO Button

Add the following before the last end tell in our Simple MSO script above:

   copy imageBounds to {y0, x0, y1, x1}
   set buttonBounds to {y1 + 12, x1 - 96, y1 + 48, x1}
   tell document 1
      set myPlayMSOButton to make button with properties ¬
{geometric bounds:buttonBounds, fill color:"Green", stroke color:"Green", name:"Go to Next State"}
   end tell
   tell myPlayMSOButton
      set myMSOBehavior2 to make goto next state behavior with properties ¬
{associated multi state object:myMSO, loops to next or previous:true, behavior event:mouse down}
   end tell

Sample MSO Button

The Sample script creates its button similarly, only using fixed values for the button’s geometric bounds.

   tell page 2
      set myPlayMSOButton to make button with properties ¬
{geometric bounds: {6.625, 3, 7, 4},fill color:"Green", stroke color:"Green", name:"Go to Next State of MSO Button"}
   end tell
   --and link it to the MSO
   tell myPlayMSOButton
      set myMSOBehavior2 to make goto next state behavior with properties ¬{associated multi state object:myMSO, loops to next or previous:true, behavior event:mouse down}
   end tell

Of course you can always pick a button from InDesign’s Button library. We will leave this to a discussion on using libraries.

ONWARD AND UPWARD

The animations for pages 3 and 4 created by the Sample script (AnimationEncyclopedia) are fairly straightforward. For Page 3, these will include (duration and easing, looping, hiding, and setting animation origin. Page 4 will work with Animate To and From, and using the Motion Presets “move-right-grow” and “fly-in-left.” Until then, make sure you are familiar with the basic animations and using timing settings. Try creating a document with an image carousel using an MSO object. Have fun!

Disclaimer

Scripts are provided as example code from which users can build their own scripts. No representation is made for their completeness or usability.