Python scripting in NiceForm and NiceLabel Portal

When you buy NiceLabel Portal you also get the new version of NiceLabel Suite (5.2.3.4020). This version includes NiceForm that besides Visual Basic Script now also supports Python as a scripting language. This gives NiceForm and NiceLabel Portal great opportunities to create very powerful and versatile solutions.

Prerequisites

NiceForm uses Python 2.7 and PyWin32 extensions for Python that allow Python to be used in the same way VBScript is used. However these two components are not automatically installed when you install NiceLabel Suite as most users will not need them. You will have to manually install first Python 2.7 and then PyWin32 extensions. You can find them on the NiceLabel Portal CD or download them from the internet:

Python 2.7 
PyWin32 for Python 2.7

How to enable Python as scripting language in NiceForm?

You can now choose between VBScript or Python language as the scripting language that can be used in "Execute script" ("VB Script" action was renamed to "Execute script") action or as an expression for action condition. This is a new form property that you can change by double clicking the form and opening "Scripting" page in the “Form Properties” dialog box and it will be used for all actions on the form. Only one language can be used at a time.

NiceForm - Form Properties

Other changes in NiceForm 5.2.3 regarding Python scripting

Some other changes were also implemented in NiceForm in connection with Python scripting:

  • “Action Properties [Execute Script]” dialog box is now resizable.
  • Syntax highlighting for both VBScript and Python is supported.
  • “Build script …” button is not available if you use Python as a scripting language.

NiceForm - Execute Script Action Properties

Some of the differences between VBScript and Python

Variable names

If you want to use variables in scripts you have to follow some rules regarding variable names:

  • Name cannot contain spaces.
  • Name can contain lowercase or uppercase letters and digits.
  • Case is significant.
  • The following reserved words cannot be used as variable names: and, as, assert, break, class, continue, def, del, elif, else, except, exec, finally, for, from, global, if, import, in, is, lambda, not, or, pass, print, raise, return, try, while, with, yield.

This means that if you were allowed to use variable with name “Last Name” in a VBScript you will no longer be able to do that and you will have to rename it to “LastName” to use it in Python script. Please also note that Python is case sensitive and that “variable” and “Variable” are no longer the same thing (as they were in VBScript). In your Python script you have to use exactly the same name for a variable you used when you defined it.

Getting and setting variable values

One of the differences between VBScript and Python is also the way NiceForm variables are set. This is best shown on a sample code. Below you can find a piece of code that does the same thing in both VBScript and Python.

VBScript
error = ""
if Len(keypress) <> 8 then
  keypress = keypress & 8
else
  error = "You can use max 8 digits"
end if
Display = keypress
Python
error.Value = ""
if len(keypress.Value) < 8:
  keypress.Value = keypress.Value + "8"
else:
  error.Value = "You can use max 8 digits"
Display.Value = keypress.Value

 

If you want to get or set variable value you can no longer just its name, but have to use its “Value” property.

Python scripts in NiceLabel Portal

Because NiceLabel Portal runs in the browser and Python as such cannot be used there we used IronPython for running scripts in forms that are used in NiceLabel Portal. Because of this there are some differences between scripts that run on the desktop (in NiceForm) and those that run in NiceLabel Portal. Syntax of the scripts is the same you just cannot use all of the modules Python provides.

How can I tell on which platform my Python script is running?

import sys if sys.platform == 'silverlight': # Form runs in NiceLabel Portal else: # Form runs in NiceForm on the desktop

 

Resources

I find the following resources very useful for learning Python:

www.python.org/ (Python Home)

diveintopython.org/ (Dive into Python)

openbookproject.net//thinkCSpy/ (How to Think Like a Computer Scientist)

ironpython.net/ (IronPython Home)

www.trypython.org/ (Excellent resource for trying out what can be done via scripting in NiceLabel Portal)

Posted in NiceForm, NiceLabel Portal Tagged with: , , , , , , ,