Interesting PSP 8 v 2018 distinction?

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

Interesting PSP 8 v 2018 distinction?

Post by terrypin »

@LeviFiction,

I didn't see this helpful thread
http://forum.corel.com/EN/viewtopic.php?f=104&t=62793
until now. Just been stepping through it for further learning.

The only edit I made was changing your script's first line from

Code: Select all

from PSPApp import *
to

Code: Select all

from JascApp import *
When I run it in PSP 8 (on an arbitrary JPG) it fails with:

Traceback (most recent call last):
File "C:\Users\terry\My PSP8 Files\Scripts-Trusted\Levi-2.PspScript", line 15, in Do
path = App.TargetDocument.Name.rstrip(App.TargetDocument.Title)
TypeError: rstrip() takes no arguments (1 given)

When I run it in PSP 2018 (my inauguration script!) it works sweetly.

But when I change it with the aim of adding a suffix '-PS' instead of the prefix 'INTERNET_'

Code: Select all

def Do(Environment):
    # This attempts in vain to adds the suffix '-PS' to the target's filename
    ######################################################################
    path = App.TargetDocument.Name.rstrip(App.TargetDocument.Title)
    filename = path + App.TargetDocument.Title + "-PS"
    ######################################################################
it fails to add the suffix, but throws no errors.
--
Terry, East Grinstead, UK
Using PSP 8 & PSP 2018 under Win 10
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: Interesting PSP 8 v 2018 distinction?

Post by JoeB »

I'd help except I don't have a clue why 2018 runs but doesn't throw an error when you add the -ps to the title. I can only assume it's not allowed after the title variable, but you'll have to wait until LeviFiction sees your post to get a good answer and perhaps the additional info as to how to add to the end of the file name (before the extension) rather than just to the beginning.
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: Interesting PSP 8 v 2018 distinction?

Post by LeviFiction »

PSP8 uses an older form of Python. PSP 8 uses Python2.2 while Python 2018 uses Python 2.7. There will be differences in how the languages work. rstrip() trims away white space from the right-side of the string in 2.2. In 2.7 it has an additional function that let's you strip a very specific set of characters.

However, the code you've chosen won't do what you're hoping it will as the code was specifically designed for prefixes not suffixes. In the end it's a few extra steps to get the same result as simply addding "-PS" to the end of the filename. Let me show you what I mean.

Here's what's happening. In PSP App.TargetDocument.Name = the full path of the file. App.TargetDocument.Title will only equal the filename of currently opened file. If it's a new image then Name is blank as there is no path, an Title is just Image#. To simplify here's an example.

Code: Select all

Name = "C:\folder\file.jpg"
Title = "file.jpg"
rstrip in Python 2.7 lets you identify which characters to remove from the right-hand side of the string. In our case we're telling Python to remove "file.jpg" from the string "C:\folder\file.jpg"

Code: Select all

path = Name.rstrip(Title)
Path now equals "C:\folder\" So when we add the title back in, it's like we've done nothing at all. We just removed "file.jpg" from the string only to add it back in un-altered.

Code: Select all

filename = Path + Title + "-PS"
filename is now "C:\folder\file.jpg-PS" Which in my test is exactly how the file saves. I had a file named "C:\users\levifiction\desktop\godzilla.jpg" and after all of the code executed the resulting filename was "C:\users\levifiction\desktop\godzilla.jpg-PS" and I found that file on my desktop.

So you've done a whole lot of extra work to simply add "-PS" to the end of the filename. It would have been easier to just do

Code: Select all

filename = Name + "-PS"
Agan, the script you've taken from was never meant for suffixes.

What you need to do is separate the filename from the extension. There are several ways to handle this. If you can guarantee that the extension is always 4 characters ".jpg" you can easily strip them out using what are known as slices. A basic part of Python. Slices work like this you take your varialbe that contains either a list or a string an you identify the indexes for the beginning and ending items you want to retrieve. Each letter in a string has its own index. It looks like this

variable[start:end]

For example

Code: Select all

lengthOfFilename = len(App.TargetDocument.Name)
filename = App.TargetDocument.Name[0:lengthOfFilename - 4]
What this does is it grabs all of the letters in the filename except for the last 4. Start 0 is the first letter. len() tells me how many letters are in a string. I subtract 4 from that to get the end point. Now I can add any suffixes and extensions that I want.

Code: Select all

filename = filename + "-PS.bmp"
Using my godzilla example above the end result would be "c:\uses\levifiction\desktop\godzilla-PS.bmp"

Again, though, this only works if you know you'll never have a TIFF or JPEG extension in the filename as those will require you remove 5 letters and not 4.

I can also do this another way. This is even less intuitive than what I've already shown.

Code: Select all

extension = App.TargetDocument.Name[-4:]
filename = App.TargetDocument.Name.rstrip(extension) + "-PS.bmp"
What this does is it sets the splice to start 4 characters from the end of the string. So right at the "." an then to select everything after that. Leaving the second parameter of the splice blank defaults to "the rest of it." That leaves me with just the extension. I then use rstrip to remove the extension from the right-hand side of the filename. And I add in the suffix an extension to the end.

Same problem as before, knowing whether or not to remove 4 characters or 5.

To deal with that problem we can use a much easier command "rsplit." What rsplit does is it looks for a character that you specify for example a period "." and it separates the string at that point. Very specifically rsplit does this by looking at the the string from right to left. Working backwards. And we tell rsplit to limit its splits to just 1 split. So it'll find the first instance of a period, create the split, and the rest is up to us.

Code: Select all

filename, extension = App.TargetDocument.Name.rsplit(".", 1)
filename = filename + "-PS.bmp" 
This has some potential weaknesses as well, but for the most part should get you where you want to go with minimal trouble.
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: Interesting PSP 8 v 2018 distinction?

Post by terrypin »

Thanks a bunch, Levi, really appreciate your taking the time to explain that in such clear detail.

I’m in the obscure depths of an Excel VBA problem right now (a subject in which I’m also a novice) but I’ll reproduce your exercise asap and then implement in my project.
--
Terry, East Grinstead, UK
Using PSP 8 & PSP 2018 under Win 10
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: Interesting PSP 8 v 2018 distinction?

Post by JoeB »

I also appreciate the detailed explanation. You should write a book, because one with such nicely detailed examples for "almost" beginners with PSP scripts, but with some experience with PSP scripts and recording and copy/pasting, would likely be well received!
Regards,

JoeB
Using PSP 2019 64bit
Post Reply