How to Write Your Own Scripts

by Sigrid, Co-Owner of EZ Tagging



Part 3 - Editing with a Text Editor

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Analyzing the Script in a Text Editor


Make sure that you still have the copy of your script selected because now we are going to edit the script heavily. But don't worry, you cannot really do any damage even if something should go wrong. The worst thing that can happen is that PSP will crash on you. But only if you are not careful. Otherwise you can always press escape until the script is stopped.

Click on "Edit Script" again, but this time click on "Text Editor..."

This should open the text editor with your script text.
Your editor should show you something like this:

Have a good look at it. Can you find the commands you have used?

At the beginning you will find a line def Do(Environment): which is the beginning of the command section of the script. Since this is a recorded script the next command is the App.Do(Environment,'EnableOptimizedScriptUndo'{. It is optional and can be omitted. Most scripts that are written or edited don't have this part. But then your Layer Properties command begins. Scrolling down you can see it is very detailed and has every setting that a layer can have. PSP was even kind enough to record an explanatory comment before the command.


Comments in Scripts

Here we come to our first important information about a script. How to insert a comment into your text:

The # defines all letters behind it as a comment. It is easy to use for explanations about what a function does or where the loop begins and ends. As with all other programming languages commentaries are very important (and often forgotten). Remember, if you look at your script in a year or two and it is more complex than just a two command script you will likely not know anymore what you were about without a comment to remind you.


Adding variables

Since we want the script to ask the user for a name we will first need to define a variable to store the name in.

To define a variable, set your cursor exactly below the line def Do(Environment): in the first column.
Press return three times. Make sure that you are standing at the beginning of the line before you press return so that no blanks are deleted accidentally. Remember, blanks are important in Python. Then move the to the line below def Do(Environment): again.

Now add first 4 blank spaces and then the line
strMyLayer = ""
I have added a comment behind the variable.



Make sure that the line with the variables start at the same column as the line of App.Do( Environment, 'EnableOptimizedScriptUndo', {


Adding a Dialog Box

What is missing now is a dialog box that asks the user what he wants the name to look like. If you have ever written a program you probably know about the input-box. PSP knows such a box as well. It is called very aptly 'GetString'.

For your convenience I have written the whole GetString into a small text file. Just open it in your editor and copy the text and put it into the script at the right position (remember: blanks are important so make sure that everything is copied, even the blanks):


This defines an input-box named var with a title of "Get String", a Prompt that tells the user what to do, a length of 50 (which means the lenght of the layer name can be up to 50 letters) and a default text that is the same as the strMyLayer variable. Since we defined the variable as blank in the line above the first time this text will be blank but after we have added the loop it will have the previous text in it.

As you can see with var and x, a variable doesn't have to be defined beforehand, it can also be defined during the running of the script. var is the result for the input box and x a variable of the type Integer that stores the result of var. It can have the value of 0 (if you press cancel) or 1 (if you press ok).

You can freely choose a name for a variable. Just make sure that you don't choose accidentally a name that PSP or Python know in a different context (a variable continue for example would not be possible because the word continue is also a command in the script).

Save the script you have edited. If you run the script now, does the input box come up?

If you got an error message in the Script Output like this



then you should check the beginning of every line you have added. Make sure that the variable definition and the Get String operation begin at exactly the same column as the Layer Properties event below it. Also make sure that you have copied all the commas and brackets and blank lines of the GetString.txt file.

It worked for me but the layers will still all be named "Butterfly 1" or any other text you have defined while recording the script.

The reason for that is, that the App.Do( Environment, 'LayerProperties', { still doesn't know the variable we have defined.

To change that, open the script in the text editor again and go to the LayerProperties-function.
In the 'General':{-section in the third line you will find the line 'Name': u'[your text]',.


Delete the u'[your text]'-part of that line and insert instead your variable strMyLayer (mind the upper and lower case letters in the variable). Be careful about the comma at the end of the line. It has to stay or your script will fail with a syntax error.


Save your script and let it run again.

It ran perfectly for me and the name of the layer was changed without problem. Now we only need to add a loop to call up the GetString event for as often as we want.

~~~~~~~~~~~~~~~~

Back Next

~~~~~~~~~~~~~~~~

EZ Tagging

~~~~~~~~~~~~~~~~



This tutorial was written by Sigrid
Any similarities to other tutorials is merely a coincidence.
~All rights reserved - 2012~