You are here: PSPad forum > English discussion forum > How to write VBScript to replace characters using regex in current tab only?

How to write VBScript to replace characters using regex in current tab only?

#1 How to write VBScript to replace characters using regex in current tab only?

Posted by: therium | Date: 2018-06-25 16:47 | IP: IP Logged

I'd like to learn a bit of VBScript so I can loop through the text file in the current tab to replace odd characters with other characters. The reason I say "loop through each line" is I may want to search for strings at the beginning or end of each line, so I just can't do a one-shot global replace of my whole text file, AFAIK.

How does PSPad loop through each line in the current tab when a VBScript is run?

Is there any way for me to learn this? Is there an example of this somewhere?

I'm looking at the file "Scripts\ListTransform.vbs" and there are very little comments. But it looks like the VBScript starts with the Init() subroutine? Is that right?

Does every VBS script at the top have to have these lines and constants?
' it will print all open files to printer
const module_name = "ListTransform"
const module_ver = "1.1"

Can I have the Init() subroutine at the top of the VBS file?

I'm a Perl programmers but I'm not familiar with VBScript.

Thank you.

Edited 1 time(s). Last edit at 2018-06-25 16:50 by therium.

Options: Reply | Quote | Up ^


#2 Re: How to write VBScript to replace characters using regex in current tab only?

Posted by: vbr | Date: 2018-06-25 22:49 | IP: IP Logged

therium:
...
How does PSPad loop through each line in the current tab when a VBScript is run?
...

Hi,
if You are already familiar in Perl, I'd recommend to try to make use of this language for PSPad scripting - PSPad uses WSH - Windows Sripting Host for its scripting capabilities, and Perl should be generally supported there.
I can't give th relevant details, but there are certainly some hints on the web; e.g.
www.perlmonks.org
suggests using ActiveState's PerlScript

After sucessfully installing the scripting language, PSPad should create the respective folder automatically
as a subfolder in
...\PSPad\Script\
the files in this folder are then run with the dedicated interpretter

There is general help file for scripting
...\PSPad\Scripting.rtf

with several examples and the reference of the program-wide PSPad function as well as commands for handling the editor window.

module_name = "..."
module_ver = "..."

are indeed required for each script file; module_name must be unique within all scripts.

The function called Init is called automatically on each start of the editor or by using the menu item "Scripts: Recompile scripts"
Its general usage handles the creation of the (sub)menu items (including keyboard shortcuts), which enable calling the individual functions defined in the script itself.

I am not sure, whether setting up Perl for this environment has some quirks or limitations, as I don't have experience with this language;
I use Python for some more specialised tasks, but generally I use JScript in PSPad, as this is built in, and it's enough for most tasks in this area.

As for linewise looping, You could probably use the internal functions: (descriptins form the mentioned helpfile):
linesCount(): integer
returns editor lines count

lineText(string): string
replaces/returns content of active line

However, I'd retrieve the whole editor text conent, using:
text(string):string
replaces/returns all editor text

and manipulate it within the script.

I'd generally use regex with its handling of linebreaks (either via ^ $ or handling the linebreaks individually \r\n - depending on the available regex library).

hth,
vbr

Options: Reply | Quote | Up ^






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