Scripting

jAlbum is equipped with BeanShell, a very powerful Java-like scripting language. Scripting gives you the ability to add any imaginable features to generated albums. Within special <% scriptlets %> you can construct just about any expression you like that is also valid Java. Scriptlets can be embedded into the template files of jAlbum skins (index.htt and slide.htt) or inside style sheet files of skins.

Naturally, within BeanShell <% scriptlets %> you enter comments as //This is a comment. You cannot use the jAlbum comment tag <%--This is a comment--%>.

Here are some examples. Copy and paste them into a skin to have them executed during album creation.

Ordinary calculations: <%= 5*(3+2) %> --> 25

Access variables too: <%= thumbWidth*2 %>

Warn for large images and use variables in strings easily:
<%
  if (fileSize > 1500000)
    out.print("Original file is large: " + fileSize + " bytes.");
%>

Produce fast Google-like numbered links to other index pages:
<%
  if (totalIndexes == void) return;  // totalIndexes only exist if several index pages
  for (i=1; i<=totalIndexes; i++) {
    if (i == indexNum) out.print(" <b>" + i + "</b>");  // Emphasize current index page
    else {
      out.print("<a href=\"" + engine.getIndexPageName() + (i==1?"":i)
       + engine.getPageExtension() + "\">" + " " + i + "</a>");
    }
  }
%>

Scripting can be performed in four syntaxes:

Ordinary scriptlet: <% ... %>
Value of this scriptlet is outputted to the page: <%= ... %>
Old syntax for the above: <eval> ... </eval>
Function definition scriptlet (only evaluated once): <%! ... %>

Use scripting to construct advanced skins containing cool dynamic image borders etc. You have access to the full power and class hierarchy of java + there are also a number of practical "implicit objects" to assist you.

Void and null variable values

Some variables only exist in certain circumstances. The nextPage variable is one such variable. It doesn't exist for the last page of a slide show. If you need to refer to such a variable in a script, you must ensure that it exists first, otherwise a scripting error will occur. Here are two ways to ensure that you don't refer to a nonexisting variable:

<ja:if exists="nextPage">
    <% // here it's safe to refer to nextPage %>
</ja:if>
or
<%
    if (nextPage != void) {
        // here it's safe to refer to nextPage
    }
%>

As seen by the example, one uses the "void" keyword within a script to test if a variable exists (is declared). This is one of the few examples where BeanShell scripting differs from Java. Java is a compiled language, BeanShell is interpreted when executed. In Java, a reference to a nonexistent variable will cause the compiler to halt. In BeanShell such references can happen in run-time, hence the need for the "void" keyword. "Void" should not be confused with "null". Null means that a variable exists, but has no value. Java programmers know the importance of doing null tests before refering dereferencing an object that might be null. The same applies to variables that may be void.

First test for void, then null and finally do any other validation or comparison.