Amend script to save with user-specified name?

Moderator: Kathy_9

Post Reply
terrypin
Posts: 492
Joined: Tue Jun 29, 2010 9:51 am
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: Asus Z170 Pro 4
processor: Intel Core i7 6700K 4.0GHz
ram: 32 GB
Video Card: None - uses built-in graphics
sound_card: HD onboard sound card
Hard_Drive_Capacity: 4.256 TB
Monitor/Display Make & Model: iLyama Prolite E2403WS 24" 1920x1200
Corel programs: Paint Shop Pro 8; Paint Shop Pro 2018
Location: East Grinstead UK

Amend script to save with user-specified name?

Post by terrypin »

I've successfully recorded a script to edit a JPG. But how can I get it to stop in the Save As... dialog and allow me to give it my chosen filename please? Or saves automatically with the current filename plus a numeric suffix. (Or even a random suffix, which will ensure it's unique and therefore allow me to rename it later.)

At present I can either
1. Stop after the editing and do the saving manually.
2. Save it with a fixed name and then rename manually.

But I'm fairly sure it should be possible to do what I want, because LeviFiction included the current filename in the script NameOnImage he kindly wrote for me recently.

Here's the version of my script that saves a fixed name:

Code: Select all

from JascApp import *

def ScriptProperties():
    return {
        'Author': u'',
        'Copyright': u'',
        'Description': u'',
        'Host': u'Paint Shop Pro',
        'Host Version': u'8.10'
        }

def Do(Environment):
    # EnableOptimizedScriptUndo
    App.Do( Environment, 'EnableOptimizedScriptUndo', {
            
            })

    # Magic Wand
    App.Do( Environment, 'MagicWand', {
            'General': {
                'Mode': App.Constants.SelectionOperation.Replace, 
                'Antialias': App.Constants.Boolean.false, 
                'Feather': 0, 
                'SampleMerged': App.Constants.Boolean.false
                }, 
            'MatchMode': App.Constants.MatchMode.RGBValue, 
            'Point': (5.5,6.5), 
            'Tolerance': 0, 
            'AntialiasType': App.Constants.AntialiasType.Outside, 
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Default, 
                'AutoActionMode': App.Constants.AutoActionMode.Match
                }
            })

    # SelectInvert
    App.Do( Environment, 'SelectInvert', {
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Default, 
                'AutoActionMode': App.Constants.AutoActionMode.Match
                }
            })

    # Expand Selection
    App.Do( Environment, 'SelectExpand', {
            'ExpandAmount': 10, 
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Default, 
                'AutoActionMode': App.Constants.AutoActionMode.Match
                }
            })

    # Copy
    App.Do( Environment, 'Copy', {
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Default, 
                'AutoActionMode': App.Constants.AutoActionMode.Match
                }
            })

    # PasteGraphicAsNewImage
    App.Do( Environment, 'PasteGraphicAsNewImage', {
            'CreateFromDropData': App.Constants.Boolean.false, 
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Default, 
                'AutoActionMode': App.Constants.AutoActionMode.Match
                }
            })

    # SelectDocument
    App.Do( Environment, 'SelectDocument', {
            'SelectedImage': 0, 
            'Strict': App.Constants.Boolean.false, 
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Default, 
                'AutoActionMode': App.Constants.AutoActionMode.Match
                }
            })

    # RunSelectedScript
    App.Do( Environment, 'RunSelectedScript', {
            'ScriptName': u'AddSimpleFrame', 
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Default, 
                'AutoActionMode': App.Constants.AutoActionMode.Match
                }
            })

    # FileSaveAs
    App.Do( Environment, 'FileSaveAs', {
            'Encoding': {
                'JPG': {
                    'Variant': App.Constants.JpegFormat.Standard, 
                    'CompressionFactor': 15, 
                    'ChromaSubSampling': App.Constants.ChromaSubSampling.YCC_2x2_1x1_1x1
                    }
                }, 
            'FileName': u'D:\\Videos+Projects\\PROJECTS\\SWCP2017 Proj\\Expanded.jpg', 
            'FileFormat': App.Constants.FileFormat.JPG, 
            'FormatDesc': u'JPEG - JFIF Compliant', 
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Default, 
                'AutoActionMode': App.Constants.AutoActionMode.AllAlways
                }, 
            'DefaultProperties': []
            })


--
Terry, East Grinstead, UK
Using PSP 8 & PSP 2018 under Win 10
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: Amend script to save with user-specified name?

Post by LeviFiction »

Change the execution mode to "Interactive" on the command. By default, it's set to "default" which will use whatever interactivity settings you have set on your script toolbar.

I'm not sure about PSP8 but when I hit the "Edit Script" button if it's a purely recorded script it brings up a dialog where I can change the execution mode of any command to either Silent, Default, or Interactive. And then just save the change. If you don't have this in PSP8 you can just change this line of code inside the "FileSaveAs" command.

Code: Select all

                'ExecutionMode': App.Constants.ExecutionMode.Default, 
Change it to

Code: Select all

                'ExecutionMode': App.Constants.ExecutionMode.Interactive,
https://levifiction.wordpress.com/
terrypin
Posts: 492
Joined: Tue Jun 29, 2010 9:51 am
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: Asus Z170 Pro 4
processor: Intel Core i7 6700K 4.0GHz
ram: 32 GB
Video Card: None - uses built-in graphics
sound_card: HD onboard sound card
Hard_Drive_Capacity: 4.256 TB
Monitor/Display Make & Model: iLyama Prolite E2403WS 24" 1920x1200
Corel programs: Paint Shop Pro 8; Paint Shop Pro 2018
Location: East Grinstead UK

Re: Amend script to save with user-specified name?

Post by terrypin »

Thanks! Will try that asap.

--------------------

Excellent - I chose the direct edit method and that fixed it immediately.

One nice refinement would be to automatically default to the same name as the source image, to save having to enter that manually before making a minor amendment. Currently the default is 'Expanded.jpg' as specified within the File Save As section:

Code: Select all

'FileName': u'D:\\Videos+Projects\\PROJECTS\\SWCP2017 Proj\\Expanded.jpg', 
How would I edit that so that it becomes the source file please?
--
Terry, East Grinstead, UK
Using PSP 8 & PSP 2018 under Win 10
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: Amend script to save with user-specified name?

Post by LeviFiction »

Easy, in place of the name just use the App.TargetDocument.Name variable. "Name" gives you the full path of the file.

Code: Select all

'FileName': App.TargetDocument.Name,
https://levifiction.wordpress.com/
terrypin
Posts: 492
Joined: Tue Jun 29, 2010 9:51 am
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: Asus Z170 Pro 4
processor: Intel Core i7 6700K 4.0GHz
ram: 32 GB
Video Card: None - uses built-in graphics
sound_card: HD onboard sound card
Hard_Drive_Capacity: 4.256 TB
Monitor/Display Make & Model: iLyama Prolite E2403WS 24" 1920x1200
Corel programs: Paint Shop Pro 8; Paint Shop Pro 2018
Location: East Grinstead UK

Re: Amend script to save with user-specified name?

Post by terrypin »

Thanks, but I'm unable to get that working. Here's a hopefully self-explanatory screenshot:

Image

Note that Image5.jpg, the arbitrary name assigned by PSP 8, is correct. IOW, the script has worked OK, expanding outside the inner rectangle and adding a simple frame.
--
Terry, East Grinstead, UK
Using PSP 8 & PSP 2018 under Win 10
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: Amend script to save with user-specified name?

Post by LeviFiction »

Weird, it should still work. TargetDocument should always point to the document that the script was run on.

Try this instead, create a Name variable that will hold the TargetDocument name, and then use that Name variable in the FileSaveAs command instead of App.TargetDocument.Name. Will look something similar to this.

Code: Select all

from JascApp import *

def ScriptProperties():
    return {
        'Author': u'',
        'Copyright': u'',
        'Description': u'',
        'Host': u'Paint Shop Pro',
        'Host Version': u'8.10'
        }

def Do(Environment):
    name = App.TargetDocument.Name

    # ... the rest of your code goes here

    # FileSaveAs
    App.Do( Environment, 'FileSaveAs', {
            'Encoding': {
                'JPG': {
                    'Variant': App.Constants.JpegFormat.Standard, 
                    'CompressionFactor': 15, 
                    'ChromaSubSampling': App.Constants.ChromaSubSampling.YCC_2x2_1x1_1x1
                    }
                }, 
            'FileName': name, 
            'FileFormat': App.Constants.FileFormat.JPG, 
            'FormatDesc': u'JPEG - JFIF Compliant', 
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Default, 
                'AutoActionMode': App.Constants.AutoActionMode.AllAlways
                }, 
            'DefaultProperties': []
            })
https://levifiction.wordpress.com/
terrypin
Posts: 492
Joined: Tue Jun 29, 2010 9:51 am
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: Asus Z170 Pro 4
processor: Intel Core i7 6700K 4.0GHz
ram: 32 GB
Video Card: None - uses built-in graphics
sound_card: HD onboard sound card
Hard_Drive_Capacity: 4.256 TB
Monitor/Display Make & Model: iLyama Prolite E2403WS 24" 1920x1200
Corel programs: Paint Shop Pro 8; Paint Shop Pro 2018
Location: East Grinstead UK

Re: Amend script to save with user-specified name?

Post by terrypin »

Thanks, tried that but I must have made a mistake somewhere. I get this message
"The script could not be loaded"

and this in the scrip output:

Traceback (most recent call last):
File "C:\Users\terry\My PSP8 Files\Scripts-Trusted\Expand rectangle+Frame+Save.PspScript", line 96, in Do
App.Do( Environment, 'FileSaveAs', {
NameError: global name 'xyz' is not defined
Executing FileClose
Executing DeleteLayer
Executing RunScript
File "<string>", line 106
'FileFormat': App.Constants.FileFormat.JPG,
^
SyntaxError: invalid syntax
Executing RunScript
File "<string>", line 96
App.Do( Environment, 'FileSaveAs', {
^
SyntaxError: invalid syntax
Executing DeleteLayer
Executing RunScript
File "<string>", line 95
App.Do( Environment, 'FileSaveAs', {
^
SyntaxError: invalid syntax

--------------------
Going out for the morning but will re-check my script on return. Meanwhile here it is:

Code: Select all

from JascApp import *

def ScriptProperties():
    return {
        'Author': u'',
        'Copyright': u'',
        'Description': u'',
        'Host': u'Paint Shop Pro',
        'Host Version': u'8.10'
        }

def Do(Environment):
    name = App.TargetDocument.Name


def Do(Environment):
    # EnableOptimizedScriptUndo
    App.Do( Environment, 'EnableOptimizedScriptUndo', {
            
            })

    # Magic Wand
    App.Do( Environment, 'MagicWand', {
            'General': {
                'Mode': App.Constants.SelectionOperation.Replace, 
                'Antialias': App.Constants.Boolean.false, 
                'Feather': 0, 
                'SampleMerged': App.Constants.Boolean.false
                }, 
            'MatchMode': App.Constants.MatchMode.RGBValue, 
            'Point': (5.5,6.5), 
            'Tolerance': 0, 
            'AntialiasType': App.Constants.AntialiasType.Outside, 
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Default, 
                'AutoActionMode': App.Constants.AutoActionMode.Match
                }
            })

    # SelectInvert
    App.Do( Environment, 'SelectInvert', {
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Default, 
                'AutoActionMode': App.Constants.AutoActionMode.Match
                }
            })

    # Expand Selection
    App.Do( Environment, 'SelectExpand', {
            'ExpandAmount': 10, 
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Default, 
                'AutoActionMode': App.Constants.AutoActionMode.Match
                }
            })

    # Copy
    App.Do( Environment, 'Copy', {
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Default, 
                'AutoActionMode': App.Constants.AutoActionMode.Match
                }
            })

    # PasteGraphicAsNewImage
    App.Do( Environment, 'PasteGraphicAsNewImage', {
            'CreateFromDropData': App.Constants.Boolean.false, 
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Default, 
                'AutoActionMode': App.Constants.AutoActionMode.Match
                }
            })

    # SelectDocument
    App.Do( Environment, 'SelectDocument', {
            'SelectedImage': 0, 
            'Strict': App.Constants.Boolean.false, 
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Default, 
                'AutoActionMode': App.Constants.AutoActionMode.Match
                }
            })

    # RunSelectedScript
    App.Do( Environment, 'RunSelectedScript', {
            'ScriptName': u'AddSimpleFrame', 
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Default, 
                'AutoActionMode': App.Constants.AutoActionMode.Match
                }
            })

     # FileSaveAs
        App.Do( Environment, 'FileSaveAs', {
                'Encoding': {
                    'JPG': {
                        'Variant': App.Constants.JpegFormat.Standard,
                        'CompressionFactor': 15,
                        'ChromaSubSampling': App.Constants.ChromaSubSampling.YCC_2x2_1x1_1x1
                        }
                    },
                'FileName': name,
                'FileFormat': App.Constants.FileFormat.JPG,
                'FormatDesc': u'JPEG - JFIF Compliant',
                'GeneralSettings': {
                    'ExecutionMode': App.Constants.ExecutionMode.Default,
                    'AutoActionMode': App.Constants.AutoActionMode.AllAlways
                    },
                'DefaultProperties': []
            })

If you remove the section 'RunSelectedScript' I assume you/anyone could try it on any selection?
--
Terry, East Grinstead, UK
Using PSP 8 & PSP 2018 under Win 10
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: Amend script to save with user-specified name?

Post by LeviFiction »

Well, two things.

1) You added a second def Do. I was simply trying to show you where

Code: Select all

name = App.TargetDocument.Name
was supposed to go. You put it under

Code: Select all

def Do(Environment):
So get rid of the extra def Do(Environment), and place named = App.TargetDocument.Name under the other one.

Also, I notice that in the code you pasted here the final SaveAs is not indented properly. There are too many spaces.
https://levifiction.wordpress.com/
terrypin
Posts: 492
Joined: Tue Jun 29, 2010 9:51 am
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: Asus Z170 Pro 4
processor: Intel Core i7 6700K 4.0GHz
ram: 32 GB
Video Card: None - uses built-in graphics
sound_card: HD onboard sound card
Hard_Drive_Capacity: 4.256 TB
Monitor/Display Make & Model: iLyama Prolite E2403WS 24" 1920x1200
Corel programs: Paint Shop Pro 8; Paint Shop Pro 2018
Location: East Grinstead UK

Re: Amend script to save with user-specified name?

Post by terrypin »

Pleased to report success at last!

It took much trial and error, as I can't get my head around the syntax rules: brackets, curly brackets, indented spaces, etc. Resorted to lining everything up as neatly as possible.

Thanks for your patient assistance, warmly appreciated!
--
Terry, East Grinstead, UK
Using PSP 8 & PSP 2018 under Win 10
Post Reply