scripting SaveAs filename

Moderator: Kathy_9

Post Reply
drTretired
Posts: 6
Joined: Mon Nov 19, 2018 11:16 pm
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
processor: I7 3770
ram: 32GB
Video Card: AMD RADEON
Hard_Drive_Capacity: 2TB
Monitor/Display Make & Model: Dell
Corel programs: psp 2019

scripting SaveAs filename

Post by drTretired »

I thought I had this working in the past but am having trouble. What I want to do is to screen capture a series of images and open them in Paintshop 2019. That works fine. The script I am trying to write then starts with the first image in the queue which has a working name of image1 with the following images labeled image2, image3 etc. The script is then to change the canvas size (to include the portion of the screen I am preparing as part of an animated GIF), then to SAVEAS to a selected and dedicated desktop folder (same always) in jpeg format using the 'image1', 'image2' etc, as the filename which is what the default is when one manually opens SAVEAS. Then CLOSE the particular image and move to the next.
All of this works EXCEPT the actual filename. The script tries to name each image file with the name of the image I used when recording the script. Again, it seems to me this worked in earlier PSP versions--but maybe not. Below is the part of the script in question.
# FileSaveAs
App.Do( Environment, 'FileSaveAs', {
'Encoding': {
'JPG': {
'Variant': App.Constants.JpegFormat.Standard,
'CompressionFactor': 20,
'ChromaSubSampling': App.Constants.ChromaSubSampling.YCC_1x1_1x1_1x1,
'EXIF': True,
'EmbedJPGICC': True
}
},
'FileName': u'C:\\Users\\Richard\\Desktop\\giftemp\\Image6.jpg',
'FileFormat': App.Constants.FileFormat.JPG,
'FormatDesc': u'JPG JPEG ',
'WorkingMode': 0,
'GeneralSettings': {
'ExecutionMode': App.Constants.ExecutionMode.Default,
'AutoActionMode': App.Constants.AutoActionMode.AllAlways,
'Version': ((21,0,0),1)
},
'DefaultProperties': []
})

My question: Is there a way to get this to use the name of the current image rather than the 'Image6' in the above. Perhaps a way to modify the above to do that?


Thanks,

rwt
LeviFiction
Advisor
Posts: 5984
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, 9, X, XI, X2, X8,X9, 2018, 2019
Location: USA

Re: scripting SaveAs filename

Post by LeviFiction »

Yes.

Apple.ActiveDocument.Name Will give you the filename. All you need to do is attach the path and extension. Like this

Code: Select all

  path =  u'C:\\Users\\Richard\\Desktop\\giftemp\\'
  extension = '.jpg'
  filename = path + App.ActiveDocument.Title + extension
Then simply put the filename variable in place of the full filename path.
drTretired
Posts: 6
Joined: Mon Nov 19, 2018 11:16 pm
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
processor: I7 3770
ram: 32GB
Video Card: AMD RADEON
Hard_Drive_Capacity: 2TB
Monitor/Display Make & Model: Dell
Corel programs: psp 2019

Re: scripting SaveAs filename

Post by drTretired »

The name I want transferred to the SAVEAS filename box is the name of the open image in PSP 2019. When you manually open SAVEAS while working on an image, the default filename will be the name given the working image and the filetype will be the last used. So when writing a script I had hoped that recording the 'keystrokes/mouse clicks' would have preserved that behavior. When run, the script opens the SAVEAS dialog but keeps using the filename that was present when the script was recorded. So--the filename I want the script to use is the image name of the current PSP image. When doing multiple screen captures that will be image1, image 2, image 3, etc. I don't know how large a script can be in PSP but may experiment with recording up to 100 image manipulations and saves. If that works I could process 100 images at a time--then using an online service create animated gifs for each 100 images and then merge the appropriate animated gifs into one larger one. Will try that tomorrow.
drTretired
Posts: 6
Joined: Mon Nov 19, 2018 11:16 pm
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
processor: I7 3770
ram: 32GB
Video Card: AMD RADEON
Hard_Drive_Capacity: 2TB
Monitor/Display Make & Model: Dell
Corel programs: psp 2019

Re: scripting SaveAs filename

Post by drTretired »

Just to wrap this up: I was able to write a script that repeated the same 5-step process 100 times (by brute force--recording all 100 sequences) and that works. This essentially solves my problem. I still don't know how to get the image name to automatically update into SAVEAS in a script even though this happens when you manually (or script it 100 times) access SAVEAS. The other complication I did run into, FYI, was that you can't end a script in the middle of the SAVEAS function. Tried to get a script to end after opening SAVEAS where I would simply have to press RETURN to save with the current image name. Can't do that.


rwt
LeviFiction
Advisor
Posts: 5984
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, 9, X, XI, X2, X8,X9, 2018, 2019
Location: USA

Re: scripting SaveAs filename

Post by LeviFiction »

My suggestion takes the name of the currently selected document in the editor and uses that name as the name if the filesaveas dialog. Exactly what you asked for.

As you never told us what problems you had using my suggestion, or if you even tried, I cannot determine what was wrong specifically. Though I can hazard a few guesses.

I'm also changing my suggestion slightly. Originally I told you to use the constant App.ActiveDocument. Now I'm changing that to App.TargetDocument.

Let's see if I can fix my error in presenting a solution. You don't need 100 separate scripts. In fact, a single script should be able to take all open images and perform the steps you want without issue.

A lot of the functionality in the UI of PSP is overruled by scripts. When you recorded your script it saved the filename you used when recording the script. And so when the script runs the script tells PSP that you want to use "this specific name" when the file save as is run and PSP takes that over its default. That is why the name does not default to Image1, Image2, etc.

To get past this we tell PSP exactly what name we want to use. In this case we use a built in constant called App.TargetDocument. This variable always points to the currently selected document that all actions will be performed on. Each document has a number of properties in this case we're going to use Title. This returns the same text that is shown in the Titlebar of the image. So this will return Image1, Image2, etc. Exactly like you want.

In my example I used variables variables to hold the path, extension, and filename separately. This time we're going to just put all three directly into the filename parameter of the command. Here is the code you posted originally

Code: Select all

    # FileSaveAs
    App.Do( Environment, 'FileSaveAs', {
                'Encoding': {
                    'JPG': {
                        'Variant': App.Constants.JpegFormat.Standard, 
                        'CompressionFactor': 20, 
                        'ChromaSubSampling': App.Constants.ChromaSubSampling.YCC_1x1_1x1_1x1, 
                        'EXIF': True, 
                        'EmbedJPGICC': True
                        }
                    }, 
                'FileName': u'C:\\Users\\Richard\\Desktop\\giftemp\\Image6.jpg', 
                'FileFormat': App.Constants.FileFormat.JPG, 
                'FormatDesc': u'JPG JPEG ', 
                'WorkingMode': 0, 
                'GeneralSettings': {
                    'ExecutionMode': App.Constants.ExecutionMode.Default, 
                    'AutoActionMode': App.Constants.AutoActionMode.AllAlways, 
                    'Version': ((21,0,0),1)
                    }, 
                'DefaultProperties': []
                })
So what we're looking at is the part that says 'FileName': u'C:\\Users\\Richard\\Desktop\\giftemp\\Image6.jpg',

We're going to change this to say 'FileName': u'C:\\Users\\Richard\\Desktop\\giftemp\\' + App.TargetDocument.Title + '.jpg',

The first part is the parameter name 'FileName' followed by a colon and then by the folder location, we then add App.TargetDocument.Title as this contains the image name "Image1", "Image2", "Image3", etc. Finally, we add the extension because the FileSaveAs command doesn't care if you have an extension or not. And we want to have an extension. Capitalization and punctuation are very important here as Python is case sensitive.

So, the new code looks like this

Code: Select all

    # FileSaveAs
    App.Do( Environment, 'FileSaveAs', {
                'Encoding': {
                    'JPG': {
                        'Variant': App.Constants.JpegFormat.Standard, 
                        'CompressionFactor': 20, 
                        'ChromaSubSampling': App.Constants.ChromaSubSampling.YCC_1x1_1x1_1x1, 
                        'EXIF': True, 
                        'EmbedJPGICC': True
                        }
                    }, 
                'FileName': u'C:\\Users\\Richard\\Desktop\\giftemp\\' + App.TargetDocument.Title + '.jpg', 
                'FileFormat': App.Constants.FileFormat.JPG, 
                'FormatDesc': u'JPG JPEG ', 
                'WorkingMode': 0, 
                'GeneralSettings': {
                    'ExecutionMode': App.Constants.ExecutionMode.Default, 
                    'AutoActionMode': App.Constants.AutoActionMode.AllAlways, 
                    'Version': ((21,0,0),1)
                    }, 
                'DefaultProperties': []
                })
This guarantees that PSP will always save in the giftemp folder on your desktop and will use whatever name is in the titlebar of the image you're attempting to save. This is exactly what you have asked for. This solves your problem. It allows a single script to save any image in the exact manner you requested.
drTretired
Posts: 6
Joined: Mon Nov 19, 2018 11:16 pm
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
processor: I7 3770
ram: 32GB
Video Card: AMD RADEON
Hard_Drive_Capacity: 2TB
Monitor/Display Make & Model: Dell
Corel programs: psp 2019

Re: scripting SaveAs filename

Post by drTretired »

I will try your new solution, but what I have done that is working is to record a script that handles 100 files at a time. Since I am limited by the GIF maker web site that I'm using as to how many files I can load at a time, this solution works pretty well.

I did try and implement your previous code, but since I am not familiar with Python I'm not sure I did it correctly. Anyway, the script wouldn't run after I modified it, so I'm sure it was me doing something wrong. Spent 30 plus years using True Basic and while I made a couple excursions into C, the simplicity but also the actual computing power of True Basic sufficed. (What I was doing didn't work any better or faster in C than in TB which were mostly animations based on data being computed as the animations were being created--some using photo-realistic graphics.) Current project is to turn many of the animations (used in Physics education) into animated Gifs which while they lose their interactive abilities can run in Power Points with automatic scaling and can run on both Windows and Mac platforms. True Basic initially ran across platforms too--PC, MAC, AMIGA, and others--but being very small and fundamentally supplying the core of the language for teaching computing, True Basic never was able to update their Mac version from system 9 to system 10. Still is a great language for learning the fundamentals of programming. True Basic is based on the Kemeny and Kurtz original BASIC language which is NOT your Apple II or Commodore Basic. It is a compiled language with all computations done double precision and is know for its speed with such computations. Just too bad they couldn't afford to keep up with the times--a few good library routines especially to deal with the internet, might have kept it relevant for people outside the academic world of teaching!

I will try but having spent all afternoon working on one animation where the GIF software changed the size of the images in one of the three sets of files I was trying to combine--for reasons unknown, I need a drink rather than a tussle with unfamiliar code. I will try though, and thanks.

rwt
drTretired
Posts: 6
Joined: Mon Nov 19, 2018 11:16 pm
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
processor: I7 3770
ram: 32GB
Video Card: AMD RADEON
Hard_Drive_Capacity: 2TB
Monitor/Display Make & Model: Dell
Corel programs: psp 2019

Re: scripting SaveAs filename

Post by drTretired »

OK, your new FileSave section works. Below is the full code. It turns out however, that my 'brute force script that recorded doing the below 100 times has one big advantage. I click to run the script ONCE and it runs through 100 images with no more intervention. If there are less than 100 images, there is an error generated, but all the images that were there have been processed.

I am going to assume that there is a way to get the below to loop and run continuously (until running out of images). If this were in a language I knew, I would just put the whole thing in a do loop. Is there an easy way to do this in Python that would run in PSP--one click and all open images are processed?


Also--not essential--how do you enter code in this forum as you have done--maintain formatting in a separate window?


Anyway...thanks again.

rwt

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

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

# ResizeCanvas
App.Do( Environment, 'ResizeCanvas', {
'AspectRatio': 1.6,
'FillColor': (255,255,255),
'HoriPlace': App.Constants.HorizontalType.Left,
'MaintainAspect': False,
'NewDimUnits': App.Constants.UnitsOfMeasure.Pixels,
'NewHeight': 1153,
'NewWidth': 1538,
'PlaceBottom': -47,
'PlaceLeft': 0,
'PlaceRight': -382,
'PlaceTop': 0,
'VertPlace': App.Constants.VerticalType.Top,
'GeneralSettings': {
'ExecutionMode': App.Constants.ExecutionMode.Default,
'AutoActionMode': App.Constants.AutoActionMode.Match,
'Version': ((21,0,0),1)
}
})

# FileSaveAs
App.Do( Environment, 'FileSaveAs', {
'Encoding': {
'JPG': {
'Variant': App.Constants.JpegFormat.Standard,
'CompressionFactor': 20,
'ChromaSubSampling': App.Constants.ChromaSubSampling.YCC_1x1_1x1_1x1,
'EXIF': True,
'EmbedJPGICC': True
}
},
'FileName': u'C:\\Users\\Richard\\Desktop\\giftemp\\' + App.TargetDocument.Title + '.jpg',
'FileFormat': App.Constants.FileFormat.JPG,
'FormatDesc': u'JPG JPEG ',
'WorkingMode': 0,
'GeneralSettings': {
'ExecutionMode': App.Constants.ExecutionMode.Default,
'AutoActionMode': App.Constants.AutoActionMode.AllAlways,
'Version': ((21,0,0),1)
},
'DefaultProperties': []
})

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

# SelectDocument
App.Do( Environment, 'SelectDocument', {
'SelectedImage': 0,
'Strict': False,
'GeneralSettings': {
'ExecutionMode': App.Constants.ExecutionMode.Default,
'AutoActionMode': App.Constants.AutoActionMode.Match,
'Version': ((21,0,0),1)
}
})
LeviFiction
Advisor
Posts: 5984
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, 9, X, XI, X2, X8,X9, 2018, 2019
Location: USA

Re: scripting SaveAs filename

Post by LeviFiction »

There is an easy way. Python makes this very easy. I will give a better response after work. But in PSP all open documents are held in a variable called App.Documents. in Python this is a list but in Liberty Basic it would be called an array. (Full disclosure I haven't used Liberty Basic in 14 years so I don't remember a lot about it)

In Python you can use a for loop to loop through the whole list without the need for a counter variable.

Code: Select all

for doc in App.Documents:
    #perform commands here
That will loop over all open images in PSP.
LeviFiction
Advisor
Posts: 5984
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, 9, X, XI, X2, X8,X9, 2018, 2019
Location: USA

Re: scripting SaveAs filename

Post by LeviFiction »

Alright I've uploaded a completed script here so if you're not interested in how it works you can just download it and be on your way. But if you're curious as to why it's written the way it is continue reading.
SaveAllToFolder.zip
(925 Bytes) Downloaded 267 times
Okay, so going off of my previous post. We'll be using the for loop to grab each document from the App.Documents list and performing actions on it. Also there is an obscure optional parameter for every "App.Do()" command, you can actually give a document to the command and it'll perform that action on the document you pass in. Using these two things together will give us what we want.

A "for loop" in Python is like the "ForEach" loop in other languages. It iterates over a list/array of items and for each item in that list it returns a single item per loop. When we pass in the App.Documents list the "for loop" will grab the first document in the list, perform our actions, and then grab the next document and so on until it reaches the end of the list. During each loop the document from the list is put into a variable we define. In this case I've called it "doc" and I use this document to grab the Title for the file save and to point each command at the correct document. In this case App.TargetDocument could work just fine but I wanted to be absolutely sure there wouldn't be any hidden or unforseen issues. So my method here may be completely unnecessary but still it works.

Code: Select all

for doc in App.Documents:
    App.Do( Environment, 'Resize', {}, doc)
In this example I've left out all of the parameters of the resize command so you can see where I put the doc variable. It's just after the {} parameter dictionary and just before the closing ")". Here is the full code you will see I've put the resize, save, and close commands under the For loop and indented them. I also added "doc" to the end of each command and I've replaced "App.TargetDocument.Title" with just "doc.Title"

Code: Select all

from JascApp import *

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

def Do(Environment):
    # EnableOptimizedScriptUndo
    App.Do( Environment, 'EnableOptimizedScriptUndo', {
        'GeneralSettings': {
            'ExecutionMode': App.Constants.ExecutionMode.Default,
            'AutoActionMode': App.Constants.AutoActionMode.Match,
            'Version': ((21,0,0),1)
            }
        })
    for doc in App.Documents:
        # ResizeCanvas
        App.Do( Environment, 'ResizeCanvas', {
            'AspectRatio': 1.6,
            'FillColor': (255,255,255),
            'HoriPlace': App.Constants.HorizontalType.Left,
            'MaintainAspect': False,
            'NewDimUnits': App.Constants.UnitsOfMeasure.Pixels,
            'NewHeight': 1153,
            'NewWidth': 1538,
            'PlaceBottom': -47,
            'PlaceLeft': 0,
            'PlaceRight': -382,
            'PlaceTop': 0,
            'VertPlace': App.Constants.VerticalType.Top,
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Default,
                'AutoActionMode': App.Constants.AutoActionMode.Match,
                'Version': ((21,0,0),1)
                }
            }, doc)

        # FileSaveAs
        App.Do( Environment, 'FileSaveAs', {
            'Encoding': {
                'JPG': {
                    'Variant': App.Constants.JpegFormat.Standard,
                    'CompressionFactor': 20,
                    'ChromaSubSampling': App.Constants.ChromaSubSampling.YCC_1x1_1x1_1x1,
                    'EXIF': True,
                    'EmbedJPGICC': True
                    }
                },
            'FileName': u'C:\\Users\\Richard\\Desktop\\giftemp\\' + doc.Title + '.jpg',
            'FileFormat': App.Constants.FileFormat.JPG,
            'FormatDesc': u'JPG JPEG ',
            'WorkingMode': 0,
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Default,
                'AutoActionMode': App.Constants.AutoActionMode.AllAlways,
                'Version': ((21,0,0),1)
                },
            'DefaultProperties': []
            }, doc)

        # FileClose
        App.Do( Environment, 'FileClose', {
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Silent,
                'AutoActionMode': App.Constants.AutoActionMode.Match,
                'Version': ((21,0,0),1)
                }
            }, doc)
drTretired
Posts: 6
Joined: Mon Nov 19, 2018 11:16 pm
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
processor: I7 3770
ram: 32GB
Video Card: AMD RADEON
Hard_Drive_Capacity: 2TB
Monitor/Display Make & Model: Dell
Corel programs: psp 2019

Re: scripting SaveAs filename

Post by drTretired »

GREAT--this works fine and will be a little more convenient than my brute force 100 file script in that I often have more than 100 images open. I also know that if I want to do any more programming in the future I really need to learn one of the newer languages. Much of my work has been doing physics animations. I have a whole series of lab simulations often using photo-realistic graphics that simulate what is actually done in the labs. I've been doing this stuff since my first Commodore-64 and for many, many years the challenge was to speed up the animations as the programs were calculating the position of objects for the next frame. In later years, the challenge became to slow thing down until lately the speed of computer became such that I could build an entire screen with all object in place (on a hidden screen) and then copy that screen to a visible one all in a 30th of a second. True Basic actually could do this as fast as C. Now though, to modernize, I would need to write so that applications could run on a web page. At 72, not sure I'm ready for that much learning and work, especially as my summers are spent now on the golf course.

Anyway, thanks so much. I like your more elegant solutions and may look into Python as a possible language for future use (gave up on C++ a few years ago).

RICHARD TARARA
PROFESSOR EMERITUS
SAINT MARY'S COLLEGE

NEW:FREE SELF-PACED COMPUTER BASED
COURSES FOR WINDOWS OR MAC
ENERGY FOR THE 21ST CENTURY
THE PHYSICS OF MOTION
THESE AND OTHER FREE PHYSICS INSTRUCTIONAL SOFTWARE AT:
http://sites.saintmarys.edu/~rtarara/software.html
jlarysz
Posts: 2
Joined: Sat Mar 30, 2019 4:51 pm
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: Asus Z170-A
processor: Intel i7 6700K
ram: 16GB
Video Card: Radeon RX 580
sound_card: On Motherboard
Hard_Drive_Capacity: Very large
Monitor/Display Make & Model: HP 24o
Corel programs: CorelDraw X7, Paintshop Pro X7

Re: scripting SaveAs filename

Post by jlarysz »

I apologize if this suggestion is crude; I am new to Corel scripting and Python.

I want to store 'Save As' files in the directory that the original file came from, and that could be any one of a large number of directories. My original files are in TIFF format and I'm saving in JPEG format. I created a script to do the job on one file using Corel's 'Script Record' feature and then generalized the code as follows:

1: My version of PaintshopPro (the 2019 version) uses Python 2.7.5. This is an old version of Python that does not include the 'os' module in its core. I forced that module in to Corel by downloading a tarball of Python 2.7.5 onto my Windows 10 machine, expanding in (but not installing it), and then copying the library file

Code: Select all

 \Python\Python-2.7.5\Lib\os.py
from the downloaded Python to the same place in the Corel Python file structure.

2: Restart Paintshop pro

3: Add the instruction "import os.path" as the second line of the script

Code: Select all

from PSPApp import *
import os.path
4: Define the new file name as:

Code: Select all

            'FileName': os.path.dirname(App.TargetDocument.Name) + '\\' +  os.path.splitext(App.TargetDocument.Title)[0] + '.jpg', 
5: Here is my complete script

Code: Select all

from PSPApp import *
import os.path

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

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

    # FileSaveAs
    App.Do( Environment, 'FileSaveAs', {
            'Encoding': {
                'JPG': {
                    'Variant': App.Constants.JpegFormat.Standard, 
                    'CompressionFactor': 1, 
                    'ChromaSubSampling': App.Constants.ChromaSubSampling.YCC_1x1_1x1_1x1, 
                    'EXIF': True, 
                    'EmbedJPGICC': True
                    }
                }, 
            'FileName': os.path.dirname(App.TargetDocument.Name) + '\\' +  os.path.splitext(App.TargetDocument.Title)[0] + '.jpg', 
            'FileFormat': App.Constants.FileFormat.JPG, 
            'FormatDesc': u'JPG JPEG ', 
            'WorkingMode': 0, 
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Default, 
                'AutoActionMode': App.Constants.AutoActionMode.AllAlways, 
                'Version': ((21,0,0),1)
                }, 
            'DefaultProperties': []
            })

LeviFiction
Advisor
Posts: 5984
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, 9, X, XI, X2, X8,X9, 2018, 2019
Location: USA

Re: scripting SaveAs filename

Post by LeviFiction »

So, two things.

1) PSP does include the os module with its install of Python. It is part of the standard library set. PSP comes with the full standard library including TKInter for GuI development. So if you didn't have the os module, sounds like an issue with your install. So your first step is unnecessary for almost everyone.

In fact I'm fairly certain os was included almost right from the beginning when PSP first started using Python as its scripting language. This was back in PSP8 with python version 2.2.

2) Your method isn't crude, and it works well for images that are already open as opposed to ones that were created inside the program as they won't have a Name property. However I would make the suggestion that you could greatly simplify it by only using splitext and not dirname.

App.TargetDocument.Name includes the full path of the file plus the filename. The command dirname is great when you only want the directory, however, here you want the directory and the filename minus the extension. splitext splits the filename into two strings at the beginning of the extension. So you only need that one command to get your filename and directory without the extension.

Code: Select all

"FileName": os.path.splitext(App.TargetDocument.Name)[0] + ".jpg',
There is another option that doesn't require the os module at all. There is a function built into PSP called "rfind" this searches for a substring starting at the right (or end) of the string until it finds the substring. Then once you have an index, and that index is greater than -1, you know you have found the start of the extension. You can then use slices to separate the extension from the rest of the filename.

The actual splitext is a bit more invovled but the function may be given a folder name or a file that has a leading period but no real extension. So they have to account for all of that.

I'm also using python's equivalent to a ternary operator. So, this will assign a new filename if there is an extension, if not it'll just use the old filename.

Code: Select all

extStart = App.TargetDocument.Name.rfind('.')
newFilename = App.TargetDocument.Name[:extStart] + ".jpg" if extStart > -1 else App.TargetDocument.Name

... #Skipping over lines to get to the FileSaveAs parameters
     'FileName': newFilename,
Post Reply