1

Trying to set up a macro for EmEditor to execute the following steps that I could otherwise do manually.

  1. open file
  2. view > large file controller
  3. set To=1000
  4. Click Apply
  5. Click Save Opened Portion As...
  6. Enter TestMacro and Save

I'm using: EmEditor Professional (64-bit) version 22.4.1. Windows 10 operating system.

I tried recording, that didnt seem to work.

Then I tried variations of this snippet suggested as answer but error provided was specificed file does not exist.

editor.WriteProfileInt( eeRegCommon, "", "DefaultBytesTo", 1000 );    // open to 1000 bytes
editor.RefreshCommonSettings();
editor.OpenFile( "D:\\Test\\Input.txt", 0, eeOpenDetectUnicode | eeOpenDetectUTF8 );
editor.WriteProfileInt( eeRegCommon, "", "DefaultBytesTo", 0 );       // open to unlimited
editor.RefreshCommonSettings();
editor.ExecuteCommandByID(4323);  // Clear All Bookmarks in This Document
editor.ExecuteCommandByID(4588);  // Invert Bookmarks in This Document
editor.ExecuteCommandByID(4590);  // Extract Bookmarked Lines in This Document to New File
document.Save( "D:\\Test\\TestMacro.txt" );

How is this macro set up to achieve these steps?

After editing my file path to "input" instead of "input.txt" error was resolved.

enter image description here

MMsmithH
  • 323
  • 1
  • 8

1 Answers1

1

I wrote a macro to open a file E:\Test\Input.txt to 1000 bytes, and save it as E:\Test\TestMacro.txt.

Notes:

  • You can also change the default To bytes in the File page of the Customize dialog box, but this macro will change this only while opening the file, and revert it to default automatically when done.
  • There is no macro method equivalent to Save Opened Portion As, but you can set bookmarks to all lines, extract bookmarked lines to a new file, and then save the new file as TestMacro.txt.
editor.WriteProfileInt( eeRegCommon, "", "DefaultBytesTo", 1000 );    // open to 1000 bytes
editor.RefreshCommonSettings();
editor.OpenFile( "E:\\Test\\Input.txt", 0, eeOpenDetectUnicode | eeOpenDetectUTF8 );
editor.WriteProfileInt( eeRegCommon, "", "DefaultBytesTo", 0 );       // open to unlimited
editor.RefreshCommonSettings();
editor.ExecuteCommandByID(4323);  // Clear All Bookmarks in This Document
editor.ExecuteCommandByID(4588);  // Invert Bookmarks in This Document
editor.ExecuteCommandByID(4590);  // Extract Bookmarked Lines in This Document to New File
document.Save( "E:\\Test\\TestMacro.txt" );
Yutaka
  • 1,761
  • 2
  • 5
  • 9
  • When I try executing this macro, it doesnt open file, so I reset Emeditor settings then received error that "The specified file does not exist' I think this has to do with the setting for set bookmarks to all lines, which I am trying to find? – MMsmithH May 29 '23 at 01:28
  • Did you change the file path? Please be aware that a backslash ( \ ) must be escaped as double backslashes ( \\ ) in JavaScript. – Yutaka May 29 '23 at 01:29
  • I did not. I tried to duplicate exact files, so I could use the code exactly, then because my E: is external, I replicated with D:, I will edit in my question above. It did seem to show the large text editor opened without the file initially. But didnt save any ouput. If I click on Open as a new file, then it creates a 0Kb input and saves an output also 0kb – MMsmithH May 29 '23 at 01:38
  • I think I figured out. Maybe because file in folder was input.txt but path was input.txt.txt. When I changed name to input, it seems to work, though with several warnings, that I can opt out of, so I am testing to see if this can work without triggering the same errors again. – MMsmithH May 29 '23 at 01:48
  • Can I modify this Macro so that instead of "DefaultBytesTo" I can use something that will extract only x number of lines? – MMsmithH Jun 02 '23 at 14:13
  • You can't specify the exact number of lines on the Large File Controller. However, you can open a file without the Large File Controller, and use the `SetActivePoint` method to specify the selection. – Yutaka Jun 02 '23 at 15:03
  • Is there a list of commands that I can view where it states for example, the code and then comments? ie editor.ExecuteCommandByID(4323); // Clear All Bookmarks, editor.ExecuteCommandByID(4588); // Invert Bookmarks in This Document. I do see https://www.emeditor.org/en/cmd_index.html – MMsmithH Jun 02 '23 at 16:03
  • `plugin.h` contains the list of IDs that you could use. https://github.com/Emurasoft/emeditor-plugin-library/blob/master/plugin.h – Yutaka Jun 02 '23 at 16:25