How to conditionally end a Script?

Moderator: Kathy_9

Post Reply
ilgk48
Posts: 60
Joined: Sun Apr 28, 2019 12:26 pm
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
Corel programs: PaintShop PRO 2019 32bit

How to conditionally end a Script?

Post by ilgk48 »

Good morning,
this is not a matter of life or death :D

For conditionally end I mean this;
a) my script start and evaluates some conditions;
b) depending on the conditions, the script decides if to continue or to end and return to the GUI;
Say (pseudo code):

Code: Select all

#Analize condition
... do analisys ..

#Test condition
if condition == false:
	give a message
	return to the GUI (return? sys.exit? quit? ... other?)

#Condition is True, so continue with the script
def Do(Environment):
	App.Do  what you have to do ...
		
For ending:
a) the statement "return" is not applicable because it is reserved for ending a function;
b) sys.exit() ends PSP at all;
c) quit() is not recognized or it stays in some "import" that I do not know.
Please, how can I end the Script?

I have a workaround, but it annoys me because it fires a level of indenting, and i can't used to indenting (a my limitation :? )

Code: Select all

#Analize condition
... do analisys ..

def Do(Environment):
	#Test condition
	if condition == false:
		give a message
		... stops by itself
	
	#Condition is True, so continue with the script
	else:
		App.Do what you have to do ...
Thanks to all for reading.
Paolo
migf1
Posts: 485
Joined: Fri Apr 03, 2020 3:09 pm
operating_system: Windows 8.1
System_Drive: C
32bit or 64bit: 64 Bit

Re: How to conditionally end a Script?

Post by migf1 »

Hi Paolo,

your 2nd snippet is one way of doing it. To avoid the extra indentation, you can return inside the if-block and then omit the else-check:

Code: Select all

def Do(Environment):
	#Test condition
	if condition == false:  # to also check against None you may use: if not condition:
		give a message
		return
	
	#Condition is True, so continue with the script
	App.Do what you have to do ...
If condition is a global variable modified outside the Do() function, you may also want to declare it as global inside the Do() function, before checking it:

Code: Select all

def Do(Environment):
        global condition

	#Test condition
	if condition == false:  # to also check against None you may use: if not condition:
		give a message
		return
	
	#Condition is True, so continue with the script
	App.Do what you have to do ...
Yet another option (in case the condition validation is a complex task) is to wrap the complex code inside a validation function and call it whenever you need it.
Something like this:

Code: Select all

def valid_condition( cond ):
        if cond bla-bla-bla:
                return False
        ...
        return True

def Do(Environment):
        global condition  # only needed if condition is a global variable

	#Test condition
	if not valid_condition( condition ):
		give a message
		return
	
	#Condition is True, so continue with the script
	App.Do what you have to do ...

Good Unofficial PaintShop Pro Tutorials: Creation CasselMake Shop ProHEC Image EditingLeviFiction PSP Basics
(plus my own Gimp & Stuff)
ilgk48
Posts: 60
Joined: Sun Apr 28, 2019 12:26 pm
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
Corel programs: PaintShop PRO 2019 32bit

Re: How to conditionally end a Script?

Post by ilgk48 »

migf1 wrote:
Wed Apr 21, 2021 10:52 am
Hi Paolo,
your 2nd snippet is one way of doing it. To avoid the extra indentation, you can return inside the if-block and then omit the else-check:
Thanks many thanks! I've just applied your suggestion and now I understand where was my conceptual problem!
"Return" acts inside a Def for exit it, but I was trying to use "return" (at the top of the script) NOT within a Def.

PSP python execute the flow of statements starting from the top of the scritp, skipping every user def function, till it finds the last def DO(Environment) which is, let my say so, the main body of the script. Beeing this main body declared as a def, return obviously exits it !!!!
Problem solved.
Why the 'last' def DO(Environment)?
For testing, I appended two other def DO(Environment) and I've noted that apparently PSP python execute only the last.

Thanks again.
Paolo
migf1
Posts: 485
Joined: Fri Apr 03, 2020 3:09 pm
operating_system: Windows 8.1
System_Drive: C
32bit or 64bit: 64 Bit

Re: How to conditionally end a Script?

Post by migf1 »

ilgk48 wrote:
Wed Apr 21, 2021 12:43 pm

Thanks many thanks! I've just applied your suggestion and now I understand where was my conceptual problem!
"Return" acts inside a Def for exit it, but I was trying to use "return" (at the top of the script) NOT within a Def.
You are welcome Paolo!

Btw, I'm new to both Python and PSP scripting too, though I have a good programming background (mostly with C). I'm actually trying to learn Python and Tkinter by implementing something similar to the StockSolo extension for Ps, but I'm currently doing it as a stand-alone application, planning to accompanying it with a PSP script which will call it from within PSP (similar to what you did with the .py script you mentioned in the other topic, but that's gonna be the last step... for now I'm still working on the stand alone program).

Anyway, back to "return". It is the same in all programming languages I've come across. It's exiting the function or method. Python functions/methods are declared with the "def" keyword.
PSP python execute the flow of statements starting from the top of the scritp, skipping every user def function, till it finds the last def DO(Environment) which is, let my say so, the main body of the script. Beeing this main body declared as a def, return obviously exits it !!!!
Problem solved.
Why the 'last' def DO(Environment)?
For testing, I appended two other def DO(Environment) and I've noted that apparently PSP python execute only the last.

Thanks again.
Paolo
The Do() method seems to be indeed the entry point of any Psp script (it's "main" function as you called it, which is conceptually the same thing). This is what the docs say about it:
When PSP runs a script, it does so by loading the script into memory and invoking the Do method. The Do method takes a single parameter, by convention called Environment. This parameter exists to help coordination between the script and PSP, and will need to be passed back to PSP on all calls to App.Do. Do not modify the environment variable.

For all scripts created by the PSP script recorder, the Do method will be nothing more than a series of calls to App.Do
So in order to interact with PSP from within a script, you should do so inside the Do() method. I' wouldn't use more than one Do() methods in a script. I've never done it, so I didn't know PSP does not complain and keeps the last occurrence instead. But still, I can't think of any reason to have more than one Do() methods in the same script (and again, I would expect PSP to complain about it when you do so).

That said, if you completely omit the Do() method, I guess PSP will just use the Script Output palette as an output terminal for your other code, which will not be able to directly communicate with PSP.
Good Unofficial PaintShop Pro Tutorials: Creation CasselMake Shop ProHEC Image EditingLeviFiction PSP Basics
(plus my own Gimp & Stuff)
Post Reply