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 !!!!
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.
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.