Are You Having Fun Yet?

Even though we have just covered some of the basics of using a script to animate InDesign page items for a fixed layout EPUB, hopefully you have found some time to play and have some fun creating an animated page or two.

This week we will expand on the subroutines (handlers) that have been developed so far which involve moving an item either horizontally or vertically, rotating, scaling, and setting opacity. The parameters for the handlers are set as part of a script written in AppleScript editor. When the script is run from within the editor, the animation properties for the page items are set. These settings, by default, will animate the object when the EPUB page is opened. To reference the items to be animated, the objects can either be selected and named using a script or named using InDesign’s Layers panel.

Naming page items in Layers panel

As part of last week’s discussion, the reader was given two challenge projects to reinforce the concepts covered.

Challenge 1: Heart Project

The heart project involved animating two page items: the heart illustration, and a text frame. If the heart illustration is an imported image, it can be referenced in the script by its container. Otherwise, if drawn on the InDesign page, it would be referenced as a polygon. The items for our sample project were referenced using a name assigned in the Layer panel. For demonstration the image was named “Heart” and the text frame named “Message.”

With this, the script was written as follows:

tell application "Adobe InDesign CC 2014"
	set pageRef to page 1 of document 1
	set heartObj to rectangle "Heart" of pageRef
	set messageObj to text frame "Message" of pageRef
	set hScale to {{0, 100.0}, {23, 150.0}, {47, 100.0}}
	set vScale to {{0, 100.0}, {23, 150.0}, {47, 100.0}}
	set dTime to 2.0 --duration of animation
	set numTimes to 3 --times to repeat
	set tOffsets to {0.5, 0.5} --pin point for animation
	my setScaleProps(heartObj, hScale, vScale, dTime, numTimes, tOffsets)
	set oArray to {{0, 0.0}, {23, 50.0}, {47, 100.0}} --text frame opacity array
	set dOption to from current appearance
	my setOpacityProps(messageObj, oArray, 2, dOption, true)
end tell
--requires handlers setScaleProps and setOpacityProps 
(*Scales object given horizontal and vertical lists, duration, and repeat*)
on setScaleProps(objRef, scaleX, scaleY, dTime, numTimes, tOffsets)
	tell application "Adobe InDesign CC 2014"
		tell animation settings of objRef
			set duration to dTime
			set scale x array to scaleX
			set scale y array to scaleY
			set plays to numTimes
			set transform offsets to tOffsets
		end tell
	end tell
end setScaleProps
(*Sets opacity for object referenced given array of timeframe and opacity.*)
on setOpacityProps(objRef, oArray, dTime, dOption, doEase)
	tell application "Adobe InDesign CC 2014"
		tell animation settings of objRef
			set duration to dTime
			set design option to dOption
			set opacity array to oArray
			if doEase is false then
				set ease type to no ease
			end if
		end tell
	end tell
end setOpacityProps 

Challenge 2: Airplane

The script for this challenge project was fairly easy as it involved only one object, an image of an airplane. The purpose of this project was to demonstrate how transform offsets could be used to have one object rotate around another. The script for this project was written as follows:

tell application "Adobe InDesign CC 2014"
	set spreadRef to spread 1 of document 1
	set objRef to rectangle "airplane" of spreadRef
	set rArray to {{0, 0.0}, {23, 360.0}}
	set dTime to 1.0
	set numTimes to 3
	set tOffsets to {0.5, 1.0}
	my setRotateProps(objRef, rArray, dTime, numTimes, tOffsets)
end tell

(*Rotates object given rotation array, duration, and repeat*)
on setRotateProps(objRef, rArray, dTime, numTimes, tOffsets)
	tell application "Adobe InDesign CC 2014"
		tell animation settings of objRef
			set transform offsets to tOffsets
			set duration to dTime
			set rotation array to rArray
			set plays to numTimes
		end tell
	end tell
end setRotateProps 

Exporting to EPub

To this point we have verified the animations created by our scripts using the EPUB Interactivity Preview panel (option+shift+return). This panel gives you an good indication to how the animation will work when the run button (lower left corner) is clicked.

 Heart project previewed in panel

Once you think the animation works as desired, you will want to export the page (or pages) to verify how the animation will display on the intended device. The keyboard shortcut for export is Command+E. After, you will need to set the export format to EPUB (Fixed Layout).

Select EPUB (Fixed Layout) and Save

When you click Save you are then presented a series of panels for setting the export options. Suggested settings for the various panels follow:

General Panel

 Conversion Settings Panel

Metadata Panel

 Viewing Apps Panel

Challenge 1

Create a page for a fixed layout EPub that has lines of text appear one after the other similar to bullet points in a slide presentation. Hint: This will require that each line of text will need to be in a separate text frame. The script will use the setOpacityProps handler above. A problem may arise in how the text frames are referenced. If placed on the page sequentially, the objects can be selected and referenced by index from the selection list.

tell application "Adobe InDesign CC 2014"
     set selList to selection
     repeat with i from 1 to length of selList
         tell animation settings of item i of selList
              --place animation settings here
         end tell
     end repeat
end tell 

When you are satisfied with the animation, save the page to a Fixed Layout EPUB and view the animation in either iBooks or Adobe Digital Editions.

To be more resourceful, you could have your script create the text frames with the text from a list of strings or read in from a plain text file.

More Complex Animations

About now, you are probably thinking in terms of creating more complex animations for page items. To list a few ideas:

  • fly in with grow (scaling)
  • fly in with rotate
  • fly in with grow (scaling) and rotate

This could be accomplished by calling one of our handlers for each transformation one after the other. But it would be more efficient to have a handler that allows any or all animation settings to be set at once. This handler could be written as follows:

Move and Transform

(*Parameters: object, duration, travel path point, rotation array, horizontal and vertical scale arrays, opacity array, number of times to repeat*)
   on moveAndTransform(objRef, dTime, dTravel, rArray, hScale, vScale, oArray, numTimes)
    --default List for straight line path
    set mPathPoints to {{{{0.0, 0.0}, {0.0, 0.0}, {0.0, 0.0}}, {{0.0, 0.0}, {0.0, 0.0}, {0, 0.0}}}, true}
    repeat with i from 1 to 3
	set item i of item 2 of item 1 of mPathPoints to dTravel
    end repeat
    tell application "Adobe InDesign CC 2014"
	tell animation settings of objRef
	    set duration to dTime
	    set design option to to current location
	    set motion path points to mPathPoints
	        if rArray is not {{}, {}} then
	            set rotation array to rArray
		end if
		if hScale is not {{}, {}} then
		    set scale x array to hScale
		end if
		if vScale is not {{}, {}} then
		    set scale y array to vScale
		end if
		if oArray is not {{}, {}} then
		    set opacity array to oArray
		end if
		set plays to numTimes
	    end tell
	end tell
    end moveAndTransform

To set the animation properties for an object, you could then pass any one or more of the animation arguments to the handler. For example, to set the same animation properties for any number of selected page items, the top part of the script could be written as follows:

    --set up values for animation settings and call moveAndTransform 
    tell application "Adobe InDesign CC 2014"
	set selList to selection
	set dTravel to {-400, 0} --direction of travel	
	set dTime to 2 --duration
	set numTimes to 1 --number of times to repeat
	set rArray to {{0, 0.0}, {47, 360.0}} --rotation
	set hScale to {{0, 100.0}, {47, 150.0}} --horizontal scale
	set vScale to {{0, 100.0}, {47, 150.0}} --vertical scale
	set oArray to {{0, 0.0}, {47, 100.0}} --opacity
	repeat with i from 1 to length of selList
	    set objRef to item i of selList
	    my moveAndTransform(objRef, dTime, dTravel, rArray, hScale, vScale, oArray, numTimes)
	end repeat
    end tell

To test this out, create any number of page items in a document set up for Digital Publishing intent (1024 x 768). With the items selected, run the script above. Preview the result in the EPUB Interactivity Preview panel (Option+Shift+Return). If desired, export the page as a Fixed Layout EPUB following the instructions above.

Challenge 2

Try your creativity in animating a page and its items using the handlers that have been created so far. Some suggestions are:

  • Fade background in and animate a large centered title
  • Bring an image onto the page and animate labels or callouts for the image

Onward and Upward

Next week will have you working with paths to make it possible to bring an item onto the page from any angle. We will then explore various ways to create the paths for items to follow; maybe even have the item do a little happy dance. What fun!