Debugging OpusScript

OpusScript is a full programming language that allows you to add extra functionality to your publication. However, when you start writing anything but the most simple program structures you will quickly begin to find you sometimes get unexpected results. A piece of script or a function quite often does not work how you expected it to or does not work at all. This will be especially true if you are just beginning to dabble in programming or if you have written a complex piece of code.

These unexpected results are called "bugs" and the process of finding and curing them is called "debugging".

Note:

To track the values of variables*pop_VariableDef and expressions, Opus provides a function called Debug.trace. This allows you to display the value of a variable in a separate window (the Script Console) as your publication is running.

As you use the publication, you can see what a value is at a certain point. This often gives you an excellent clue as to why your program isn’t working as you expected. Opus does not provide a separate debugger tool, as many large programming environments do.

Helpful things to consider:

  1. Case Sensitivity – the simplest error when creating program scripts using OpusScript is that part of the script is not spelt correctly or has been capitalised incorrectly. OpusScript is case sensitive, that is myImage is NOT the same as MyImage or myimage. So the first thing to do when you begin debugging is to check for typing errors.

  2. The Script tab in the Page Organiser lists all of the OpusScript functions for you. You can double-click a function from the list and it will automatically be inserted at the point where the I-beam is flashing in a script – this will help to reduce the chance of misspelling OpusScript functions.

Note:
There is also an Auto Complete feature in OpusScript. Type a partial name of an OpusScript function, then press Ctrl + Space and the function name will be completed. If there is more than one possible function name, you will be given a list to choose from, simply select appropriate function name.

  1. Furthermore, there is an Auto Capitalise feature in OpusScript, if you type the name of an OpusScript function, it will automatically correct the case. For example, if you type button1.show(), it will be corrected to button1.Show().

  2. Variables – another cause of problems can be variables*pop_VariableDef having the wrong information when they are used. If you don’t give a variable a specific value at a specific time or you don’t reset the variable after it has been used you might find it still contains a value which (when you come to add something or do something with it) is not the right value.

  3. Another problem with variables is what is called "scope". If a variable is declared using the var operation within a function it is only available to code within that function and not to the rest of your script. Think of a function like a black box – whatever is in there is a mystery to the rest of your script – all you can do is run the function and then work on the result it provides you with. Thus you might find that you are trying to use a variable in another function that doesn’t exist because it has been declared (i.e. the var operator has been used) in a different function.

  4. Counting – it is standard practice in programming to start counting from zero. For the layman or the newcomer to programming, this can be confusing, as you can end up with loops going for one too many times or accessing elements in an Array*pop_defArray you did not intend.

  5. Pathnames – a common error in OpusScript is to include pathnames*pop_defPathname with only one backslash. This will result in an error message Unable to find file… followed by a pathname with no backslash characters, for example, c:windowstempmyfile.rtf If you wish to include backslash characters in literal strings in OpusScript – for example in pathnames such as c:\windows\temp\myfile.rtf – then, in common with the ECMA standard, you need to use double backslash characters i.e. c:\\windows\\temp\\myfile.rtf

  6. Operator Precedence – another cause of problems is sometimes caused by an expression not quite being calculated the way you intended because of the way arithmetical operators are performed in a particular order as explained in Operators in the OpusScript Help file.

Related Topics:

Overview of OpusScript