Script editing?

Moderator: Kathy_9

User avatar
Scribzy
Posts: 45
Joined: Sun Jun 21, 2020 1:41 pm
operating_system: Windows 10
System_Drive: N/A

Script editing?

Post by Scribzy »

Hello I am trying to edit a script to include a for loop to save an image that moves left 10px until off the side of the screen.

but I'm not sure how to include it.
I would also need a variable to save the image name to an auto-incensement number (image[x].png)

Script:

Code: Select all

from PSPApp import *

def ScriptProperties():
    return {
        'Author': u'',
        'Copyright': u'',
        'Description': u'',
        'Host': u'PaintShop Pro',
        'Host Version': u'22.00'
        }

def Do(Environment):
    # EnableOptimizedScriptUndo
    App.Do( Environment, 'EnableOptimizedScriptUndo', {
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Default, 
                'AutoActionMode': App.Constants.AutoActionMode.Match, 
                'Version': ((22,0,0),1)
                }
            })

    # Pick
    App.Do( Environment, 'Pick', {
            'Type': App.Constants.ObjectSelection.Select, 
            'X': 0, 
            'Y': 0, 
            'Width': 1, 
            'Height': 1, 
            'Group': True, 
            'Handle1': (335,805), 
            'Handle2': (335,194), 
            'Handle3': (645,194), 
            'Handle4': (645,805), 
            'Pivot': (500,499.5), 
            'EarlyX4Script': False, 
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Default, 
                'AutoActionMode': App.Constants.AutoActionMode.Match, 
                'Version': ((22,0,0),1)
                }
            })

    # PickFixup
    App.Do( Environment, 'PickFixup', {
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Silent, 
                'AutoActionMode': App.Constants.AutoActionMode.Match, 
                'Version': ((22,0,0),1)
                }
            })

    # FileSaveAs
    App.Do( Environment, 'FileSaveAs', {
            'Encoding': {
                'PNG': {
                    'Interlaced': False, 
                    'OptimizedPalette': True, 
                    'AlphaChannel': False
                    }
                }, 
            'FileName': u'C:\\Users\\Windows\\Downloads\\psp move left\\Image1.png', 
            'FileFormat': App.Constants.FileFormat.PNG, 
            'FormatDesc': u'PNG Portable Network Graphics', 
            'WorkingMode': 0, 
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Default, 
                'AutoActionMode': App.Constants.AutoActionMode.AllAlways, 
                'Version': ((22,0,0),1)
                }, 
            'DefaultProperties': []
            })

I am not sure where to place for x in range(50): I am guessing above # Pick and how to open/close the loop
and how to set and use variables to change the image number and the Handle3 data.

Any help or pointers in the right direction would be much appreciated!

Also is this the same scripting as python?
Kathy_9
Site Admin
Posts: 2699
Joined: Tue Nov 30, 2010 12:44 am
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
processor: 8th Generation Intel Core i7 8700 3 20 GHz
ram: 16GB
Video Card: NVIDIA GeForce RTX 2060 [6 GB GDDR6 dedicated]
sound_card: NVIDIA High Definition Audio
Hard_Drive_Capacity: 2TB
Monitor/Display Make & Model: HP EliteDisplay E243m 23.8-inch
Corel programs: PSPX2 ~2023; Painter 2018~22
Location: USA

Re: Script editing?

Post by Kathy_9 »

Moved to the Scripting sub forum where it will get better attention.
PSPX9 | PSP2019 | PSP2020 | PSP2021| PSP2022 | PSP2023 & PhotoMirage installed; PSPX | PSPX2 thru PSP2018 owned but not installed
http://www.flickr.com/photos/37153430@N03/
LeviFiction
Advisor
Posts: 6472
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: Script editing?

Post by LeviFiction »

Yes this is python scripting, it's not just the same they are literally using python. Because this is python using whitespace to indent lines under the for loop declaration statement will make the code block.

If your goal is to move a layer off screen and save a copy of the image after each small movement you want to put the for...loop before the Pick command, and then indent the Pick and FileSaveAs by an additional 4 spaces. Each line. I like using a full editor (like VS Code or Notepad++) for this as I just need to highlight it and then hit "tab" and it indents the line for me.

I recommend using the Move tool (command Name "Mover") as it only uses an offset parameter (x,y) so you don't have to do any math. Just set the offset to (10,0) and it'll move 10px to the right, set it to (-10,0) and it'll move 10px to the left every time it gets run. With the Pick tool, you have to move all 4 handles otherwise you'll end up stretching the layer. Not that this is hard, you're just subtracting 10 from the X value in each x,y pair. But you also want to calculate the new pivot point every time otherwise the pivot point will get left behind. And you have to get the current layer rectangle so that it can work on images of all sizes. It's just way easier to use Mover.

Code: Select all

    # Move
    App.Do( Environment, 'Mover', {
            'Offset': (-10,0), 
            'Object': App.Constants.LayerOrSelection.Layer, 
            })
As for the filename. You can either use a library like os to do the work for you. Or you can use string manipulation. But essentially what you're wanting to do is separate the filename from the extension. I use the library method using the os library. Quick hint, App.TargetDocument is a variable that holds the image that was active when you started the script. And the member variable "Name" contains the full file pathname. If the image is brand new and hasn't been saved yet there will only be a name like "Image1" and no path.

Code: Select all

from PSPApp import *
import os

# ... code up to this point
filename, ext = os.path.spllitext(App.TargetDocument.Name)
Now we have two variables, the full filepath minus the extension and the extension. Since we'll be forcing this to save as PNG we'll want to use .'png' as the extension so we'll ignore the extension variable. There is another command inside the os.path library called "isfile" which will check the path and return true or false if the path leads to an existing file or not. I use this to determine if the Image is already saved or brand new. If it's brand new I save it to a default location.

Code: Select all

from PSPApp import *
import Os 

#... code up to this point
    if os.path.isfile(App.TargetDocument.Name):
        filename, ext = os.path.splitext(App.TargetDocument.Name)
    else:
        filename = "c:\\Users\\Windows\\Downloads\\psp move left\\Image"
    ext = '.png'
We then replace the "FileName" parameter with our new filename + str(x) + ext

Code: Select all

                'FileName': filename + str(x) + ext, 
And I'm going to make one final edit. I'm going to change the ExecutionMode of FileSaveAs to "Silent" so that it never asks for the filename even if you have "interactive" mode turned on. This is just my personal preference. You can leave this as "Default" if you want.

Now that the filename variable has been set up and I have moved from the Pick tool to the move tool this is all a lot easier.

Put the Move tool and FileSaveAs indented under the for loop and it'll loop 50 times to move the layer to the left by 500 pixels saving after each move. Here's the full code.

Code: Select all

    for x in range(50):
        # Move
        App.Do( Environment, 'Mover', {
                'Offset': (-10,0), 
                'Object': App.Constants.LayerOrSelection.Layer, 
                })

        # FileSaveAs
        App.Do( Environment, 'FileSaveAs', {
                'Encoding': {
                    'PNG': {
                        'Interlaced': False, 
                        'OptimizedPalette': True, 
                        'AlphaChannel': False
                        }
                    }, 
                'FileName': filename + str(x) + ext, 
                'FileFormat': App.Constants.FileFormat.PNG, 
                'FormatDesc': u'PNG Portable Network Graphics', 
                'WorkingMode': 0, 
                'GeneralSettings': {
                    'ExecutionMode': App.Constants.ExecutionMode.Silent, 
                    'AutoActionMode': App.Constants.AutoActionMode.AllAlways, 
                    'Version': ((22,0,0),1)
                    }, 
                'DefaultProperties': []
                })
If you want to move the layer completely off the canvas, it'll only work if the image is 500px right now. So you might want to make the number of loops equal to the Width of the image/10. Luckily App.TargetDocument also has a member variable named "Width" for the image width.

Code: Select all

loops = App.TargetDocument.Width /10
#Check if there is a remainder, if there is, add one more loop to finish it off
if App.TargetDocument.Width % 10 > 0:
    loops += 1
Here's the entire script

Code: Select all

from PSPApp import *
import os 

def ScriptProperties():
    return {
        'Author': u'',
        'Copyright': u'',
        'Description': u'',
        'Host': u'PaintShop Pro',
        'Host Version': u'22.00'
        }

def Do(Environment):
    # EnableOptimizedScriptUndo
    App.Do( Environment, 'EnableOptimizedScriptUndo', {
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Default, 
                'AutoActionMode': App.Constants.AutoActionMode.Match, 
                'Version': ((22,0,0),1)
                }
            })
    loops = App.TargetDocument.Width /10 
    #Check if there is a remainder, if there is, add one more loop to finish it off
    if App.TargetDocument.Width % 10 > 0:
        loops += 1

    if os.path.isfile(App.TargetDocument.Name):
        filename, ext = os.path.splitext(App.TargetDocument.Name)
    else:
        filename = "c:\\Users\\Windows\\Downloads\\psp move left\\Image"

    ext = ".png"
    for x in range(loops):
        # Move
        App.Do( Environment, 'Mover', {
                'Offset': (-10,0), 
                'Object': App.Constants.LayerOrSelection.Layer, 
                })

        # FileSaveAs
        App.Do( Environment, 'FileSaveAs', {
                'Encoding': {
                    'PNG': {
                        'Interlaced': False, 
                        'OptimizedPalette': True, 
                        'AlphaChannel': False
                        }
                    }, 
                'FileName': filename + str(x) + ext, 
                'FileFormat': App.Constants.FileFormat.PNG, 
                'FormatDesc': u'PNG Portable Network Graphics', 
                'WorkingMode': 0, 
                'GeneralSettings': {
                    'ExecutionMode': App.Constants.ExecutionMode.Silent, 
                    'AutoActionMode': App.Constants.AutoActionMode.AllAlways, 
                    'Version': ((22,0,0),1)
                    }, 
                'DefaultProperties': []
                })
User avatar
Scribzy
Posts: 45
Joined: Sun Jun 21, 2020 1:41 pm
operating_system: Windows 10
System_Drive: N/A

Re: Script editing?

Post by Scribzy »

Wow @LeviFiction Thank you so much for spending that much time on this question.
You have really helped me to understand this a lot better and you have even gone further and fixed future problems with image sizes being different sizes.

I am very impressed and very grateful!
User avatar
Scribzy
Posts: 45
Joined: Sun Jun 21, 2020 1:41 pm
operating_system: Windows 10
System_Drive: N/A

Re: Script editing?

Post by Scribzy »

I just tested this and it failed the first time as I had it in a restricted folder.

The second time it ran and did exactly what I wanted!
It did error out on the last frame:

Code: Select all

Executing FileSaveAs
Executing Move
------- Command Execution Failed -----------
Command Name:	Move
Error Text:	Internal program error: An invalid parameter has been passed to a function
Traceback (most recent call last):
  File "<string>", line 37, in Do
JascApp.InvalidParameterType: Invalid parameter type (parameter )
------- Command Execution Failed -----------
Command Name:	RunScript
Error Text:	Internal program error: An invalid parameter has been passed to a function

Script 'move left' has completed with an error.
But it doesn't matter as all other images were perfect!

Thank you once again! :D