Script to add text from clipboard

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

Script to add text from clipboard

Post by terrypin »

I'm not a script coder so maybe what I'm asking is outside the scope of Python?

In PSP 8 I recorded a trivial script (pasted in full below) to add some arbitrary text and centre it horizontally. (Despite its simplicity it generated an astonishing amount of code!) It works as expected.

But can anyone show me how to edit this so that instead of that specific text string it will add whatever text is on the Windows clipboard please?

Code: Select all

# This script was recorded. I simply added some arbitary text 'My added text' and horizontally centered it.
# Font size seems to be specified in lines 44 and 82
# And text string specified in line 95; its position appears to be in lines 46 & 84, although I don't know how to relate the numbers '820.5,220.5' to the actual position I see in the image.
from JascApp import *

def ScriptProperties():
    return {
        'Author': u'',
        'Copyright': u'',
        'Description': u'',
        'Host': u'Paint Shop Pro',
        'Host Version': u'8.10'
        }

def Do(Environment):
    # EnableOptimizedScriptUndo
    App.Do( Environment, 'EnableOptimizedScriptUndo', {
            
            })

    # Text
    App.Do( Environment, 'Text', {
            'CreateAs': App.Constants.CreateAs.Vector, 
            'Segments': [{
                'Bold': App.Constants.Boolean.true, 
                'Fill': {
                    'Color': (0,0,0), 
                    'Pattern': None, 
                    'Gradient': None, 
                    'Texture': None, 
                    'Identity': u'Material'
                    }, 
                'Font': u'Arial', 
                'LineStyle': {
                    'Name': u'', 
                    'FirstCap': (u'Butt',0.25,0.25), 
                    'LastCap': (u'Butt',0.25,0.25), 
                    'FirstSegCap': (u'',0.25), 
                    'LastSegCap': (u'',0.25), 
                    'UseSegmentCaps': App.Constants.Boolean.false, 
                    'Segments': []
                    }, 
                'LineWidth': 0, 
                'PointSize': 20,     # Font size specified here (and also in line 82) 
                'SetText': App.Constants.Justify.Center, 
                'Start': (820.5,220.5),     # Text position seems to be set here. Oddly with pixels that are not simple integers, but always with the decimal '0.5'. Also line 84?
                'Stroke': {
                    'Color': (255,255,255), 
                    'Pattern': None, 
                    'Gradient': None, 
                    'Texture': None, 
                    'Identity': u'Material'
                    }
                },{
                'Antialias': App.Constants.Boolean.true, 
                'WarpText': App.Constants.Boolean.true, 
                'AutoKern': App.Constants.Boolean.true, 
                'Bold': App.Constants.Boolean.true, 
                'Kerning': 0, 
                'Leading': 0, 
                'Fill': {
                    'Color': (0,0,0), 
                    'Pattern': None, 
                    'Gradient': None, 
                    'Texture': None, 
                    'Identity': u'Material'
                    }, 
                'Font': u'Arial', 
                'Italic': App.Constants.Boolean.false, 
                'Join': App.Constants.JointStyle.Miter, 
                'LineStyle': {
                    'Name': u'', 
                    'FirstCap': (u'Butt',0.25,0.25), 
                    'LastCap': (u'Butt',0.25,0.25), 
                    'FirstSegCap': (u'',0.25), 
                    'LastSegCap': (u'',0.25), 
                    'UseSegmentCaps': App.Constants.Boolean.false, 
                    'Segments': []
                    }, 
                'LineWidth': 0, 
                'MiterLimit': 10, 
                'PointSize': 20,     # Font size specified here (and also in line 44)
                'SetText': App.Constants.Justify.Center, 
                'Start': (820.5,220.5),     # LH end of text position?
                'Strikethru': App.Constants.Boolean.false, 
                'Stroke': {
                    'Color': (255,255,255), 
                    'Pattern': None, 
                    'Gradient': None, 
                    'Texture': None, 
                    'Identity': u'Material'
                    }, 
                'Underline': App.Constants.Boolean.false
                },{
                'Characters': u'My added text'     # Text is added here
                }], 
            'SavedText': None, 
            'FinalApply': App.Constants.Boolean.false, 
            'Matrix': None, 
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Default, 
                'AutoActionMode': App.Constants.AutoActionMode.Match
                }
            })

    # VectorAlignHCenterInCanvas
    App.Do( Environment, 'VectorAlignHCenterInCanvas', {
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Default, 
                'AutoActionMode': App.Constants.AutoActionMode.Match
                }
            })

--
Terry, East Grinstead, UK
--
Terry, East Grinstead, UK
Using PSP 8 & PSP 2018 under Win 10
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: Script to add text from clipboard

Post by LeviFiction »

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: Script to add text from clipboard

Post by terrypin »

Yes and no! As mentioned in that thread "I have meanwhile been handling the text aspects of my project using my macro approach, although it was hard work."

I used a mixture of your excellent scripts to edit the original image plus my Macro Express macros to add text. That latter part was only semi-automated and ponderous. So, in pursuit of a more elegant method, I'm still hoping for a way to get a script as described above.

--
Terry, East Grinstead, UK
--
Terry, East Grinstead, UK
Using PSP 8 & PSP 2018 under Win 10
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: Script to add text from clipboard

Post by LeviFiction »

I'm missing something, the code I gave you gets the text from the clipboard and uses it as the text for the text command. So it seems to me, to do exactly what you're asking it to do now. I'm not seeing the difference.
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: Script to add text from clipboard

Post by terrypin »

I've just tried that last full script you sent me and got what I recall getting before, a message saying the file couldn't be loaded. I'll shortly have another crack at modifying my own simple script (as pasted up thread) in line with your previous instructions, as I'm sure success is close. But if you could suggest an appropriate edit of my very basic script that would be much appreciated please! I'm afraid I don't feel comfortable editing PSP scripts, which seem very unforgiving. :?

EDIT, about half an hour later:
Breakthrough! I think this was the same issue I encountered before and had forgotten. Your script after pasting into my text editor (TextPad) had four spaces at the beginning of every line. After removing them it worked perfectly, many thanks.

Can you just confirm that the script's function is exclusively to add a piece of clipboard text (at that particular size/style of font), no other processing of colours etc? That's what I want, because I can now run that
after the previous scripts. IOW, I will be applying it to JPG images like this one:

Image


Terry, East Grinstead, UK
--
Terry, East Grinstead, UK
Using PSP 8 & PSP 2018 under Win 10
User avatar
Ken Berry
Site Admin
Posts: 22481
Joined: Fri Dec 10, 2004 9:36 pm
operating_system: Windows 11
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: Gigabyte B550M DS3H AC
processor: AMD Ryzen 9 5900X
ram: 32 GB DDR4
Video Card: AMD RX 6600 XT
Hard_Drive_Capacity: 1 TB SSD + 2 TB HDD
Monitor/Display Make & Model: Kogan 32" 4K 3840 x 2160
Corel programs: VS2022; PSP2023; DRAW2021; Painter 2022
Location: Levin, New Zealand

Re: Script to add text from clipboard

Post by Ken Berry »

I'm going to transfer this thread to the Scripting forum as it seems to be more suited to that.
Ken Berry
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: Script to add text from clipboard

Post by LeviFiction »

The script I gave you contains a single function that you can call. This function only grabs the text from the clipboard and returns it as a string.

Inside the App.Do(Environment, 'TextEx'....) command you call this function for the text data. So yes, my script only returns text and does not mess with the colors. Whatever colors are used in the TextEx command are the colors that will always be used.
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: Script to add text from clipboard

Post by JoeB »

terrypin wrote:EDIT, about half an hour later:
Breakthrough! I think this was the same issue I encountered before and had forgotten. Your script after pasting into my text editor (TextPad) had four spaces at the beginning of every line. After removing them it worked perfectly, many thanks.
I use Notepad++ and it doesn't seem to mess with any scripts. The same with the much more basic EditPad Lite.
Regards,

JoeB
Using PSP 2019 64bit
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: Script to add text from clipboard

Post by terrypin »

Ken Berry wrote:I'm going to transfer this thread to the Scripting forum as it seems to be more suited to that.
Thanks Ken, glad you did - I'd forgotten about this sub-forum.
--
Terry, East Grinstead, UK
Using PSP 8 & PSP 2018 under Win 10
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: Script to add text from clipboard

Post by terrypin »

LeviFiction wrote:The script I gave you contains a single function that you can call. This function only grabs the text from the clipboard and returns it as a string.

Inside the App.Do(Environment, 'TextEx'....) command you call this function for the text data. So yes, my script only returns text and does not mess with the colors. Whatever colors are used in the TextEx command are the colors that will always be used.
Thanks, understood. Studying it more carefully after asking, it became clear it was a 'text only' script. At first sight it just seemed so extensive at 110 lines!

I'm using it to add three pieces of text. So I'm preparing to run three separate scripts, changing the clipboard contents before each run. Each script has its pair of 'Start' entries edited for the appropriate position. At present it's trial and error, adding a comment each time to remind me what I've done, like this:

Code: Select all

'Start': (35.5,235.5),     # Changed from 17.5,175.5
But is it possible to pre-determine those numbers somewhow?

Following this thread's move to the Scripting sub-forum, where I should have posted it originally, I've just seen your helpful list of resources. I've made promises to myself about this before, but that's prompted me to highlight it on myToDo list!

Presumably the clipboard is the only way PSP8 can communicate externally'? For instance, if I wanted to place several specific text strings in various positions, could I get these into a single script any other neater way?

Re my reply to Joe, any thoughts about the four spaces thing?
--
Terry, East Grinstead, UK
Using PSP 8 & PSP 2018 under Win 10
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: Script to add text from clipboard

Post by terrypin »

JoeB wrote: I use Notepad++ and it doesn't seem to mess with any scripts. The same with the much more basic EditPad Lite.
Thanks Joe. That's very odd, as I get the spaces with all three of the editors I've tried, including Notepad++

Image
--
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: Script to add text from clipboard

Post by JoeB »

terrypin wrote:
JoeB wrote: I use Notepad++ and it doesn't seem to mess with any scripts. The same with the much more basic EditPad Lite.
Thanks Joe. That's very odd, as I get the spaces with all three of the editors I've tried, including Notepad++
Sorry - my mistake! I misread your original comment and thought you meant it added 4 line spaces between commands, not 4 spacebar spaces at the beginning of each line.

As I recall (and stand to be corrected because it was awhile ago I saw it), there is a post somewhere that indicates that the issue is with this board and how it posts coded stuff. When a script is posted in it's original form as coded and copied then pasted into an editor it seems to cause this issue. When the same script is posted as a zip file and downloaded, unzipped and opened in a text editor there is no such problem. I don't have time to test that right now to confirm that I remember the issue correctly.
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: Script to add text from clipboard

Post by LeviFiction »

I believe we had narrowed it down to the browser they were using. Some people never saw it and they used Chrome or IE. I can't remember either. I think Firefox definitely tended to add extra spaces. The good news on spaces, if you have the python template in Notepad++ set to use 4 spaces between indents, and to convert all tabs to spaces. You can do a CTRL + A to select all then SHIFT+TAB to un-indent the code by 4 spaces. So very easy to correct. I haven't used the other editors so I don't know how to handle those.
https://levifiction.wordpress.com/
Post Reply