You are here: PSPad forum > English discussion forum > Python Scripting

Python Scripting

Goto Page: Previous1 2 3 Next

#11 Re: Python Scripting

Posted by: vbr | Date: 2015-08-07 10:17 | IP: IP Logged

pythonui:
Success !

But when running script getting error !

pspad.addMenuItem("Example function", "PySample sample", "beispiel")

Could you please tell me what is 'pspad' in this case? I know this can be class or module but how?

Hi,
in some python scripts for PSPad I have seen before, there is an assignment:

pspad = globals()['global']

it appears, the support for python via WSH, which is also utilised by PSPad, is somehow unintuitive in the context of python programming - the namespaces are somehow nivellated...

hth,
vbr

Options: Reply | Quote | Up ^


#12 Re: Python Scripting

Posted by: pythonui | Date: 2015-08-07 10:39 | IP: IP Logged

Thank You Very Much smiling smiley

pspad = globals()['global']

Its working perfectly ....!!

Options: Reply | Quote | Up ^


#13 Re: Python Scripting

Posted by: pythonui | Date: 2015-08-07 11:13 | IP: IP Logged

@vbr
Could you please share demo example of python script for PSPAD? (If you can)

Edited 1 time(s). Last edit at 2015-08-07 11:14 by pythonui.

Options: Reply | Quote | Up ^


#14 Re: Python Scripting

Posted by: vbr | Date: 2015-08-07 22:24 | IP: IP Logged

pythonui:
@vbr
Could you please share demo example of python script for PSPAD? (If you can)

Hi, I'm glad, it helped,
I actually normally don't use python for PSPad scripting, but here is an ad hoc coded demo.
It should show the current character before the caret, its unicode number, name and category using the unicodedata module.

There are some limitations of this approach - there may be no text selection on calling this function; non-printing system characters are not handled. More importantly the "foreign" characters not supported in the respective ansi-codepage (e.g. win 1252) are not shown - due to the limitation of the script interface; however, the data for such characters should be shown correctly (for characters in the basic multilingual pane - up to U+FFFF).

It's rather a demo code, my previous attempts on using tkinter gui in connection with pspad scripting were not fully successful and I am therefore not using python for this - unless you plan to utilize some already available python library, I don't see strong advantages for this usage.

hth,
vbr

(Should the formatting and whitespace get mangled, you can obtain the original text in the editing window, if you click to quote my message;)

Quote:
#! Python

module_name = "py_char_inf"
module_ver = "0.1"

pspad = globals()['global']

import unicodedata

def py_char_info():
"""Show some basic unicodedata information about the current character."""
if pspad.editorsCount() < 1: # quit if no editor panels are open
return False
act_ed = pspad.newEditor()
act_ed.assignActiveEditor()
act_ed.command("ecSelRight")
char_after_caret = act_ed.selText()
char_inf = u"Character info:\nunknown"
if len(char_after_caret) == 1:
act_ed.command("ecLeft")
unicode_nr = "U+"+str(hex(ord(char_after_caret)))[2:].upper().zfill(4)
unicode_categs = {"":"??", "Lu":"Letter, Uppercase", "Ll":"Letter, Lowercase", "Lt":"Letter, Titlecase", "Lm":"Letter, Modifier", "Lo":"Letter, Other", "Mn":"Mark, Nonspacing", "Mc":"Mark, Spacing Combining", "Me":"Mark, Enclosing", "Nd":"Number, Decimal Digit", "Nl":"Number, Letter", "No":"Number, Other", "Pc":"Punctuation, Connector", "Pd":"Punctuation, Dash", "Ps":"Punctuation, Open", "Pe":"Punctuation, Close", "Pi":"Punctuation, Initial quote (may behave like Ps or Pe depending on usage)", "Pf":"Punctuation, Final quote (may behave like Ps or Pe depending on usage)", "Po":"Punctuation, Other", "Sm":"Symbol, Math", "Sc":"Symbol, Currency", "Sk":"Symbol, Modifier", "So":"Symbol, Other", "Zs":"Separator, Space", "Zl":"Separator, Line", "Zp":"Separator, Paragraph", "Cc":"Other, Control", "Cf":"Other, Format", "Cs":"Other, Surrogate", "Co":"Other, Private Use", "Cn":"Other, Not Assigned (no characters in the file have this property)"}
unicode_name = unicodedata.name(char_after_caret, "??")
unicode_categ_abbr = unicodedata.category(char_after_caret)
unicode_categ = unicode_categs.get(unicode_categ_abbr, "??")
char_inf = u"Character info:\n%s - %s %s (%s - %s)" % (char_after_caret, unicode_nr, unicode_name, unicode_categ_abbr, unicode_categ)
pspad.echo(char_inf)

def Init():
pspad.addMenuItem("char info","","py_char_info", "Ctrl+Alt+Shift+C")

Options: Reply | Quote | Up ^


#15 Re: Python Scripting

Posted by: pythonui | Date: 2015-08-08 03:25 | IP: IP Logged

@vbr

Thanks for your great answer ! I am now going to convert my all PSPAD scripting(javascript) into python scripting smiling smiley

Thanks again !

Options: Reply | Quote | Up ^


#16 Re: Python Scripting

Posted by: pythonui | Date: 2015-08-08 04:03 | IP: IP Logged

Here is demo python script that will format the json content (still in progress of development for custom indentation!! )

Quote:
# imports
import json

pspad = globals()['global']
module_name = "FormatJSON";
module_ver = "1.1";
module_title = "FormatJSON";

def open_script():
"""Open this script in new editor window"""

new_editor = pspad.newEditor()
new_editor.openFile(pspad.moduleFileName("FormatJSON"))

def format_json():
"""Format the json content with indent 4"""

editor = pspad.newEditor()
editor.assignActiveEditor()
json_data = editor.text()
format_data = json.dumps(json.loads(json_data), indent=4)

new_editor = pspad.newEditor()
windowName = "__JSON__"
try:
new_editor.assignEditorByName(windowName)
except:
new_editor.newFile(windowName)
new_editor.text("")
new_editor.activate()
new_editor.highlighter("JSON")
new_editor.appendText(format_data)

def about():
pspad.echo(
"\n" + module_name + " " + module_ver + "\n\n" +
"PSPad Format JSON Script\n\n" +
"by Python UI,\n" +
"This script format the content of json file"
)

def Init():
pspad.addMenuItem("Format JSON", "JSON", "format_json")
pspad.addMenuItem("-","JSON","","")
pspad.addMenuItem("Edit this script", "JSON", "open_script")
pspad.addMenuItem("About", "JSON", "about")

NOTE: You have to indent the python code smiling smiley

Thanks

Edited 2 time(s). Last edit at 2015-08-08 04:06 by pythonui.

Options: Reply | Quote | Up ^


#17 Re: Python Scripting

Posted by: vbr | Date: 2015-08-08 07:33 | IP: IP Logged

Hi,
I'm glad it helped; indeed, the mangled formatting is rather unfriendly for python code...; at least the quoting trick seems to work ok.
This is exactly the oppourtunity for python scripting, where you can reuse a ready made function in the json module.
Just a small tip - you can open any script file by simultaneously pressing left and right mouse button over a menu item of the respective script; you don't need a dedicated own function for this.
regards,
vbr

Options: Reply | Quote | Up ^


#18 Re: Python Scripting

Posted by: pythonui | Date: 2015-08-08 07:41 | IP: IP Logged

Quote:
Just a small tip - you can open any script file by simultaneously pressing left and right mouse button over a menu item of the respective script; you don't need a dedicated own function for this.

Thanks for suggestion smiling smiley

Options: Reply | Quote | Up ^


#19 Re: Python Scripting

Posted by: steph | Date: 2016-12-17 17:58 | IP: IP Logged

Hello,

I need some help as I didn't succeed having python script working with PSPad.

Where am I ?
- windows 10
- PSPad 4.6.1 (2730)
- ActivePython34 installed
- seems to interface properly with WSH as I can execute correctly script with command line:
> cscript wsh.pys
or
> cscript //E:python wsh.py
where wsh.py(s) is a script found on internet (it uses WScript.Echo("hello"))

What is missing ?
- no python script subdirectory is created for python when I launch PSPad (even as admin) but I got new ones : JScript.Compact and XML that didn't exist before
- so I created subdirectories by hand named python, pyscript, Python.AXScript.2 (and some others) with simplified version of script given by vbr and pythonui (kept only about option and pspad = globals...) and recompiled scripts but got nothing at all

Can anybody help me please ?

Thanks.

Options: Reply | Quote | Up ^


#20 Re: Python Scripting

Posted by: pspad | Date: 2016-12-17 18:30 | IP: IP Logged

steph:
- ActivePython34 installed

What is missing ?
- no python script subdirectory is created for python when I launch PSPad (even as admin) but I got new ones : JScript.Compact and XML that didn't exist before
- so I created subdirectories by hand named python, pyscript, Python.AXScript.2 (and some others) with simplified version of script given by vbr and pythonui (kept only about option and pspad = globals...) and recompiled scripts but got nothing at all

Hello

if your PSPad is in the Program settings folder, it needs to be run with elevated rights to have right to create folder. Delete all your python folders and run PSPad as administrator. If Python is installed, PSPad will create proper folder for you

Options: Reply | Quote | Up ^


Goto Page: Previous1 2 3 Next





Editor PSPad - freeware editor, © 2001 - 2024 Jan Fiala, Hosted by Webhosting TOJEONO.CZ, design by WebDesign PAY & SOFT, code Petr Dvořák, Privacy policy and GDPR