IF statement in PspScript

Moderator: Kathy_9

Post Reply
afarkas
Posts: 38
Joined: Thu Oct 23, 2014 11:36 am
operating_system: Windows 7 Home Premium
System_Drive: C
32bit or 64bit: 64 Bit

IF statement in PspScript

Post by afarkas »

Hi,
is there anything like "If statement" in PspScripting?

For example - is it possible to to skip some steps in PspScript if certain conditions are not met or if they are fullfilled???

How to do that?

Thanks
JoeB
Posts: 2778
Joined: Fri Mar 28, 2008 10:04 pm
operating_system: Windows 8.1
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: LENOVO 4524PE4 ThinkCentre M91p
processor: 3.10 gigahertz Intel Quad Core i5-2400
ram: 8 GB
Hard_Drive_Capacity: 4.6 TB
Corel programs: PSP 9, X7 to 2019, 32 & 64-bit
Location: Canada

Re: IF statement in PspScript

Post by JoeB »

If you type the words "python if statement" - you don't need the quotes - into Google you'll get some results that might prove helpful.
Regards,

JoeB
Using PSP 2019 64bit
LeviFiction
Advisor
Posts: 6831
Joined: Thu Oct 02, 2008 1:07 pm
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: Alienware M17xR4
processor: Intel Core i7-3630QM CPU - 2_40GH
ram: 6 GB
Video Card: NVIDIA GeForce GTX 660M
sound_card: Sound Blaster Recon3Di
Hard_Drive_Capacity: 500GB
Corel programs: PSP: 8-2023
Location: USA

Re: IF statement in PspScript

Post by LeviFiction »

Scripts are written in the python programming language. So yes, you can use the if statement in python to add flow control to your scripts. But you must add this manually using your favorite text editor (Notepad, or Notepad++, or other such editors) there is no way to do it from the UI or while recording a script.
https://levifiction.wordpress.com/
afarkas
Posts: 38
Joined: Thu Oct 23, 2014 11:36 am
operating_system: Windows 7 Home Premium
System_Drive: C
32bit or 64bit: 64 Bit

Re: IF statement in PspScript

Post by afarkas »

This is great news, mister...
I am familiar with notepad++ and i already made modifications in my scripts with notepad...
Now, i can easy write the command to merge layergroup, but have no idea how to define "If the layer is a layergroup" ...

(If i give an order to merge layergroup and the layer is not actually a layergroup but a single layer, it will end up with error)

Can you suggest a direction how to find it out?

Thank you
LeviFiction wrote:Scripts are written in the python programming language. So yes, you can use the if statement in python to add flow control to your scripts. But you must add this manually using your favorite text editor (Notepad, or Notepad++, or other such editors) there is no way to do it from the UI or while recording a script.
LeviFiction
Advisor
Posts: 6831
Joined: Thu Oct 02, 2008 1:07 pm
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: Alienware M17xR4
processor: Intel Core i7-3630QM CPU - 2_40GH
ram: 6 GB
Video Card: NVIDIA GeForce GTX 660M
sound_card: Sound Blaster Recon3Di
Hard_Drive_Capacity: 500GB
Corel programs: PSP: 8-2023
Location: USA

Re: IF statement in PspScript

Post by LeviFiction »

There is a pinned topic called 'Scripting Resources" that points to a lot of the basic information you need: http://forum.corel.com/EN/viewtopic.php?f=104&t=56846

Scripting For Script Authors is a PDF document that explains a lot about how PSP organizes data and what you need to do to call commands. It breaks down things like getting layer information and navigating the layers use the the selectLayer command etc. It also does a bit on various other helpful utility commands such as message boxes, getting data from the user, just a whole basic primer for writing scripts for PSP.

Then there is the Script API. This is a list of all of the commands and their parameters so you can find any command you need. Really helpful when some things aren't recordable such as 'ReturnImageInfo" or "GetLayerProperties." Speaking of "GetLayerProperties" will return a layer type and it's that you can do comparisons against.

Several versions are listed, just grab the one that's closest to the version of PSP you use.
https://levifiction.wordpress.com/
afarkas
Posts: 38
Joined: Thu Oct 23, 2014 11:36 am
operating_system: Windows 7 Home Premium
System_Drive: C
32bit or 64bit: 64 Bit

Re: IF statement in PspScript

Post by afarkas »

Thank you, Sir
I am going to search for that API... That is exactly what i need.
Thanks
LeviFiction wrote:There is a pinned topic called 'Scripting Resources" that points to a lot of the basic information you need: http://forum.corel.com/EN/viewtopic.php?f=104&t=56846

Scripting For Script Authors is a PDF document that explains a lot about how PSP organizes data and what you need to do to call commands. It breaks down things like getting layer information and navigating the layers use the the selectLayer command etc. It also does a bit on various other helpful utility commands such as message boxes, getting data from the user, just a whole basic primer for writing scripts for PSP.

Then there is the Script API. This is a list of all of the commands and their parameters so you can find any command you need. Really helpful when some things aren't recordable such as 'ReturnImageInfo" or "GetLayerProperties." Speaking of "GetLayerProperties" will return a layer type and it's that you can do comparisons against.

Several versions are listed, just grab the one that's closest to the version of PSP you use.
afarkas
Posts: 38
Joined: Thu Oct 23, 2014 11:36 am
operating_system: Windows 7 Home Premium
System_Drive: C
32bit or 64bit: 64 Bit

Re: IF statement in PspScript

Post by afarkas »

hm... noone, not even mister google, ever heard about "GetLayeProperties" ...
LeviFiction wrote:There is a pinned topic called 'Scripting Resources" that points to a lot of the basic information you need: http://forum.corel.com/EN/viewtopic.php?f=104&t=56846

Scripting For Script Authors is a PDF document that explains a lot about how PSP organizes data and what you need to do to call commands. It breaks down things like getting layer information and navigating the layers use the the selectLayer command etc. It also does a bit on various other helpful utility commands such as message boxes, getting data from the user, just a whole basic primer for writing scripts for PSP.

Then there is the Script API. This is a list of all of the commands and their parameters so you can find any command you need. Really helpful when some things aren't recordable such as 'ReturnImageInfo" or "GetLayerProperties." Speaking of "GetLayerProperties" will return a layer type and it's that you can do comparisons against.

Several versions are listed, just grab the one that's closest to the version of PSP you use.
LeviFiction
Advisor
Posts: 6831
Joined: Thu Oct 02, 2008 1:07 pm
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: Alienware M17xR4
processor: Intel Core i7-3630QM CPU - 2_40GH
ram: 6 GB
Video Card: NVIDIA GeForce GTX 660M
sound_card: Sound Blaster Recon3Di
Hard_Drive_Capacity: 500GB
Corel programs: PSP: 8-2023
Location: USA

Re: IF statement in PspScript

Post by LeviFiction »

Follow the link I gave you to the post. Download the Script API from one of the links on that post. Unzip it. Open index.html in a web browser, do a quick search for "LayerProperties" I don't have the file on my phone so I can't look it up. It's probably "ReturnLayerProperties" instead of "GetLayerProperites" a general Google search won't help as these are very specific to PSP and haven't been generally released on any website.
https://levifiction.wordpress.com/
afarkas
Posts: 38
Joined: Thu Oct 23, 2014 11:36 am
operating_system: Windows 7 Home Premium
System_Drive: C
32bit or 64bit: 64 Bit

Re: IF statement in PspScript

Post by afarkas »

Well, seems i have some strange version of PSP...
I have PaintShop Pro 2018 ...

I found LayerProperties in that index, but my PSP does not recognises that command...

In a "faramir script" i found the utilisation of that command"

Code: Select all

    # A flatten image improves speed and prevent script errors
    LayerProperties = App.Do( Environment, 'ReturnLayerProperties' )
    if LayerProperties[ 'IsBackground' ] == App.Constants.Boolean.false:
So i have try to make it that way...
I build the scrit like:

Code: Select all

	LayerProperties = App.Do( Environment, 'ReturnLayerProperties' )
    if LayerProperties[ 'IsBackground' ] == App.Constants.Boolean.false:
		# LayerMergeGroup
		App.Do( Environment, 'LayerMergeGroup', {
				'GeneralSettings': {
					'ExecutionMode': App.Constants.ExecutionMode.Default, 
					'AutoActionMode': App.Constants.AutoActionMode.Match, 
					'Version': ((20,0,0),1)
					}
				})
This should make exactly what i need, but, there is an error... PSP does nor recognises "LayerProperties" command

It says:
Executing RunScript
File "<string>", line 31
LayerProperties = App.Do( Environment, 'ReturnLayerProperties' )
^
IndentationError: unexpected indent

Did not found CommandApi for PSP2018 ... only PSPX*

But Thanks anyway for the direction...
LeviFiction wrote:Follow the link I gave you to the post. Download the Script API from one of the links on that post. Unzip it. Open index.html in a web browser, do a quick search for "LayerProperties" I don't have the file on my phone so I can't look it up. It's probably "ReturnLayerProperties" instead of "GetLayerProperites" a general Google search won't help as these are very specific to PSP and haven't been generally released on any website.
LeviFiction
Advisor
Posts: 6831
Joined: Thu Oct 02, 2008 1:07 pm
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: Alienware M17xR4
processor: Intel Core i7-3630QM CPU - 2_40GH
ram: 6 GB
Video Card: NVIDIA GeForce GTX 660M
sound_card: Sound Blaster Recon3Di
Hard_Drive_Capacity: 500GB
Corel programs: PSP: 8-2023
Location: USA

Re: IF statement in PspScript

Post by LeviFiction »

indentation error is the error. It's not that it doesn't recognize the command. It's that your spacing doesn't match.

Python is white-space sensitive as it uses indentation to determine which areas of code belong to each other. You need to be careful not to mix spaces and tabs, and to always use the same number of spaces or tabs as other indented pieces of code. PSP's recorded scripts always use 4 spaces, never tabs. So if you've tabbed in the code or used too many spaces or too few spaces. PSP is going to throw an error.

I recommend reading up on Python basics before continuing. It doesn't take long to learn at all.

As for the API, the newest API available was last released for version X8. In that form post I gave you the link to the last download link was for X8. Here is the download link for PSP X8 (18) Script API. https://drive.google.com/open?id=0B8FlF ... 29la2FyYmM Follow that link to download the last version of the API that we have.

Very little changes in the script API so the fact that it's not for 2018 is mostly irrelevant.
https://levifiction.wordpress.com/
afarkas
Posts: 38
Joined: Thu Oct 23, 2014 11:36 am
operating_system: Windows 7 Home Premium
System_Drive: C
32bit or 64bit: 64 Bit

Re: IF statement in PspScript

Post by afarkas »

OK, thank you sir, i am going to brush thru that
LeviFiction wrote:indentation error is the error. It's not that it doesn't recognize the command. It's that your spacing doesn't match.

Python is white-space sensitive as it uses indentation to determine which areas of code belong to each other. You need to be careful not to mix spaces and tabs, and to always use the same number of spaces or tabs as other indented pieces of code. PSP's recorded scripts always use 4 spaces, never tabs. So if you've tabbed in the code or used too many spaces or too few spaces. PSP is going to throw an error.

I recommend reading up on Python basics before continuing. It doesn't take long to learn at all.

As for the API, the newest API available was last released for version X8. In that form post I gave you the link to the last download link was for X8. Here is the download link for PSP X8 (18) Script API. https://drive.google.com/open?id=0B8FlF ... 29la2FyYmM Follow that link to download the last version of the API that we have.

Very little changes in the script API so the fact that it's not for 2018 is mostly irrelevant.
afarkas
Posts: 38
Joined: Thu Oct 23, 2014 11:36 am
operating_system: Windows 7 Home Premium
System_Drive: C
32bit or 64bit: 64 Bit

Re: IF statement in PspScript

Post by afarkas »

Ok, so i corrected the script and it runs now, but get to an other error

i want to merge layer group only in case it is a layer group and pass to next step in case it is a single layer
so if it is not a group, the "if LayerProperties[ 'IsBackground' ] == App.Constants.Boolean.false:" leads to "unsupported layer" error...

I have also try "if LayerProperties[ 'IsGroup' ] == App.Constants.Boolean.true:" but "IsGroup", "IsLayerGroup", "IsGrouped" are apparently unrecognized strings...

In that API i downloaded there is no mention how Layer Group is identified, neither google knows the answer...

Anyone any idea?
LeviFiction wrote:indentation error is the error. It's not that it doesn't recognize the command. It's that your spacing doesn't match.

Python is white-space sensitive as it uses indentation to determine which areas of code belong to each other. You need to be careful not to mix spaces and tabs, and to always use the same number of spaces or tabs as other indented pieces of code. PSP's recorded scripts always use 4 spaces, never tabs. So if you've tabbed in the code or used too many spaces or too few spaces. PSP is going to throw an error.

I recommend reading up on Python basics before continuing. It doesn't take long to learn at all.

As for the API, the newest API available was last released for version X8. In that form post I gave you the link to the last download link was for X8. Here is the download link for PSP X8 (18) Script API. https://drive.google.com/open?id=0B8FlF ... 29la2FyYmM Follow that link to download the last version of the API that we have.

Very little changes in the script API so the fact that it's not for 2018 is mostly irrelevant.
LeviFiction
Advisor
Posts: 6831
Joined: Thu Oct 02, 2008 1:07 pm
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: Alienware M17xR4
processor: Intel Core i7-3630QM CPU - 2_40GH
ram: 6 GB
Video Card: NVIDIA GeForce GTX 660M
sound_card: Sound Blaster Recon3Di
Hard_Drive_Capacity: 500GB
Corel programs: PSP: 8-2023
Location: USA

Re: IF statement in PspScript

Post by LeviFiction »

You use ['LayerType']. In the API there is a link to the "Returned Properties of the Layer" when you click on this it takes you to a list of the returned options. They present these as a table of Name, Script Name, Type, Description, etc. The ScriptName is the name you use in scripts.
Script Name
===============
LayerType
LayerRect
IsBackground
Path
In the Description it may also have a link to further details on the types. In this case LayerType is an Enumerated constant. This means that the first possible value is 0 and it increases by 1 for each subsequent type. You don't have to know the numbers though, PSP makes this easy by storing them in the Constants variable. App.Constants.LayerType.Raster or Vector or ArtMedia, etc. Here is the list provided by the API documentation The first few show the name, the value associated with that name, the layter type, and finally the variable that you can use if you don't want to use the value. I only do this for the first few, the rest are just the name an value. I'm sure you can see the pattern an work out the script variable name for yourself.
Name Value Description Script variable name
Raster 0 Raster layer App.Constants.LayerType.Raster
Vector 1 Vector layer App.Constants.LayerType.Vector
Group 2 Group layer App.Constants.LayerType.Group
Mask 3 Mask layer App.Constants.LayerType.Mask
ColorBalance 4 White balance App.Constants.LayerType.ColorBalance
ChannelMixer 5 Channel mixer App.Constants.LayerType.ChannelMixer
Curves 6
HueSatLum 7
Posterize 8
Threshold 9
Invert 10
Levels 11
BrightnessContrast 12
Selection 13
FloatingSelection 14
ArtMedia 15
Vibrancy 16
Histogram 17
LocalToneMapping 18
FillLightClarity 19
SmartFix 20
Now, the reason that there is a 'IsBackground' variable is because background layers are raster layers. For example, if you have a background layer selected both of these If Statements return true. If you only have a regular raster layer selected the first will return false and not print anything. Only the second if statement will.

Code: Select all

    if LayerProperties['IsBackground']:
        print "This is a raster layer and a background layer"
    if LayerProperties['LayerType'] == 0:
        print "This is a raster layer"
Every other layer type other than "Background" will be identified by the 'LayerType' key.

So to check if the layer is a layer group simply compare the 'LayerType' to App.Constants.LayerType.Group or the number 2. Both of these examples do the exact same thing. One uses just the value 2 while the other uses the variable that PSP provides.

Code: Select all

    if LayerProperties['LayerType'] == 2:
        print "This is a Layer Group"

    if LayerProperties['LayerType'] == App.Constants.LayerType.Group:
        print "This is a Layer Group" 
I highly recommend reading the Scripting For Script Authors PDF. The link for which is also found in the Scripting Resources topic at the top of the PSP Scripting Subform. Here is the direct Download from Corel http://dwnld.paintshoppro.com/member/PS ... tGuide.exe

When you run the installer it'll add a folder to your Documents folder called "Corel PaintShop Pro X8 Script Guide" Which will include the PDF and some sample scripts.
https://levifiction.wordpress.com/
afarkas
Posts: 38
Joined: Thu Oct 23, 2014 11:36 am
operating_system: Windows 7 Home Premium
System_Drive: C
32bit or 64bit: 64 Bit

Re: IF statement in PspScript

Post by afarkas »

Ah so... so it is Layertype...
LayerType is not mentioned in the PSPCommandAPI, thats why i did not found it - went thru several times...

Thank you so much, Sir
With this explanation it will be so easy to build nearly any script now

Thanks again
LeviFiction wrote:You use ['LayerType']. In the API there is a link to the "Returned Properties of the Layer" when you click on this it takes you to a list of the returned options. They present these as a table of Name, Script Name, Type, Description, etc. The ScriptName is the name you use in scripts.
Script Name
===============
LayerType
LayerRect
IsBackground
Path
In the Description it may also have a link to further details on the types. In this case LayerType is an Enumerated constant. This means that the first possible value is 0 and it increases by 1 for each subsequent type. You don't have to know the numbers though, PSP makes this easy by storing them in the Constants variable. App.Constants.LayerType.Raster or Vector or ArtMedia, etc. Here is the list provided by the API documentation The first few show the name, the value associated with that name, the layter type, and finally the variable that you can use if you don't want to use the value. I only do this for the first few, the rest are just the name an value. I'm sure you can see the pattern an work out the script variable name for yourself.
Name Value Description Script variable name
Raster 0 Raster layer App.Constants.LayerType.Raster
Vector 1 Vector layer App.Constants.LayerType.Vector
Group 2 Group layer App.Constants.LayerType.Group
Mask 3 Mask layer App.Constants.LayerType.Mask
ColorBalance 4 White balance App.Constants.LayerType.ColorBalance
ChannelMixer 5 Channel mixer App.Constants.LayerType.ChannelMixer
Curves 6
HueSatLum 7
Posterize 8
Threshold 9
Invert 10
Levels 11
BrightnessContrast 12
Selection 13
FloatingSelection 14
ArtMedia 15
Vibrancy 16
Histogram 17
LocalToneMapping 18
FillLightClarity 19
SmartFix 20
Now, the reason that there is a 'IsBackground' variable is because background layers are raster layers. For example, if you have a background layer selected both of these If Statements return true. If you only have a regular raster layer selected the first will return false and not print anything. Only the second if statement will.

Code: Select all

    if LayerProperties['IsBackground']:
        print "This is a raster layer and a background layer"
    if LayerProperties['LayerType'] == 0:
        print "This is a raster layer"
Every other layer type other than "Background" will be identified by the 'LayerType' key.

So to check if the layer is a layer group simply compare the 'LayerType' to App.Constants.LayerType.Group or the number 2. Both of these examples do the exact same thing. One uses just the value 2 while the other uses the variable that PSP provides.

Code: Select all

    if LayerProperties['LayerType'] == 2:
        print "This is a Layer Group"

    if LayerProperties['LayerType'] == App.Constants.LayerType.Group:
        print "This is a Layer Group" 
I highly recommend reading the Scripting For Script Authors PDF. The link for which is also found in the Scripting Resources topic at the top of the PSP Scripting Subform. Here is the direct Download from Corel http://dwnld.paintshoppro.com/member/PS ... tGuide.exe

When you run the installer it'll add a folder to your Documents folder called "Corel PaintShop Pro X8 Script Guide" Which will include the PDF and some sample scripts.
Post Reply