Run script or selection from Notepad++

Scripting and setups with Notepad++ and AutoHotkey.
User avatar
rediffusion
Posts: 58
Joined: 15 Mar 2019, 05:16

Re: Run script or selection from Notepad++

Post by rediffusion » 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++

Post by Splongus » 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++

Post by jyloup » 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++

Post by jyloup » 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: 16767
Joined: 21 Dec 2014, 02:44

Re: Run script or selection from Notepad++

Post by boiler » 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: 16767
Joined: 21 Dec 2014, 02:44

Re: Run script or selection from Notepad++

Post by boiler » 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. :)
nomi69
Posts: 2
Joined: 05 Mar 2021, 13:04

Re: Run script or selection from Notepad++

Post by nomi69 » 05 Mar 2021, 13:12

I use this version of NPP:
Notepad++ v7.9.1 (64-bit)
Build time : Nov 2 2020 - 01:07:46
Path : C:\Program Files\Notepad++\notepad++.exe
Admin mode : OFF
Local Conf mode : OFF
OS Name : Windows 10 Pro (64-bit)
OS Version : 2009
OS Build : 19042.804
Current ANSI codepage : 1252
Plugins : ComparePlugin.dll DSpellCheck.dll JSMinNPP.dll LanguageHelp.dll mimeTools.dll NppConverter.dll NppEventExec.dll NppExec.dll NppExport.dll NppSaveAsAdmin.dll RunMe.dll XMLTools.dll _CustomizeToolbar.dll

I follow the first post and it's ok for installation.

But when I try to run script, a new windows open with RunScript_Notepad++.ahk open inside and ... that all!

Something wrong? Don't work with 64-bit?
User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: Run script or selection from Notepad++

Post by boiler » 05 Mar 2021, 13:51

Does that happen with any script you run? It sounds like you may have associated the extension .ahk with Notepad++ (or whatever window opened up with the script in it) instead of with AutoHotkey.exe. You don’t want to associate your editor with opening the file, such as when you right-click on the file and select “Open file” or simply double-click on it. It should be associated with editing it.

If you opened a .ahk file using “Open with...” and chose Notepad++ (or something else) and checked the box saying “Always use this app to open .ahk files”, that would cause it to open for editing in that app instead of running it. There’s a different way that you associate the program that you would use to edit the file than the one you would use to “open” the file.

See this page on how to check and change your default file associations. Go to the section “Change default apps by file type” and see what app .ahk files are shown to be associated with. You need it to be associated with AutoHotkey.exe.
nomi69
Posts: 2
Joined: 05 Mar 2021, 13:04

Re: Run script or selection from Notepad++

Post by nomi69 » 08 Mar 2021, 04:54

boiler wrote:
05 Mar 2021, 13:51
Does that happen with any script you run? It sounds like you may have associated the extension …
OMG, of course I do that :oops: . Sorry.
I modify the default association as you mention and it's OK now.
User avatar
divanebaba
Posts: 804
Joined: 20 Dec 2016, 03:53
Location: Diaspora

Re: Run script or selection from Notepad++

Post by divanebaba » 20 Nov 2021, 04:42

Intensive use of this excellent script prompted me to extend its functionality to the browser and he AHK-Help-File.
I realized this by adding and supplementing one line each.
With this change I am now able to execute code directly from the ahk-forum.

Code: Select all

SetTitleMatchMode, 1
WinGetClass, OutputVar, A ; <--- BY DIVANEBABA  -  ADDED to determine class of active window
StatusBarGetText, StatusBarText, 3, ahk_class Notepad++ ahk_exe notepad++.exe
ClipSave := ClipboardAll
RegExMatch(StatusBarText, "(?<=Sel : )\d+", SelectionLen)
if (SelectionLen || OutputVar = "Chrome_WidgetWin_1" || OutputVar = "HH Parent") ; run the selected text only in a named pipe ; <--- BY DIVANEBABA  -  SUPPLEMENT class of your Browser and help-file. In this case my browser is CROME 
{
Now you can click inside ahk-forum to button "Select all" inside code section an execute the selected code.
As I'm not a professional programmer, maybe someone else, or you @boiler, could implement this fuctionality in a better way than me.
User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: Run script or selection from Notepad++

Post by boiler » 20 Nov 2021, 09:17

That's nice to see its use extended in this way to make it more useful for you. You could even make it so that the selected text isn't limited to Notepad++, Chrome, or anything else, which would allow any selected text to be run as an AHK script in a named pipe when a hotkey is pressed, but your implementation obviously supports how you want to use it, so good job with that. Your approach to accomplish it is probably fine as is.
sofista
Posts: 645
Joined: 24 Feb 2020, 13:59
Location: Buenos Aires

Re: Run script or selection from Notepad++

Post by sofista » 22 Nov 2021, 07:57

divanebaba wrote:
20 Nov 2021, 04:42
Intensive use of this excellent script prompted me to extend its functionality to the browser and he AHK-Help-File.
I realized this by adding and supplementing one line each.
With this change I am now able to execute code directly from the ahk-forum.
Pretty cool, thanks for sharing!
User avatar
lmstearn
Posts: 688
Joined: 11 Aug 2016, 02:32
Contact:

Re: Run script or selection from Notepad++

Post by lmstearn » 23 Nov 2021, 04:21

@divanebaba: What a super way to run a script- if it can be somehow ported to JS for a Run option in the browser as well, making it an optional feature for the poster of the code is even better.
@Boiler, thanks, noted of your move over to VSCode- might follow that idea sometime soon. Just a weird gotcha with #include this end with-
#Include file "MyScript.ahk" cannot be opened.
Just the following

Code: Select all

#include MyScript.ahk
works if double-click from explorer shell, but not from OP script or N++ RunMe (admin)

Code: Select all

#Include %A_ScriptDir%
#include MyScript.ahk
works with RunMe, but not with OP script

Code: Select all

#include <Full Path>\MyScript.ahk
works with everything. By looking at the script it isn't obvious at all how to correct it, sorry.
Edit: There might be a trick with NPPExec - will try that. :)
:arrow: itros "ylbbub eht tuO kaerB" a ni kcuts m'I pleH
User avatar
Stem75
Posts: 57
Joined: 18 Dec 2021, 02:37

Re: Run script or selection from Notepad++

Post by Stem75 » 04 Feb 2022, 19:15

boiler wrote:
05 Mar 2021, 13:51
Does that happen with any script you run? It sounds like you may have associated the extension .ahk with Notepad++ (or whatever window opened up with the script in it) instead of with AutoHotkey.exe. You don’t want to associate your editor with opening the file, such as when you right-click on the file and select “Open file” or simply double-click on it. It should be associated with editing it.

If you opened a .ahk file using “Open with...” and chose Notepad++ (or something else) and checked the box saying “Always use this app to open .ahk files”, that would cause it to open for editing in that app instead of running it. There’s a different way that you associate the program that you would use to edit the file than the one you would use to “open” the file.

See this page on how to check and change your default file associations. Go to the section “Change default apps by file type” and see what app .ahk files are shown to be associated with. You need it to be associated with AutoHotkey.exe.
For newbies like me.
Because i use Adventure IDE as the default editor for .ahk files (and notepad++ sometimes), i changed the extension of the script to .ahka and associate it with
AutoHotkeyU(32 - 64).exe
so i open all .ahk files with double click in IDE and the script runs with AutoHotkey from Notepad++ when i select run script.
User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: Run script or selection from Notepad++

Post by boiler » 04 Feb 2022, 19:58

I’m not sure I understand what you’re saying. Did you purposely associate the .ahk extension with your IDE so it will open with the script in the IDE when you double-click it? Is that just because it’s faster to double-click than right-clicking on it and selecting Edit? Or did you not know you could associate “Edit” with the IDE and leave the main association for “Open”/double-click with the AHK binary?

Then is your process that when you consider a script complete, you rename it with a .ahka extension so that you can run the script by double-clicking it?
User avatar
Stem75
Posts: 57
Joined: 18 Dec 2021, 02:37

Re: Run script or selection from Notepad++

Post by Stem75 » 05 Feb 2022, 04:36

Yes i associate the .ahk extension with the IDE because it’s faster to double-click it and edit.

When i consider a script complete i compile it into an exe leaving the original .ahk as a backup for edit.

I've renamed only YOUR script with a .ahka extension so it can run from notepad without opening the IDE as a workaround to my setup.

Maybe i am doing something wrong but:

No i don't know how to set (edit and open with) differently. When i select open with from the right click i have one option, to choose one program (from control panel the same). From the link you provide i can't understand how to do it.

My setup is portable.

AHK portable
Adventure IDE portable
Notepad++ portable

So this might be a problem.

Can you drop some light as to how can i set them both with a quick example.

Thanks in advance.
User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: Run script or selection from Notepad++

Post by boiler » 05 Feb 2022, 07:48

Stem75 wrote:
05 Feb 2022, 04:36
No i don't know how to set (edit and open with) differently. When i select open with from the right click i have one option, to choose one program (from control panel the same). From the link you provide i can't understand how to do it.

AHK portable
Adventure IDE portable
Notepad++ portable

Can you drop some light as to how can i set them both with a quick example.
I’m not understanding why you want to set it up to run scripts from Notepad++? Why not just set it up so Adventure is the default editor that the script opens in when you select “Edit” for a .ahk file when right-clicking on it in File Explorer and have the script just run immediately when selecting “Open” or double clicking it? I understand that you weren’t aware that could be done before, but now that you know, is there a reason to use Notepad++ to run scripts rather than set it up the way everyone else does?

To set up the default editor, you need to change the registry. There is a script called “Default Editor.ahk” in the Adventure “Tools” folder that will present you some options and do that for you. You can just change the registry yourself if you prefer. Or you can run a one-line AHK script (or command line in a CMD window if you prefer) that will change the registry for you as shown for Notepad++ in the “Default Editor” part of this post by jNizM. You will have to change to path to the actual path to your editor of choice, whether it’s Adventure or Notepad++.
User avatar
Stem75
Posts: 57
Joined: 18 Dec 2021, 02:37

Re: Run script or selection from Notepad++

Post by Stem75 » 05 Feb 2022, 09:47

I think i am misunderstood here.

First of all thanks for the tip. Always learning from you!

I am doing what i am doing because i don't want to change anything from my setup and because (probably wrong) i thought that nomi69 was asking for something like this.
I am just trying to setup notepad++ this way for testing purposes, to see if it feels better and to have them all in one place with documents editing (.txt .ini .py .ahk....) and your script is one that raises the percentage towards it because it works fine.
In the future i might have it as default.

So. No suggestion here just saying...

And after all i am a newbie!!! If i don't do this things who else...... :lol:
User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: Run script or selection from Notepad++

Post by boiler » 05 Feb 2022, 09:57

Well, for sure, do whatever works best for you. I just wanted to make sure you were aware of how it typically would be set up and how to do it.
User avatar
Stem75
Posts: 57
Joined: 18 Dec 2021, 02:37

Re: Run script or selection from Notepad++

Post by Stem75 » 05 Feb 2022, 10:09

Now i know better. Thank you for that.
Post Reply

Return to “Notepad++”