How to Write Your Own Scripts

by Sigrid, Co-Owner of EZ Tagging



Part 4 - Creating an IF and WHILE loop

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

Open the script again by clicking on the "Edit" command. Since you have used a text editor to edit it the last time the text editor should open up immediately.


Defining a IF - ELSE Condition

We have the variable and the input box. Now all we need is to define a loop and (very important) an event how to stop the script.

Python knows an if and else loop definition like most other programming languages. So we now have to define what the script should do if the user enters a new layer name and clicks on ok.

Below the last line of the GetString-event, add a blank line again (blank lines don't count in Python, only blank spaces in a line) to get a bit of space between the lines and then add the new line
if x==1:


(I have added a short commentary again)

We just have defined an if condition. That means everything below it has to belong to the ok-loop. Unfortunately at the moment it is still on the same level as the App.Do( Environment, 'EnableOptimizedScriptUndo', { because both begin at column 5 of the text.

If you want, you can save the script right now and try to let it run. You will get an error message like this:

We have two options now. Either we set the whole text below the if-operation some spaces back (which will be a lot of work and which also bears a great danger for errors if you loose count) or we can simply bring the whole GetString-command one or two spaces out to achive the same effect. I chose the second, easier option. So set your cursor before the line strMyLayer = "" and delete one blank. Do the same for every line in the paragraph var = App.Do( Environment, 'GetString', { including the line with the two brackets at the end and for the other lines we have added.



It is not very visible, since they are only one space apart but notice that the if-line and everything above begins further out than the rest of the text except for the line def Do(Environment): which, as mentioned always has to come first.

We have defined what the script should do if the user enters a text and presses ok. But still the script only runs once and then ends. If you want, you can let the script run now and see if it works. Don't forget to save first.

If you get an error message, check all the upper and lower cases, names of variables and the spaces before the lines.


Adding a WHILE Loop

Since we want the script to run again and again until we say stop we need still another loop.

If you know programming a bit you know that there is usually a while-loop option. That loop tells a program to run until a defined stopping option is reached. For our purpose we will have to define the variable GoOn which can be True or False.

Place your cursor below the variable definition strMyLayer="", then add the line
GoOn=True

We are now ready to insert the while-loop. Since we want the loop to begin with asking about the layer name, it has to begin directly before the Get String operation. And unfortuantely it has to be a level above the if-condition. Fortunately we still have a blank left but it is a close call. So add the line
while GoOn:
with two blanks before it.



GoOn is your variable. while must be lower case or Python won't recognize it as while and don't forget the colon or you will get a syntax error. GetString and everything following below now begins at column 3, while and the variable definition at column 2.

We are almost finished now. All we need is to define a stopping condition or the loop will run without end. If you would run the script now then there would be no stopping it and you would have to kill PSP with the task manager to get out of the loop.

In our if-condition we defined what should be done if the user presses ok on the input-box. So now we can define that if the user presses cancel then the while condition should break.

Place your cursor below the if-condition and add 4 blanks and the line
GoOn=True


This is just a confirmation for the script that the value of GoOn has not changed.

For the alternative condition you need to scroll down to the bottom of the script.

Add a new line after the last line press 3 blanks. We have to begin at column 3 since we are still adding to our if-condition and therefore have to be on the same level.

This last command return simply tells the script to stop.
This is how the last part of your script should look like now:



I have added a last few comments but they are not really necessary.

Now it's time to try the script again. Save it and let it run. Press cancel in the dialogbox whenever you like
Did it work? It did for me. Here is the result



Congratulations!

You have just written your first own script.

In the last part of this tutorial I will give you a few more tips and useful comments for Python that you can try out in your scripts.

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

Back Next

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

EZ TAGGING

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



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