Need script to "Save As" all open images

Moderator: Kathy_9

Post Reply
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

Need script to "Save As" all open images

Post by JoeB »

Need help from scripting gurus on this one. I open multiple images (jpg format usually) in the workspace and crop, edit, layers, etc. Then I want to either Save As or Save Copy As to be able to save ALL of those open images in pspimage format without having to save each one individually. I save in pspimage format so I can go back later for more editing.

So the script should likely work in a way that will allow it to open the dialogue where I can preferably select pspimage as the save format and select any needed options plus the output folder (which could be a different one for different projects), then run the script again so that it automatically saves all of the images that are in the workspace according to those instructions. It is fine if the existing file names remain as they are.

I can record a simple script that will work if I run it on each individually open image, particularly if I set the PSP image saving dialogue to my preferred options in advance, but there is no way in PSP to select ALL images in the workspace so that the script will apply to all of them once started without my intervention with each open image. Batch Process doesn't work because it only works with files already in folders and these images, as edited, are only in the workspace.

Any suggestions would be most appreciated!
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: Need script to "Save As" all open images

Post by LeviFiction »

It would take me about 30 minutes to test everything. But here is the basic idea in my head.

1) Grab active document into variable

2) Run Save As command on this document.

3) Get the last used parameters from Save As.

4) Use For Each loop to loop through the App.Documents list

5) For each document,
5) a) check if document matches active document skip if true
5) b) if false Grab document name and convert to appropriate file extension
5) c) Remove path if you want all saves to go to the same folder, or leave the path to save to original folder for each image
5) d) calls Save As silently.

So it would loosely look something like this.

Code: Select all

def Do(Environment):
    Doc = App.ActiveDocument
    App.Do(Environment, 'FileSaveAs', {}, Doc)   #Last parameter defines the document you want the command to work on so you can work on any document even if it's no active
    Params = App.Do(Environment, 'GetCmdInfo', {})  #Include necessary parameters to get last used params
    for doc in App.Documents:
        drive, path = os.path.splitdrive(doc.Name)
        filename, ext = os.path.splitext(path)
        filename = filename+".pspimage"
        App.Do(Environment, 'FileSaveAs', {#params from Params, 'GeneralSettings':{#run mode silent}}, doc)
https://levifiction.wordpress.com/
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: Need script to "Save As" all open images

Post by JoeB »

Thanks for that, LeviFiction. I've copied what you've done and saved it. I have a present project (actually a couple) that will take me a few days then out of town for almost a week so don't know when I'll actually be able to start working with it and tweaking (or more probably asking for more help! :-) ), but will get on it as soon as I can. I'll keep checking back on this thread regularly, at least until I go away, just to see if there is any more action. Thanks again!
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: Need script to "Save As" all open images

Post by LeviFiction »

Just making sure, but you know that's pseudo code right? It won't actually work as is.

Okay so here is a semi-working version of the code

Current limitations: 1) It will only work if you save to PSPImage format. That's hardcoded. 2) It will always save to the exact same file location as the first

My attempts to make the file format more automatic failed. PSP always complained about an invalid setting when I did that.

Code: Select all

from PSPApp import *
import os

def ScriptProperties():
    return {
        'Author': u'LeviFiction',
        'Copyright': u'',
        'Description': u'Save All Images to same folder with same parameters',
        'Host': u'PaintShop Pro',
        'Host Version': u'15.00'
        }

def Do(Environment):
    Doc = App.ActiveDocument
    firstname = Doc.Title
    if Doc.Name != "":
        fistname = Doc.Name
    App.Do(Environment, 'FileSaveAs', {'FileName': firstname,'GeneralSettings':{'ExecutionMode':App.Constants.ExecutionMode.Interactive}}, Doc)
    # now call GetCommandInfo to extract the last used parameters.
    Info = App.Do(Environment,  'GetCommandInfo', {
                'TargetCmd': 'FileSaveAs',
                'ParamInfo': App.Constants.ParamInfo.LastUsed
                } )
    
    # command parameters are stored in a key consisting of library name and command name
    CmdParam = Info[ 'Library' ] + '\\' + Info['Name']
    Params = Info[CmdParam] # get a simpler dictionary to work with
    
    for doc in App.Documents:
        if doc.Name == Doc.Name and doc.Title == Doc.Title:
            continue

        name = doc.Title
        if doc.Name != "":
            name = doc.Name

        path, filename = os.path.split(name)
        filename, ext = os.path.splitext(filename)
        filename = filename+".pspimage"
        App.Do(Environment, 'FileSaveAs', {
            'Encoding':Params['Encoding'],
            'FileName': Params['FileFolder']+filename,
            'FileFormat': App.Constants.FileFormat.PSP,
            'PluginFormat': Params['PluginFormat'],
            'FormatDesc':  Params['FormatDesc'],
            'GeneralSettings':{'ExecutionMode':App.Constants.ExecutionMode.Silent}
            }, doc)
            
https://levifiction.wordpress.com/
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: Need script to "Save As" all open images

Post by JoeB »

LeviFiction wrote:Just making sure, but you know that's pseudo code right? It won't actually work as is.

Okay so here is a semi-working version of the code

Current limitations: 1) It will only work if you save to PSPImage format. That's hardcoded. 2) It will always save to the exact same file location as the first

My attempts to make the file format more automatic failed. PSP always complained about an invalid setting when I did that.
Thanks again, LeviFiction! Yes, I did realize that what you originally provided wasn't useable as a script but just an outline to build on. But I really appreciate this code post as it will definitely make it easier for me to tweak it to try to overcome its limitations as you set them out. And yes, for my purpose saving as pspimage is what I want anyway because my initial goal is to save lossless and with all layers intact for future editing. I'm sure future work with the script will find ways to make it somewhat more versatile for different situations, but that's for the future and anyone (including myself) who has the time and/or inclination to do the tweaks.

I really appreciate your contributions on this. Hopefully they will also be of some interest to others in some way!
Regards,

JoeB
Using PSP 2019 64bit
Post Reply