Tom L. Hall

President

 

TCC Publishing Newsletter

 

Issue 10

 

Sept 15, 2007

 

 


This Newsletter comes quickly after Issue 9 but someone trying to do a lesson in my Actions Editor, Simulations, ActiveX, and HTTP Post book has stimulated me to do some work with Microsoft Agents in ToolBook this week.

 

In this Issue:

 

Background

Getting Started

How Does It Work?

Demo of MSAgents in ToolBook


Background

 

For several versions of my Actions Editor book, I have covered how to make the Microsoft Agents work in Native ToolBook applications as an example of ActiveX in ToolBook. For those of you not familiar with this, the Microsoft Agents are Merlin, Peedy, Genie, and Robby (a magician, a bird, a genie, and a robot) and are characters that can be animated using a defined group of commands as well as speak text.  There is a setup that is needed but once you install what is needed, you are all set.  In the book, I have an extensive set of lessons on making this work but it requires a lot of OpenScript and is a bit painful.  Likewise, a long time back, I used a small routine provided by Cecil Cheah (so where is he these days for those of you who remember?) that used a Display URL action to call JavaScript functions to then have an HTML Product where the Agents would work.  This was in the days prior to us having the ability to add JavaScript to ToolBook and then use the Actions Editor to execute the functions we might find in the js file.  Back to the “old” way. It was a bit painful to also have the ability to use the Agents in a ToolBook application to be converted to HTML.  This method also forced me to provide lessons to show the developer how to insert the code needed to hook the HTML product up to the old JavaScript file. Likewise, it was necessary to get the ID of the control that has been added to the page to make this work and then do an edit to the JavaScript file.  The last part is still needed but the rest if fairly painless. So much so, that I have an interest in this again and would like to use it as a good example of how we can easily incorporate JavaScript into our ToolBook applications that we deploy to the Web.

 

top


Getting Started

 

For convenience, I have updated a Web page to provide links to where you can download and install the components needed to make this work. Just follow the instructions on this page and you will be all set.

 

http://tcc-pub.com/agents/agents.htm

 

Basically, you install the core components, one or more characters, the text to speech engine, and perhaps an additional component to make sure that the text to speech engine works.  Likewise, there are lots more characters out there and it would be easy to use them once you get going with one of the Microsoft Four.

 

 

top


How Does It Work?

 

After you have installed what is needed, you will need to download a sample file, just to make sure you get going properly. It will still be up to you to make the post-Export edit that is needed.

 

  1. First, click here to download the tbk file. Unzip it to a location on your hard drive. Note that this is a ToolBook Instructor 9 file but the basic steps I will outline will work with a file you can create from scratch using ToolBook 2004 if you have not upgraded yet.  The file is Agents3.tbk.  This exercise uses Merlin.

 

  1. Next, download the JavaScript file by clicking here.  Note that this is a text file and when you do a SaveAs from the Browser window, just save it as agents3.js and save it to the same folder as the Tbk file.

 

  1. If you want to start from scratch, such as a user of ToolBook 2004, then create a book using the Default Template and add some title to the first page.  Then add a new page to be used for the exercise.  Add a third page as a Summary page just to assure that when we leave the agent page, we hide the agent.

 

  1. Use the Object, Properties for Book command and go to the Web tab.  Click Import to import the JavaScript file.  Notice the functions.  If you have opened my file, you will likely have to do this since your files will be in a different location than mine. Exit this dialog box when done.

 

 

  1. Now, use the File, Extensions command to locate and add the Microsoft Agent Control 2.0 ActiveX control.

 

 

  1. Now, go to Page 2 and use the Tool Palette to draw this control on the page.

 

  1. On Page 2, draw a button somewhere. Set the Caption to Load Agent.  Set up the On load page event as shown here. Then go to the On click event and add the Execute Script action.  From a dropdown list, select the WinLoad function. This will load and initialize the character based upon what is found in the JavaScript file but will only do it once and only for this page where the control is located.  Hide this button when done.

 

 

 

  1. Now, draw a button on the page and set the Caption to Summon Merlin. Create the remaining buttons as shown below.  Add a field to the right and name it input. Make sure to uncheck activate scripts for this field. Finally add the instruction text.

 

 

  1. Now add this action to the On click event for the first button (Summon Merlin).  Once we look at the showAgent function, we will see that we also have him speak once visible.  You should notice that all the functions are available from the dropdown list.

 

 

  1.  Add this to the Move and Talk button.

 

 

  1.  Add this to the Do Magic button.

 

 

  1.  Add this to the Dismiss Merlin button. Also add the On unload page action to make sure he is hidden if the user leaves the page without clicking this button.

 

 

 

  1.  Now, let’s go back to the Get and Read Text from Field button since this one is a bit more complicated, but shows the true power of what we can do with a JavaScript function, since we can pass parameters out to a function and have something happen as a result.  Add this code to the button.

 

 

  1.   Now, doubleclick on the beginning of the Execute Script line to obtain the Properties dialog box.  You will notice that there is one parameter defined in the JavaScript file for this function: readme.  Enter the value as shown here: text of field “input”

 

 

  1.  That’s it. The book is ready to be published.  You can go ahead and do that now.

 

 

Before we do anything else, let’s look at the agents3.js file that you downloaded and saved previously.  The top part of this code will not be touched by you except for a simple substitution of a number once we know what the ID of the agent control is. The main thing is to look at the tbfunctions.  This is how we designate a function to be identified by ToolBook:

 

function tbfunction_WinLoad() {

          document.sys126.Connected = true;

          MerlinLoaded = LoadLocalAgent(MerlinID, MerlinACS);

          if (MerlinLoaded) {

                   SetCharObj();

          }

          CheckLoadStatus();

}

 

function tbfunction_showAgent(){

          InitAgentCommands();

          Merlin.MoveTo(0, 0);

          Merlin.Show();

          Merlin.Speak("Hello, my name is Merlin.");

}

 

function tbfunction_moveToAgent(){

          GetScreenPositions();

          InitAgentCommands();

          Merlin.Show();

          Merlin.MoveTo(MerlinCenterX , MerlinCenterY );

          Merlin.Speak("Did you see me move?");

}

 

 

function tbfunction_ReadInput(readme){

          var texttoread=readme

          GetScreenPositions();

          InitAgentCommands();

          Merlin.Show();

          Merlin.Speak(texttoread);

}

 

function tbfunction_agentdomagic() {

          InitAgentCommands();

          Merlin.Show();

          Merlin.Play("DoMagic2");

}

 

function tbfunction_agenthide() {

          InitAgentCommands();

          Merlin.Hide();

}

 

The first function is the one we call on loading the page.  The others are the ones called by the on click events of the buttons.  Hopefully you can easily see what you can do to have Merlin speak more text. Take a look at the Merlin.Speak(“Hello, my name is Merlin.”) line.

 

The Do Magic button calls the function that has a Merlin.Play(“DoMagic2”) line.  More on where this comes (the DoMagic2) from later.

 

As shown above, the button to have Merlin get the text from the input field and speak it is a bit more complicated. Here we pass out whatever is typed as a parameter named readme, set a variable equal to that, and then have Merlin speak the contents of the variable (texttoRead).  Actually, pretty simple isn’t it?  As suggested by the person who started me down this road by asking about the original lesson in the book, you could have the student type something and then Merlin would speak it. Likewise, you could have Merlin read the text of a Question and the answers to choose from.  There are all sorts of possibilities here.  I personally like the idea of passing the text to read as a parameter each time as this avoids having to have a whole bunch of functions that are hardcoded to speak various strings of text.  You would still need to write other functions for the different Play actions.  If you want Merlin to appear on other pages, a better technique would be to put the control on a common background and then get the ID as described below form the JavaScript file for the background, often either b0.js or b1.js.  The main thing is to come up with a way where you only load the Agent once for the whole book and I suggest this be the first time you need the Agent on a page. This is critical to avoid errors when returning to a page with the Agent.

 

Once you get going, you have to find out what each character can do and this is a table of the Play actions I came across back when I wrote the lessons I first did.  In addition to these Play actions, I needed to know about Show, Hide, MoveTo, and Speak.  I got a lot of my information from a MASH program that is used to generate scripts for incorporation directly into Web Pages.  A link to this is on the http://tcc-pub.com/agents/agents.htm page.

 

 

Acknowledge

Alert

Announce

Blink

Confused

Congratulate

Congratulate_2

Decline

DoMagic1

DoMagic2

DontRecognize

Explain

GestureDown

GestureLeft

GestureRight

GestureUp

GetAttention

GetAttentionReturn

Greet

Hearing_1

Hearing_2

Hearing_3

Hearing_4

Hide

Idle1_1

Idle1_2

Idle1_3

Idle1_4

Idle2_1

Idle2_2

Idle3_1

Idle3_2

LookDown

LookDownBlink

LookDownReturn

LookLeft

LookLeftBlink

LookLeftReturn

LookRight LookRightBlink

LookRightReturn

LookUp

LookUpBlink

LookUpReturn

MoveDown

MoveLeft

MoveRight

MoveUp

Pleased

Process

Processing

Read

ReadContinued

Reading

ReadReturn

RestPose

Sad

Search

Searching

StartListening StopListening Suggest

Surprised

Think

Thinking

Uncertain

Wave

Write

WriteContinued

WriteReturn

Writing

 

Now, there is one final, very important task. We have to make an edit to the Javascript file, but before we do, we need to know something. 

 

  1. Take a look at the Page Properties dialog box for the page with the Agent and make a mental note of the Page ID. In my case, it is 5.

 

  1. If you have exported the book, go to the IE5 folder and locate the JavaScript file that is associated with this page. In my case, it will be p5.js. Open this file in Notepad. Find a line that looks like this:

 

p5.o0=new _ah("p5.o0","sys114",true,true,false,"","",p5,"sys115","<span id=\"sys114\" style=\"height:33px;width:33px;visibility:inherit;z-index:32768;position:absolute;left:689px;top:427px;\"><object classid=\"clsid:D45FD31B-5C6E-11D1-9EC1-00C04FD7081F\" id=\"sys115\" width=\"100%\" height=\"100%\"><param name=\"mercury_id\" value=\"p5.o0\"><param name=\"Connected\" value=\"false\"><param name=\"RaiseRequestErrors\" value=\"true\"></object></span>");

 

  1. You will see a “clsid: D45FD31B-5C6E-11D1-9EC1-00C04FD7081F\" part and we are looking for the id number following that: sys115 in this case.  Make a mental note of this.  In thinking about this a little, perhaps we can’t keep the agent open across pages and have it work, but this might be only necessary to initialize him.

 

  1. Now, locate the 1.js file (yes, that is what agents3.js is renamed to when copied to the media folder) in the media folder and open it with Notepad.  You will notice a number of occurrance of this: document.sys126.  Just do a search and replace, replacing sys126 with sys115 or whatever number was assigned to the control in your book upon export. 

 

  1. That’s it!  It should now work.

 

 

top


Demo of MSAgents in ToolBook

 

Note: It has come to my attention as one or more of the first readers of this Newsletter try this, issues are coming up and it’s worthy to discuss exactly what the student will encounter and be reminded that it works only with IE.

 

First, there likely will be an ActiveX message such as this:

 

 

I found that click X and closing this message allowed the Agent to work even though the message said it has been blocked.  This information bar can be turned off but it really is not recommended. Since this only happens once, I would suggest that you just inform your students that they will get this message but to ignore it.

  1. Open Internet Explorer.
  2. On the Tools menu, point to Pop-up Blocker, and then click Pop-up Blocker Settings.
  3. Clear the Show Information Bar when a pop-up is blocked check box.

 

A more significant issue is the voice not being heard and I have to be honest and say this first happened to me.  I had installed the core components, the agent, and the L&H TrueVoice speech engine but only saw the bubble with the words with no sound. I went back and installed the SAPI 4 component and it worked.  To make sure your student machines or end-user machines can handle this, I would provide a link to a site like this:

 

http://chemware.co.nz/AgentSupport.htm

 

or create your own Setup page where you tell the end-user what is needed if this does not work. It is likely that most of the components needed will be there already but this is an important note found on the above site:

 

Windows 2000 includes Microsoft TTS (synthetic speech engine) and SAPI 4 runtime. The SAPI 4 is needed to run TTS engines other than TruVoice.

 

Click here to see this work.

 

 

top


 

 

TCC Publishing, Inc

2055 Cherry Stone Lane

Greenville, NC 27858 USA

Internet Address: tomhall1@tcc-pub.com

http://tcc-pub.com

Phone: 252-758-4590

Fax: 252-758-4590