Run script or selection from Notepad++

Discuss features, issues, about Editors for AHK
User avatar
rediffusion
Posts: 58
Joined: 15 Mar 2019, 05:16

Re: Run script or selection from Notepad++

10 Dec 2019, 12:26

@chrisbtbi

Btw, «Notepad++» has a plugin called "SaveAsAdmin" it can save the file with `administrator` rights.
Splongus
Posts: 9
Joined: 27 Jun 2020, 23:15

Re: Run script or selection from Notepad++

17 Jul 2020, 12:48

I use NPP's inbuilt "Folder as workspace / Document map / Function list" sidebar that displays a zoomed out view of the file's contents, and when that's enabled it appears that any WinTitle that isn't just plain window title text will not be recognized (ahk_exe, ahk_pid, etc). In other words, this script fails to work on my NPP when the sidebar is enabled. Here's a quick fix (that prob still could use some extra checks but hey):

Code: Select all

SetTitleMatchMode, 2
StatusBarGetText, StatusBarText, 3, % " - Notepad++"
ClipSave := ClipboardAll
RegExMatch(StatusBarText, "(?<=Sel : )\d+", SelectionLen)
if (SelectionLen != 0 && SelectionLen != "") ; run the selected text only in a named pipe
{
	Clipboard := ""
	Send, ^c ; copy npp contents
	ClipWait, 1, 1
	if !ErrorLevel
		RunTempScript(Clipboard, "Selected Text - " A_TickCount)
	Clipboard := ClipSave
}
else ; run the whole script
{
	WinGetTitle, Title, % " - Notepad++"
	StringReplace, Title, Title, % " - Notepad++"
	if (SubStr(Title, -3) = ".ahk") ; has been saved as an ahk file
	{
		if (SubStr(Title, 1, 1) = "*") ; changes since last save, so save it
		{
			WinMenuSelectItem, % " - Notepad++",, File, Save
			Title := SubStr(Title, 2) ; remove "*"
			WinWait, %Title% ; wait for title without "*"
		}
		SplitPath, Title,, WorkingDir
		Run, %Title%, %WorkingDir%
	}
	else ; file has not been saved so grab all the text and run it in a named pipe
	{
		ControlGetFocus, ControlName, % " - Notepad++"
		if (InStr(ControlName, "Scintilla"))
		{
			ControlGetText, ScriptText, %ControlName%, ahk_exe notepad++.exe
			RunTempScript(ScriptText, "Unsaved Script - " A_TickCount)
		}
	}
}
return

RunTempScript(TempScript, name:="")
{
	if (name = "")
		pipe_name := A_TickCount
	else
		pipe_name := name
	pipe_ga := CreateNamedPipe(pipe_name, 2)
	pipe    := CreateNamedPipe(pipe_name, 2)
	if (pipe=-1 or pipe_ga=-1)
	{
		MsgBox CreateNamedPipe failed.
		ExitApp
	}
	Run, %A_AhkPath% "\\.\pipe\%pipe_name%",,,PID
	DllCall("ConnectNamedPipe","uint",pipe_ga,"uint",0)
	DllCall("CloseHandle","uint",pipe_ga)
	DllCall("ConnectNamedPipe","uint",pipe,"uint",0)
	Script := chr(0xfeff) TempScript
	if (!DllCall("WriteFile","uint",pipe,"str",Script,"uint",(StrLen(Script)+1)*2,"uint*",0,"uint",0))
		MsgBox WriteFile failed: %ErrorLevel%/%A_LastError%
	DllCall("CloseHandle","uint",pipe)
	Return PID
}

CreateNamedPipe(Name, OpenMode=3, PipeMode=0, MaxInstances=255)
{
    return DllCall("CreateNamedPipe","str","\\.\pipe\" Name,"uint",OpenMode,"uint",PipeMode,"uint",MaxInstances,"uint",0,"uint",0,"uint",0,"uint",0)
}
jyloup
Posts: 2
Joined: 25 Jul 2020, 11:50

Re: Run script or selection from Notepad++

26 Jul 2020, 10:02

Hello
i tried the script it works well !
But the only annoying thing is everytime i run the script from the same file, it open a new instance, that makes many scripts running at the same time, and so many green "H" on taskbar, instead of replacing the ongoing script. So i must always quit the tested script, before running again from the editor.
I remember in Scite4autohotkey editor, everytime i run a script from the same file, it replaces the previous executed tested script, that avoids populating tasbar with multiple "green H" scripts.

Is there a way to avoid this?
jyloup
Posts: 2
Joined: 25 Jul 2020, 11:50

Re: Run script or selection from Notepad++

26 Jul 2020, 10:11

Is there a way to avoid this?
I found the solution finally by using #SingleInstance option at beginning of file :

Code: Select all

#SingleInstance force
Now at each run from notepad++, i have only one single instance of the edited script (only 1 green "H" on taskbar) that's great ! :)

Cheers !
User avatar
boiler
Posts: 6107
Joined: 21 Dec 2014, 02:44

Re: Run script or selection from Notepad++

26 Jul 2020, 10:47

jyloup wrote:
26 Jul 2020, 10:02
Hello
i tried the script it works well !
But the only annoying thing is everytime i run the script from the same file, it open a new instance, that makes many scripts running at the same time, and so many green "H" on taskbar, instead of replacing the ongoing script. So i must always quit the tested script, before running again from the editor.
I remember in Scite4autohotkey editor, everytime i run a script from the same file, it replaces the previous executed tested script, that avoids populating tasbar with multiple "green H" scripts.

Is there a way to avoid this?
It's true that it will open a new "named pipe" if you leave the old script running. Sorry, I don't have a way to avoid this at the moment. I will have to study more about re-using the same named pipe or at least having the new one wait for the current one to close. I will look into it at some point, but I can't say I'll have an immediate solution.
User avatar
boiler
Posts: 6107
Joined: 21 Dec 2014, 02:44

Re: Run script or selection from Notepad++

26 Jul 2020, 10:49

jyloup wrote:
26 Jul 2020, 10:11
Is there a way to avoid this?
I found the solution finally by using #SingleInstance option at beginning of file :

Code: Select all

#SingleInstance force
Now at each run from notepad++, i have only one single instance of the edited script (only 1 green "H" on taskbar) that's great ! :)

Cheers !
OK. Glad that you don't mind doing that. I may still look into making this tool recognize if a pipe is active and not create another one, although some might consider that a feature. :)

Return to “Editors”

Who is online

Users browsing this forum: No registered users and 2 guests