Register

Macro Tutorial - adding an Action to a Slide's Event

This forum allows users to share macros created with ViewletBuilder7 Enterprise and above.
Site Admin
User avatar
Posts: 51
Joined: Sat May 27, 2006 1:27 am
Location: Dolny Kubin, Slovakia

Macro Tutorial - adding an Action to a Slide's Event

Postby Rado » Thu Nov 13, 2014 12:06 pm

Requires: ViewletBuilder Enterprise 8.0 or higher

Since none of the stock macros nor those posted on the forum manipulate Question Banks or Events/Actions, the macro below should help you get started with these. Unlike the others, this is not a macro that you could use to perform some actual, useful task. It should serve as the starting point for your own macro experiments. Its code is documented with inline comments for clarity, and demonstrates how to iterate all slides in a project - the main timeline slides, master slides, as well as those inside Question Banks. For those, it will also attempt to locate the slides' events in order to add an action. The code includes 2 commented lines that add a "Goto Slide" action to the slides' "OnCorrect" event. Those lines are commented out in order to avoid unintentionally modifying your project - make sure you uncomment them to actually add the action.

To start experimenting, just copy the code below into a plain-text file named QuesionBank.groovy, and save it into your Library/Macros folder. Then load the macro using Edit menu - Load Macro command inside ViewletBuilder8 Enterprise. You can use the Edit menu - Run Macro command to execute a macro, or simply press CTRL+K.

A word of warning: macro code is not constrained by the "foolproofness" of the GUI and can break your project if used incorrectly. There are no checks made for the validity of IDs that you use, or actions that you add to a specific element. Please keep this in mind and keep a backup copy of your projects before running any macros. Macros are written in the Groovy language. While it is not necessary to have previous Groovy experience (its syntax is similar to Java, C++ or JavaScript) it certainly helps if you have programming skills before attempting to write your own macros.

Code: Select all
import com.qarbon.shasta.project.*;
import com.qarbon.shasta.action.*;

log.debug "Question Bank Event/Action Demonstration Macro"

// get the main timeline slide sequence and log the slide count
def slides = sequence.getChildren()
log.debug "Main Sequence Slides: " + slides.size()

// get the master slide sequence and log the slide count
def masters = project.getMasterSequence().getChildren()
log.debug "Masters: " + masters.size()

// get the question banks list and log the count
def banks = project.getQuestionBanks()
log.debug "Question Banks: " + banks.size()

// list the main timeline slides and their names/IDs
log.debug "Listing Slides:"
for(def slide in slides)
{
   log.debug " - Slide " + slide.name + " ID: " + slide.getID()
}

// list the master slides and their names
log.debug "Listing Master Slides:"
for(def master in masters)
{
   log.debug " - Master Slide " + master.name
}

// list the question banks
log.debug "Listing Question Banks:"
for(def bank in banks)
{
   log.debug " - Question Bank " + bank.name
   
   // iterate over the slides inside the question banks
   for(def slide in bank.getChildren())
   {
      log.debug "   - Bank Slide " + slide.name + " ID: " + slide.getID()
      
      // if you were adding an action to slide's onLoad and onUnload events, you could do it here:
      // slide.onLoadEvent.actions.add(someAction)
      
      // however, adding to slide's onCorrect/onIncorrect events requires you to get the interactive zone
      // object reference first (all question bank slides should have one)
      def children = slide.findChildrenOfType(QInteractiveZone.class);
      if(children.size() == 1)
      {
         // if an interactive zone was found, add the gotoslide action to its oncorrect event
         log.debug "   - interactive zone found, possible to use its onCorrect/onIncorrect events"

         // set the GotoSlide action objects slide ID to "1" - update to fit your needs
         def action = new QGotoSlideAction()
         action.setSlideID(1)

         // add the action to interactive zone's onCorrect action - uncomment the following 2 lines
         //def interactive = children[0]
         //interactive.onCorrectEvent.actions.add(action)
      }
   }
}

Return to Macros

Who is online

Users browsing this forum: No registered users and 1 guest