Skip to content

Helpful scripts

Eugen Kremer edited this page Jun 9, 2020 · 6 revisions

On this page I will collect short snippets I needed someday. You will find Scintilla relevant snippets here.

How to save current document with certain name/path

Because of API of Notepad++ do not provide a possibility to save file with certain name, we can try to implement a workaround.

We use the fact that Save As dialog pop ups with activated file name text box. So we can paste our file name in this box and "press" enter. Drawback of this way is, you will see this dialog shortly and you have to care that file does not exist.

function SaveAs(newFileName){
    // go around the blocking FILE_SAVE
    System.setTimeout({millis:100,cmd:function(){
        var shell = new ActiveXObject("WScript.Shell");
        shell.SendKeys(newFileName);
        shell.SendKeys("{ENTER}");
    }});

    // blocks executing of script
    MenuCmds.FILE_SAVEAS();
}

More information about WScript.Shell

How to use system "Browse for folder" dialog

var shellApp = new ActiveXObject("shell.application");

var folder = shellApp.BrowseForFolder(0,"Select my folder",0, "c:\\");
if (folder != null)
	alert(folder.self.Path);
else
	alert("Canceled");

First parameter is a handle of a parent window. It is possible to use Editor.handle for this purpose. The second parameter is a text message shown at the top of dialog. Third parameter is a flag for a dialog. Fourth parameter is optional start node.

More information about Shell.BrowseForFolder

Read from file

In following snippet we use ADODB.Stream ActiveX for reading from file. ADODB.Stream allows to use certain character set (UTF-8, Windows-1252, ..) for reading files.

function readFile(path, charset) {
	if (typeof(charset) == "undefined")
		charset = "Windows-1252";
		
	var stream = new ActiveXObject("ADODB.Stream");

	stream.Charset = charset;
	stream.Open();
	stream.LoadFromFile(path);

	var result = stream.ReadText();
	stream.close();

	return result;
}

// test code
try{
	alert(readFile("d:\\my-utf8-file.txt","UTF-8"));
	alert(readFile("d:\\my-ansi-file.txt"));
}catch(e){
	alert(e.message)
}

Read registry keys using WMI

The next code using WMIs StdRegProv is very tricky, because javascript does not support out parameters and the call we use returns value over it. It was a good message for me, that there is a way to do it.

function readRegSubKeys(keyNum, subKeyName){
	//Constructing InParameters Objects and Parsing OutParameters Objects
	
	var objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!//./root/default:StdRegProv");
	
	// because of javascript does not support out parameters do all things other
	// InParameters
	var objInParam = objReg.Methods_("EnumKey").InParameters.SpawnInstance_();


	// Add the input parameters.
	objInParam.Properties_.Item("hDefKey")     =  keyNum;
	objInParam.Properties_.Item("sSubKeyName") =  subKeyName;

	// Execute the method and obtain the return status.
	// The OutParameters object in objOutParams
	// is created by the provider.
	var objOutParams = objReg.ExecMethod_("EnumKey", objInParam);
	
	// read output parameter. For StdRegProv it is sNames
	var sNames = objOutParams.Properties_.item("sNames").Value;
	
	// because of sNames is a SafeArray convert it to javascript array
	return  (new VBArray(sNames)).toArray();
}

try{

	var HKEY_CLASSES_ROOT  = 0x80000000;

	alert(readRegSubKeys(HKEY_CLASSES_ROOT, "MIME\\Database\\Charset").join(", "));
}catch(e){
	alert(e.message)
}

Launch a program in command prompt

Following code opens a command prompt and then executes a command and shows its standard output.

var shell = new ActiveXObject("WScript.Shell");
var cmd = shell.Exec("cmd /c dir c:\\");

alert(cmd.StdOut.ReadAll());

Exec method Api

Launch a program with user interface

Following code opens new instance of Windows Explorer with selected C: drive.

var shell = new ActiveXObject("WScript.Shell");
shell.run('explorer /select,"c:"')

Run method Api

Change the text of statusbar

Since 2.2.185.1 Release you can use "lib/Window.js" in your scripts to find a child window of Notepad++, to get or set its text.

Following code snippet finds the statusbar of Notepad++ schows the current text of it and then replaces it with "my text". By switching to another file the text will be adapted by Notepad++.

require("lib/Window.js");

var statBar = Window.FindByClass(Editor.handle, "msctls_statusbar32");
alert(statBar.GetWindowText());
statBar.SetWindowText("my text");
alert(statBar.GetWindowText());

Open a Page in InternetExplorer

Some times you need to open a web page as a dialog window.

var ie = new ActiveXObject("InternetExplorer.Application");
ie.Visible = true;
ie.AddressBar = false;
ie.Navigate("http://google.com");

More information about InternetExplorer.Application

Generate Guid

If can use generated guid as unique file or folder name.

var guid = new ActiveXObject("Scriptlet.TypeLib").Guid;