How to use current filename for Save As in a Script?

Moderator: Kathy_9

Post Reply
stanektool
Posts: 3
Joined: Tue Jun 26, 2012 5:16 pm
operating_system: Windows 7 Professional
System_Drive: C
32bit or 64bit: 64 Bit

How to use current filename for Save As in a Script?

Post by stanektool »

I am using Paintshop Pro X2. I've created a script that goes something like this.

1. Save original size as jpg in folder 1.
2. Resize 50%
3. Save as jpg in folder 2.
4. Resize 100x77
5. Save as jpg in folder 3.
6. Close file.

Everything works great except for one thing. During the save as operations the script remembers the original file names from when I record the script. Is it possible to replace that old static file name in the script with the file name of the current image?

-Craig
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: How to use current filename for Save As in a Script?

Post by LeviFiction »

Yes it is.

Just open the script in a text editor like Notepad, or Notepad++, whatever you're comfortable with.

In the script you'll probably see an area similar to this

Code: Select all

    App.Do( Environment, 'FileSaveAs', {
            'Encoding': {
                'PSP': {
                    'Compression': App.Constants.PspCompression.LZ77, 
                    'Version': App.Constants.PspVersion.PSP14, 
                    'EmbedICC': False
                    }
                }, 
            'FileName': u'C:\\Users\\usrname\\Pictures\\test.pspimage', 
            'FileFormat': App.Constants.FileFormat.PSP, 
            'FormatDesc': u'PSPIMAGE', 
            'WorkingMode': 0, 
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Default, 
                'AutoActionMode': App.Constants.AutoActionMode.AllAlways, 
                'Version': ((14,0,0),1)
                }, 
            'DefaultProperties': []
            })

This was recorded in X4 but most of it should be exactly the same for X2. And I'm saving to PSPImage so the parts regarding the file format encoding and file type will be different from yours.

You see the parameter for 'FileName' in there.

Code: Select all

            'FileName': u'C:\\Users\\usrname\\Pictures\\test.pspimage', 
You'll just be replacing that with the following line

Code: Select all

           'FileName': App.TargetDocument.Name, 
"App.TargetDocument.Name" returns the current name and directory of the document that was active when the script started running.

Save it into your Scripts-Trusted folder (mine gave an error when in the Scripts-Restricted folder I'm assuming it'll do the same for you). Now when you run the script it'll use the image's original name in the Save AS dialog.

Of course this is slightly dangerous as you will be saving over the original file if you just hit "enter" right away when the box comes up.

If you just want the name and want to specify the directory yourself you can use "App.TargetDocument.Title" which only returns the actual file name and not the directory. So

Code: Select all

            'FileName': u'C:\\Users\\usrname\\Pictures\\' + App.TargetDocument.Title, 
Will save in the Pictures directory and use the name provided in the title bar of the open image.
https://levifiction.wordpress.com/
stanektool
Posts: 3
Joined: Tue Jun 26, 2012 5:16 pm
operating_system: Windows 7 Professional
System_Drive: C
32bit or 64bit: 64 Bit

Re: How to use current filename for Save As in a Script?

Post by stanektool »

Thank you very much. That gets me close. It's doing everything correct except for one thing. App.TargetDocument.Title includes the file extension. So instead of saving "file.jpg" it's saving "file.pspimage" even though the format of the saved file is jpeg. Is there any way to tell it to use the image title up until the . then use .jpg instead of .pspimage?
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: How to use current filename for Save As in a Script?

Post by LeviFiction »

Yes there is. But that starts getting into the python programming language. If you aren't familiar with python at all I suggest you look at some of the tutorials online. Python is a bit finicky with it's spacing so it's always a good idea to get the basics down.

With python there are a number of built in functions that allow you to extract portions of text based on certain criteria. I don't know what these are off hand because I rarely do text editing in python.

The other option is to import the "os" library and use it's path.split() function to easily split the filename from the extension.

I'm very poor at explaining these things.

At the top of the script you should see something like this

Code: Select all

from PSPApp import *
Go ahead and add this line right underneath it

Code: Select all

import os
That will import the OS library into python.

Then before you use the FileSaveAs command you'll want to enter something like this. Keep in mind that Python uses indentation (spaces) to determine if a command belongs inside of a portion of the program. It's the reason all of the "App.Do" commands are always indented 4 spaces. This is so that Python knows they belong inside the main body of the script. If you use too many or too few spaces it'll throw an error and cause you headaches.

Code: Select all

    filename,ext = os.path.split(App.TargetDocument.Title)
That will go ahead and split the text between the name and the extension. The file name is placed inside the "filename" variable.

Then replace the "App.TargetDocument.Title" portion of the SaveAs command with "filename" and that should do it.

Code: Select all

        'FileName': filename,
Again, there are plenty of ways to handle this without the OS library. This is just the easiest method that I know of. And again, this script, because it uses an external library, will require that you place it in the Scripts-trusted folder.

Sorry if I didn't explain that very well.
https://levifiction.wordpress.com/
Post Reply