PSP script to "Save Copy As:" maintaining file name

Moderator: Kathy_9

bobclevenger
Posts: 20
Joined: Mon Dec 21, 2020 4:09 am
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: Shuttle SH370R6 V2
processor: Intel Core i9
ram: 128 GB
Video Card: Onboard
sound_card: Onboard
Hard_Drive_Capacity: 2TB
Monitor/Display Make & Model: HP 27yh
Corel programs: Paint Shop Pro 2021
Location: Reno, Nevada

PSP script to "Save Copy As:" maintaining file name

Post by bobclevenger »

I would like a script that will do a "Save Copy As:" procedure that keeps the file name of the file being saved (not the file that was used when recording the script). Essentially I wish the script would assign the file name to a variable and then pass that variable to the "File Save Copy As:" operation. This could be an interactive script only if necessary.
LeviFiction
Advisor
Posts: 6777
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: PSP script to "Save Copy As:" maintaining file name

Post by LeviFiction »

So, this is technically easy. PSP provides a python object for the active image and the image that was targeted when you ran the script. App.ActiveDocument and App.TargetDocument. These objects include a member variable called Name that will return the full path of the file if it has one. If it's a brand new image it'll just have the image name.

Now, it should be noted that a lot of the functionality of the Save Copy As command comes inside the dialog box itself. For example, changing the extension of the file to match the selected extension. So if it's not running interactively you will need to make up for that or PSP will save the file with whatever name you pass into it.

If you record a script that includes Save Copy As (make sure to save it in Scripts-Trusted) just open the script in Notepad or your preferred code editor. And you're looking for a line that says "App.Do( Environment, 'FileSaveCopyAs', {"

Here's an Example. This will save the file as a JPG using the File Save Copy As command.

Code: Select all

    # FileSaveCopyAs
    App.Do( Environment, 'FileSaveCopyAs', {
            'Encoding': {
                'JPG': {
                    'Variant': App.Constants.JpegFormat.Standard, 
                    'CompressionFactor': 6, 
                    'ChromaSubSampling': App.Constants.ChromaSubSampling.YCC_1x1_1x1_1x1, 
                    'EXIF': True, 
                    'EmbedJPGICC': True
                    }
                }, 
            'FileName': u'C:\\Users\\Jack\\Desktop\\IMG_0001 -1.jpg', 
            'FileFormat': App.Constants.FileFormat.JPG, 
            'FormatDesc': u'JPG JPEG ', 
            'WorkingMode': 0, 
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Default, 
                'AutoActionMode': App.Constants.AutoActionMode.Match, 
                'Version': ((23,0,0),1)
                }, 
            'DefaultProperties': []
            })
Notice the 'FileName': parameter. It uses a unicode string literal for the filename. We can erase this and simply put App.TargetDocument.Name

Code: Select all

            'FileName': App.TargetDocument.Name, 
And so long as the targetdocument's extension and the FileSaceCopyAs extension are the same there is zero issue.

If, however, you want to be able to save any file regardless of original extension type, then you're going to have to split the extension from the filename and change it. This is made really easy with the "os" library.

At the top of your script underneath "From PSPApp import *" add this line "import os"

Code: Select all

from PSPApp import *
import os
Now anywhere inside the "def Do():" function add these lines (make sure the spacing matches, it's 4 spaces no tabs in front of each line. Python is very particular about whitespace.

Code: Select all

    # Check if Name is empty (new image).  If it is set the Title as the filename.  If it's not, set the Name as the filename
    filename = App.TargetDocument.Title if App.TargetDocument.Name == "" else App.TargetDocument.Name
    
    # Remove the extension
    filename, _ = os.path.splitext(filename)
    
    # Add new extension
    filename = filename + '.jpg'
Then change the FileName parameter to match the new filename variable we created.

Code: Select all

            'FileName': filename, 
Now, no matter what starting file type you use it'll always end in .jpg.
bobclevenger
Posts: 20
Joined: Mon Dec 21, 2020 4:09 am
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: Shuttle SH370R6 V2
processor: Intel Core i9
ram: 128 GB
Video Card: Onboard
sound_card: Onboard
Hard_Drive_Capacity: 2TB
Monitor/Display Make & Model: HP 27yh
Corel programs: Paint Shop Pro 2021
Location: Reno, Nevada

Re: PSP script to "Save Copy As:" maintaining file name

Post by bobclevenger »

Wow! A lot to absorb for a non-Python person, but I shall try this -- and thank you very much. This bit of code will save me a lot of time (if I do not make any misteaks (sic).
bobclevenger
Posts: 20
Joined: Mon Dec 21, 2020 4:09 am
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: Shuttle SH370R6 V2
processor: Intel Core i9
ram: 128 GB
Video Card: Onboard
sound_card: Onboard
Hard_Drive_Capacity: 2TB
Monitor/Display Make & Model: HP 27yh
Corel programs: Paint Shop Pro 2021
Location: Reno, Nevada

Re: PSP script to "Save Copy As:" maintaining file name

Post by bobclevenger »

OK, this works -- sorta. While the file name is maintained, the intended destination directory is not. I want the script to default to "C:\users\bob\pictures\wallpaper." that can be hard-coded as it will not change for a long time.
LeviFiction
Advisor
Posts: 6777
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: PSP script to "Save Copy As:" maintaining file name

Post by LeviFiction »

Then use App.TargetDocument.Title instead of Name. Name uses the full file path while Title only uses the name that shows up in the image title bar.
bobclevenger
Posts: 20
Joined: Mon Dec 21, 2020 4:09 am
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: Shuttle SH370R6 V2
processor: Intel Core i9
ram: 128 GB
Video Card: Onboard
sound_card: Onboard
Hard_Drive_Capacity: 2TB
Monitor/Display Make & Model: HP 27yh
Corel programs: Paint Shop Pro 2021
Location: Reno, Nevada

Re: PSP script to "Save Copy As:" maintaining file name

Post by bobclevenger »

When I try that I get the following message:
File "<string>", line 35
'FileFormat': App.Constants.FileFormat.JPG,
^
SyntaxError: invalid syntax
------- Command Execution Failed -----------
Command Name: RunScript
Error Text: The script could not be loaded.

Any ideas?
LeviFiction
Advisor
Posts: 6777
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: PSP script to "Save Copy As:" maintaining file name

Post by LeviFiction »

Did you make sure to have the comma at the end of the line?
bobclevenger
Posts: 20
Joined: Mon Dec 21, 2020 4:09 am
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: Shuttle SH370R6 V2
processor: Intel Core i9
ram: 128 GB
Video Card: Onboard
sound_card: Onboard
Hard_Drive_Capacity: 2TB
Monitor/Display Make & Model: HP 27yh
Corel programs: Paint Shop Pro 2021
Location: Reno, Nevada

Re: PSP script to "Save Copy As:" maintaining file name

Post by bobclevenger »

I fumbled around and found something that works.
Here is the script portion for "Save Copy As:"

# FileSaveCopyAs
App.Do( Environment, 'FileSaveCopyAs', {
'Encoding': {

'JPG': {
'Variant': App.Constants.JpegFormat.Standard,
'CompressionFactor': 6,
'ChromaSubSampling': App.Constants.ChromaSubSampling.YCC_1x1_1x1_1x1
,
'EXIF': True,
'EmbedJPGICC': True
},
},

'FileName': u'C:\\Users\\bob\\OneDrive\\Pictures\\Wallpaper\\u' +App.TargetDocument.Title,
'FileFormat': App.Constants.FileFormat.JPG,
'PluginFormat': 0,
'FormatDesc': u'JPG JPEG ',
'WorkingMode': 0,
'GeneralSettings': {
'ExecutionMode': App.Constants.ExecutionMode.Interactive,
'AutoActionMode': App.Constants.AutoActionMode.Match,
'Version': ((23,0,0),1)
},
'Plugin': {

},
'DefaultProperties': []
})
bobclevenger
Posts: 20
Joined: Mon Dec 21, 2020 4:09 am
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: Shuttle SH370R6 V2
processor: Intel Core i9
ram: 128 GB
Video Card: Onboard
sound_card: Onboard
Hard_Drive_Capacity: 2TB
Monitor/Display Make & Model: HP 27yh
Corel programs: Paint Shop Pro 2021
Location: Reno, Nevada

Re: PSP script to "Save Copy As:" maintaining file name

Post by bobclevenger »

Cleaned this up a bit.


# FileSaveCopyAs
App.Do( Environment, 'FileSaveCopyAs', {
'Encoding': {
'JPG': {
'Variant': App.Constants.JpegFormat.Standard,
'CompressionFactor': 6,
'ChromaSubSampling': App.Constants.ChromaSubSampling.YCC_1x1_1x1_1x1,
'EXIF': True,
'EmbedJPGICC': True
}
},
'FileName': u'C:\\Users\\bob\\Pictures\\Wallpaper\\u' +App.TargetDocument.Title,
'FileFormat': App.Constants.FileFormat.JPG,
'FormatDesc': u'JPG JPEG ',
'WorkingMode': 0,
'GeneralSettings': {
'ExecutionMode': App.Constants.ExecutionMode.Interactive,
'AutoActionMode': App.Constants.AutoActionMode.Match,
'Version': ((23,0,0),1)
},
'DefaultProperties': []
})
bobclevenger
Posts: 20
Joined: Mon Dec 21, 2020 4:09 am
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: Shuttle SH370R6 V2
processor: Intel Core i9
ram: 128 GB
Video Card: Onboard
sound_card: Onboard
Hard_Drive_Capacity: 2TB
Monitor/Display Make & Model: HP 27yh
Corel programs: Paint Shop Pro 2021
Location: Reno, Nevada

Re: [Sticky] -- Wishlist for PSP 2022

Post by bobclevenger »

Update: I found a workaround. I edited the script and deleted all lines that had specific data for the variables such as "+15 red" and the script now uses the last data used when run again. The only downside I have seen is that the <edit script> function opens up in text editor mode instead of the previous mode where you have check boxes for each step. I would like to see the original mode (check boxes for each step) open up in all cases, modified script or not.