Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #22355
    LifeTimer
    Participant

    Using the /mf switch, it is possible to execute a macro on any given file directly from the command line, which is very good!

    One big problem with this though is that I haven’t been able to find any way to provide parameters for this macro from this same command line – how can this be done?

    A simple example:
    I have a macro that will delete every n:th line from the current file, and I want to be able to provide this “n” value (i.e. so that it can delete for example every second line, every third line, every fourth line etc, based on my current choice).

    This must be possible somehow, right? But how? I cannot find anything like this in the EmEditor command-line reference?

    #22359
    Patrick C
    Participant

    Just worked on the same problem today – guess I was lucky

    The command line therefore is
    set myarg=blabla && start "" "c:\Program Files\EmEditor\EmEditor.exe" /mf "d:\tmp\mymacro.jsee

    The macro file code then is

    var she = new ActiveXObject("wscript.shell");
    myarg = she.ExpandEnvironmentStrings("%myarg%");
    alert(myarg);

    There is one important issue though:
    Emeditor must not yet run when calling. Most likely because once running it would only have access to environment variables which existed upon starting EmEditor

    Hope this helps
    Patrick

    PS Credits for those who brought me on the right path:
    https://www.emeditor.com/forums/topic/macro-scripting-importexport-of-filter-settings/
    https://stackoverflow.com/questions/21315708/reading-environment-variables-with-javascript
    https://superuser.com/questions/424001/launch-windows-program-with-custom-environment-variable

    #22364
    LifeTimer
    Participant

    Thanks for this handy hack!

    Unfortunately, I already have an open EmEditor instance most of the time when working (also with many active tabs in it, so it cannot be easily closed either), but perhaps this could in turn be worked-around by adding the “/sp” parameter, thus forcing the creation of a new EmEditor process? I will try.

    It would be great though with an “official” confirmation from Mr Emura, that this environment variable hack is the only way to provide parameters to macros from the command line (in which case I would like to submit a feature suggestion for a new method to provide such parameters to macros directly on the command-line when starting EmEditor). Mr Emura?

    #22365
    Yutaka Emura
    Keymaster

    Hello,

    Please use ExecuteMacro method to run a macro within a macro. Does it work for you?

    Thanks,

    #22366
    LifeTimer
    Participant

    Hello Mr Emura, and thanks for your reply.

    I’m not quite sure I understand what you mean though, or that I managed to explain my problem/goal to you properly.

    What I want to accomplish is this:

    With a single command-line (i.e. from outside EmEditor, from the normal cmd.exe command-line) I want to:

    1. Open EmEditor with a certain text file (which is of course done simply by specifying the path to the file on the command line after “emeditor.exe”).
    2. Execute a certain macro on this file (which can be done using the “/mf” switch).
    3. Provide this macro with some further parameter data before it executes (which is what I’m asking you here how it can be done from the command-line???).

    As I stated in my initial post above, such a macro could for example be one that will delete every n:th line from the current file, and I then want to be able to provide this macro with the “n” value (i.e. so that it can delete for example every second line, every third line, every fourth line etc, based on my current choice).

    The command-line would then be as follows for deleting every third line from the file “c:\some_dir\some_target_file.txt”, and then displaying the result in EmEditor:

    emeditor.exe c:\some_dir\some_target_file.txt /mf c:\my_emeditor_macros\delete_every_nth_line.jsee -macroparam “n=3”

    The “-macroparam” switch is something I made up just now, as an example for how this could be done, and what I’m asking you is if some similar way to provide parameter data to macros directly from the command-line already exists in EmEditor today (or how I otherwise could accomplish this)?

    #22368
    Yutaka Emura
    Keymaster

    Hello LifeTimer,

    I am sorry that I misunderstood your question. Unfortunately, there are no good ways to pass parameters from commandline to macros. To work around this, you can use an external file or registry values to pass parameters. For instance, you can write data to an external file, and then you can read that data in the macro. You can also use a registry value to pass data.
    I hope this helps.

    Thanks,

    #22370
    LifeTimer
    Participant

    Hello Mr Emura, thanks for your reply and this confirmation.

    Unfortunately I cannot use any external data source like files or registry values, since I need to be able to call these command-lines from another software that I use, which can invoke external tools like EmEditor by using command-lines, but not write any files or registry values prior to that (and there are of course other cases where also only self-contained command-lines work, e.g. if I need to send a command-line to my colleague, which they then quickly can use on their computer for some task).

    I think that such command-line parameter passing for macros would be a highly useful feature to go with the already super useful possibility to execute macros from the command-line with the “/mf” switch, so I have now created an enhancement suggestion for this, which I hope that the higher powers would at least take into consideration. :-)

    #22388
    Patrick C
    Participant

    Dear LifeTimer,

    Sorry I wasn’t able to post this earlier, though better late than never…

    How to pass command line arguments using registry values without having to close EmEditor:

    Approach 1: using multiple command line statements

    reg add "HKCU\_Patrick_custom_" /v str1 /t REG_SZ    /f /d "ab cd"
    reg add "HKCU\_Patrick_custom_" /v str2 /t REG_SZ    /f /d "1234"
    reg add "HKCU\_Patrick_custom_" /v num1 /t REG_DWORD /f /d 1234

    Regarding the syntax:
    // /v value name (“variable name”)
    // /t variable type: REG_SZ = string; REG_DWORD = 32-bit integer
    // /f force overwrite already existing value without prompting for Y/N
    // /d data to be written (“assigned to the variable”)

    to visualise the values in the registry:
    Registry

    then call the EmEditor macro
    "c:\Program Files\EmEditor\EmEditor.exe" /mf "d:\tmp\mymacro.jsee

    with the macro code being

    var WshShell = new ActiveXObject( "WScript.Shell" );
    var str1 = WshShell.RegRead( "HKCU\\_Patrick_custom_\\str1" );
    var str2 = WshShell.RegRead( "HKCU\\_Patrick_custom_\\str2" );
    var num1 = WshShell.RegRead( "HKCU\\_Patrick_custom_\\num1" );
    alert( str1 );
    alert( str2 + 1 );   // an imported string is not automatically a number
    alert( num1 + 1);    // an imported DWORD really is an integer

    Approach 2: using one single command line statement
    If, and only if it really has to be a one liner:
    reg add "HKCU\_Patrick_custom_" /v str1 /t REG_SZ /f /d "xyz" && "c:\Program Files\EmEditor\EmEditor.exe" /mf "d:\tmp\mymacro.jsee"

    I hope this will be of use for someone.

    Credits:
    https://www.windowscentral.com/how-edit-registry-using-command-prompt-windows-10
    http://www.emeditor.org/en/macro_tutorial_tutorial_regread.html

    #22393
    LifeTimer
    Participant

    Thank you very much Patrick for yet another very hacky (and accordingly messy ;-)), but yet very functional solution to work around this problem! :-)

    Just to be a pain in the ass though, I will present yet another problem that I would have with using this solution in production:
    We are planning to have multiple users being logged into the same terminal server (using the same user account), which would then result in the risk of race conditions with undefined outcomes if more than one person run the same command (with different parameter data) at the same time. :-)

    Of course a quite low-probability problem, but I wouldn’t want Mr Emura to think that the problem is fully solved already, so that he doesn’t want to implement the macro parameter passing inside EmEditor itself anymore, as per my enhancement suggestion. ;-)

Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.