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"
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.
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
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.
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.