Tapatalk

When to use Scripts over Macros

When to use Scripts over Macros

10
Basic UserBasic User
10

PostOct 01, 2007#1

Is there a general rule of thumb on when to use a script instead of a macro? Can scripts do everything a macro can do and more?
I know this question might be hard to give a definitive answer to but if someone could elaborate on the subject I'd appreciate it.

JH

6,824625
Grand MasterGrand Master
6,824625

PostOct 01, 2007#2

Scripts can much more than macros, but there are some things which cannot be done with scripts.

For example it is not possible with scripts to select a text from actual cursor position or expand an existing selection to end of a found string with a Find Select like it is possible with macros which I often use in my macros.

On the other hand there are script commands and properties not available in the macro environment. See Scripting reference for all available UltraEdit scripting functions and properties and Macro commands for all available UltraEdit macro commands and their parameters.

An advantage of macros is that they can be quickly recorded and re-run to redo some commands on the current file. I use that very often. Scripts must be always programmed, they cannot be recorded. That requires programming skills and more time when just do something with the actual file and then the script is not needed anymore.

Scripts have the big advantages of support for variables, easy string manipulations in RAM and there is the possibility to work with the output window. Nesting of loops is also possible in scripts, but not in macros.

In scripts you can also easily add comments which is not possible for macros (except with a workaround). The source of a script can be also easily viewed and formatted with indentations and directly edited in the document window. That's not possible for macros (except with a workaround). The error messages are often better for scripts than for macros.

Scripts are really well when they are designed for a job which must be done daily, weekly or monthly especially when reports should be also created by the scripts. Creating reports with macros is not so easy, mainly because it is not possible to simply copy a fixed text into a clipboard without the usage of an edit window and because there are no variables (counters).

Scripts are also better when working on multiple documents at the same time because data can be modified in a document which currently does not have the focus. In macros the document/window must be always switched when data in another file should be modified or read.

Scripts are now better for complex jobs. But for simple tasks I would still prefer macros over scripts.

On execution of a script the current output window content is overwritten with status information of script interpreter except Show status information in output window is unchecked at Advanced - Settings or Configuration - Scripting. This setting should be always enabled on script development as otherwise errors detected by scripting engine are not displayed anywhere. When entire script is fully tested, this setting can be disabled to prevent current output window content to be overwritten on script execution. The execution of a macro never results in overwriting the current output window content.

A macro edited in Edit/Create Macro dialog is compiled on closing the macro and confirming the change. Therefore a syntax error in macro code is immediately detected. This is not the case for a script. While the script engine makes a basic syntax check on start of script execution, some errors like referencing value of a variable which was not defined in code executed before are detected by script engine only on executing the appropriate line. Therefore testing a script to detect errors means that really each line must be executed once. That means each branch of every IF condition must be at least once executed by running the script several times with various inputs respectively under various use cases to make sure that really each line in script works and does not result in an error on execution.

10
Basic UserBasic User
10

PostOct 05, 2007#3

Mofi,
I appreciate your in-depth comparison between the macros and scripts. I think you've saved me a lot of time by knowing the differences.

Best Regards,
JH

13
Basic UserBasic User
13

PostJan 26, 2010#4

Mofi wrote:Scripts can much more than macros, but there are some things which cannot be done with scripts...
This is a great response, which really ought to be in the UltraEdit help file as a FAQ for newbies like me. UltraEdit clearly has a lot of history and there are clever things which were done with macros in the past, which ought to be done with scripts nowadays.
Mofi wrote:there are some things which cannot be done with scripts
More than two years have past since you posted this, Mofi. Is this still true? Do we still have to learn macro programming as well as scripting to get full value from UltraEdit 15.20 (e.g. to select text from the cursor position)?

If so, it would be good to know how to invoke macro functions and pass data to and from scripts so that newbies can use macros for as little as possible (other than recording) and focus their attention on ECMA scripting.

6,824625
Grand MasterGrand Master
6,824625

PostJan 26, 2010#5

See replacement for macro command Find Select.

The other macro commands not available in the script environment are:

NextDocument and PreviousDocument
NextWindow and PreviousWindow

The first two commands are simply not required in script environment because scripts can access all open documents parallel which is not possible in macros. The other two commands are mainly also for switching the active document and are therefore also normally not required within scripts.

It is not possible to run macros from within a script and it is also not possible to run scripts from within macros.

There are some small differences on some commands in special cases, but I think every other macro command is also available in the script environment.

13
Basic UserBasic User
13

PostJan 29, 2010#6

Do you reckon it is a fair assessment to say to future-power-user-newbies that they need not bother learning about macros then, and focus on scripting? i.e. Treat the macro language as deprecated (or something internal for the recorder)

6,824625
Grand MasterGrand Master
6,824625

PostJan 29, 2010#7

For larger tasks newbies should really focus on scripting. The macro environment is for newbies mainly helpful only for small customizations (like my small macros to insert German umlauts based on file extension), for redoing some steps several times after quickly recording them, and when it is necessary to execute some commands on every file load/save which is not possible with scripts (at the moment).

161
Basic UserBasic User
161

Post2:06 - Feb 22#8

Hi!
I am trying to run a small script in macros, but it does not work.
A short explanation of my problem: I edit .FB2 file and wish to insert a new footnote in the middle. I wrote a small script that increases the selected number by 1. Then I recorded a macro that finds the next footnote, selects its number, and calls the script. But when I run the macro, it fails to execute the script. Can I solve this problem without using an external macro management program?

Edit: I solved my problem, writing JavaScript with regular expressions. But this requires much more effort and is by no means a universal solution. I tried using an external program to work with macros, but it is significantly more inconvenient than the ones built into UltraEdit. So my question remains.

6,824625
Grand MasterGrand Master
6,824625

Post8:35 - Feb 22#9

An UltraEdit script requires more effort as it cannot be recorded like an UltraEdit macro. An UltraEdit script must be coded like it is necessary for most scripting languages. That requires also more or less programming skills.

There is no UltraEdit macro command to run an UltraEdit script from within an UltraEdit macro. There is no UltraEdit scripting command to run an UltraEdit macro from within an UltraEdit script. It is therefore necessary to do a task either completely with an UltraEdit macro or an UltraEdit script. It is not possible to use both together to get a text modification task done.

161
Basic UserBasic User
161

Post17:01 - Feb 22#10

I have more than enough skills as a programmer. I have been working as a programmer for over 35 years and have written code for just about everything – from Assembler, Fortran and dBASE, through C, Prolog and LISP, to JavaScript, Macromedia (Adobe) ActionScript and PHP. The point is that what I can do with five lines of JavaScript and a simple macro, that calling the script, which I can record in 10 seconds, took me 67 lines of code and more than an hour of work in JavaScript alone. So it's a real shame that macros in UltraEdit can't execute scripts.

Post19:34 - Feb 24#11

One more question about JavaScript. At the beginning of my script, I save the current row and column with the commands:

Code: Select all

var lineNum = UltraEdit.activeDocument.currentLineNum;
var colNum = UltraEdit.activeDocument.currentColumnNum;
At the end of my script, I set the row and column saved at the beginning as the current row and column with the command:

Code: Select all

UltraEdit.activeDocument.gotoLine(lineNum, colNum);
The problem is that when restoring the position, the current row is centered on the screen. And if it wasn't there before the script was executed, the text is moved up or down by several rows. Is there a way to restore the text on the screen to the exact position it was in before the script was executed?

6,824625
Grand MasterGrand Master
6,824625

Post5:49 - Feb 25#12

There is no possibility to get the information which lines of the file are currently displayed in the document window and therefore also no possibility to restore the initial display of file contents in the document window. There can be used replace all commands from current position of the caret in the file to avoid a repositioning of the caret and a change of the displayed portion of the file.