Run script or selection from Notepad++

Scripting and setups with Notepad++ and AutoHotkey.
User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Run script or selection from Notepad++

20 Apr 2017, 20:18

This tool lets you run a script directly from Notepad++ whether or not it has ever been saved to a file, making it handy as a quick code tester and for easily running your scripts as you develop them. It will also run a text selection as a script if you just want to test a certain few lines of code. It runs the selected text or a never-saved script through a pipe (thank you to Lexikos for run dynamic script through a pipe and HotKeyIt for the RunTempScript function in same thread). You can set it up to run from the menu, a shortcut key, and/or a toolbar button. Follow the steps below to implement it.

Usage:
Once you have it set up per below as a menu item, shortcut key, toolbar button, or all three, it will do the appropriate one of the following:
  • If text is selected, it will run the selection as an AHK script through a pipe (no file is created).
  • If no text is selected and the file has been saved to a file, it will save it again if there were changes since the last save, then run the script from its saved location.
  • If no text is selected and the file has never been saved, it will run the entire script through a pipe (no file is created).
Setup:
1. Download and unzip the attached file, which contains the script and a bmp image file (only needed if you will be creating a toolbar button).
RunScript_Notepad++.zip
(1.77 KiB) Downloaded 3447 times
(updated 2017-05-05 with version that ensures proper working directory)

2. Create a Menu Item in Notepad++
  • In Notepad++, go to the "Run" menu and select "Run..." (or the equivalent if your NP++ menus are in a language other than English)
  • In the window that appears, click the "..." button to browse and identify the RunScript_notepad++.ahk script you just saved. You'll need to change the file type to "All files : (*.*)" to see .ahk files.
  • After you have selected the file, click the "Save..." button in the "Run..." window. In the "Shortcut" window that appears, enter "Run Script" for the name and select a keyboard shortcut if you'd like (although it's not necessary), then click "OK".
  • Click "Cancel" in the "Run..." window. You should now see a menu item under the "Run" menu called "Run Script". You can now run it from the menu.
3. Create a Button on the Toolbar (Optional)
  • If you haven't already, install the "Customize Toolbar" plugin using the "Plugin Manager".
  • Also if you haven't done so already, in the "Plugins" menu, select "Customize Toolbar" and select "Custom Buttons" (it will have a check mark next to it when it has been selected).
  • Go to the Notepad++ plugin config folder (typically "C:\Users\<username>\AppData\Roaming\Notepad++\plugins\config") and edit the file "CustomizeToolbar.btn". It will probably have two example lines in it if you just enabled Custom Buttons for the first time, which you can delete (if you don't delete them, a hammer icon will appear on the toolbar for each). Add the line below and save the file.
    Run,Run Script,,,Run btn.bmp ("Run" needs to match whatever the menu actually is in your language)
  • Copy or move the "Run btn.bmp" file from the zip file to the config folder.
  • Restart Notepad++. You should see a new button on the toolbar that will run your scripts.
Last edited by boiler on 05 May 2017, 12:28, edited 1 time in total.
User avatar
haichen
Posts: 631
Joined: 09 Feb 2014, 08:24

Re: Run script or selection from Notepad++

21 Apr 2017, 07:43

Work like a charm!
Thanks for this!!
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Run script or selection from Notepad++

21 Apr 2017, 07:55

Hey boiler,

does it the same like the runme plugin?
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: Run script or selection from Notepad++

21 Apr 2017, 08:10

haichen wrote:Work like a charm!
Thanks for this!!
You're welcome!
jNizM wrote:does it the same like the runme plugin?
I assume that plugin doesn't run just the selection if you have selected text. This lets you use the same key or button to run either the the whole file or just the selection if there is some text selected when you invoke it.
User avatar
tdalon
Posts: 41
Joined: 21 Apr 2017, 07:19
Location: Germany
Contact:

%temp% variable changed? Re: Run script or selection from Notepad++

21 Apr 2017, 16:15

Hi.
simply having this script:
#SingleInstance force ; for running from editor

^q::
MsgBox %Temp%
return
run from NotePad++Portable.
And my temp variable is: %temp%/Notepad++PortableTemp.
User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: Run script or selection from Notepad++

21 Apr 2017, 16:55

The value for Temp shows the expected Temp path when I run it. Your issue must have to do with the portable version of Notepad++.
User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: Run script or selection from Notepad++

05 May 2017, 12:34

I replaced the zip file in the first post with a new one that contains an updated script that ensures the default working directory is correct. You can make the change in your copy of the script directly by replacing this line:

Code: Select all

		Run, %Title%
with these two lines:

Code: Select all

		SplitPath, Title,, WorkingDir
		Run, %Title%, %WorkingDir%
Johana
Posts: 189
Joined: 02 May 2017, 02:34

Re: Run script or selection from Notepad++

02 Nov 2017, 16:54

Works awesome, thank you very much for this.
Johana
Posts: 189
Joined: 02 May 2017, 02:34

Re: Run script or selection from Notepad++

02 Nov 2017, 17:02

Johana wrote:Works awesome, thank you very much for this.
SideNote: The customizer plugin only works for 32 bits NP++ :(
TXShooter
Posts: 165
Joined: 13 Dec 2017, 09:27

Re: Run script or selection from Notepad++

05 Feb 2018, 19:46

I decided to start using Versioning in my AHK script names, and all of the sudden when I use this tool, it will only run the previously saved filename that doesn't have a version number, as though it has been cached somehow... having a number (1.43) in the filename seems to be an issue.

As part of my troubleshooting, I've manually launched the versioned file from Explorer without a problem, and have renamed the newest file to exclude a version number, but yet different from the previously saved prior to a version number, which also is not a problem from Notepad++.

Is there something goofy going on with my setup or can anyone else see this issue?

Windows 10 Home
User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: Run script or selection from Notepad++

05 Feb 2018, 21:21

When it's not acting right, what is it showing as the script name in the title bar of Notepad++? And is it showing the full path in the title bar?
TXShooter
Posts: 165
Joined: 13 Dec 2017, 09:27

Re: Run script or selection from Notepad++

06 Feb 2018, 20:44

boiler wrote:When it's not acting right, what is it showing as the script name in the title bar of Notepad++? And is it showing the full path in the title bar?
It's showing the full path from the Root directory all the way to the extension (ie. C:\directory\another directory\name of the script.ahk), which so happens to be in the same directory as the non-numbered and working test file.
User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: Run script or selection from Notepad++

06 Feb 2018, 22:24

That's really strange. I can't see how that could be happening. I can't reproduce it at the moment because I had to get a new computer and have been using Visual Studio Code since.
User avatar
divanebaba
Posts: 804
Joined: 20 Dec 2016, 03:53
Location: Diaspora

Re: Run script or selection from Notepad++

01 Apr 2018, 20:07

Category: Best and most useful plugIn for Notepad++ & AHK
And the the winner is: boiler (applause with standing ovation)
User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: Run script or selection from Notepad++

03 Apr 2018, 12:31

Thank you, divanebaba! I'm glad you find it useful.
User avatar
divanebaba
Posts: 804
Joined: 20 Dec 2016, 03:53
Location: Diaspora

Re: Run script or selection from Notepad++

22 Jun 2018, 17:26

Hello boiler.

That your script is excellent, I've already said. That there is a little bit to optimize, I don't have said, because I've seen this after heavy using.
You probably did not notice, but when the search-window of Notepad++ is open, the script isn't working.

I looked inside for modifying but I get headache by seeing all the DLLCall. :crazy:
For me, modifying your script is impossible.
Maybe for you it is just a little step.

This is not criticism, it's feedback. If the script can capture the open search-window or not, it is still very, very useful.
Recommended to insert into permanent running background-script.
User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: Run script or selection from Notepad++

22 Jun 2018, 18:27

Not tested, but try replacing all occurrences of: ahk_exe notepad++.exe
with: ahk_class Notepad++ ahk_exe notepad++.exe

That should have it find the main Notepad++ window instead of the search window because the search window is of a different class.
User avatar
divanebaba
Posts: 804
Joined: 20 Dec 2016, 03:53
Location: Diaspora

Re: Run script or selection from Notepad++

22 Jun 2018, 21:23

Thank you boiler.

Your suggestion did it.
Now your script is 250 % usefull.
Respect :salute:
toralf
Posts: 868
Joined: 27 Apr 2014, 21:08
Location: Germany

Re: Run script or selection from Notepad++

31 Jul 2018, 16:59

Dear Boiler,

Thank you for this script. I'm new to npp but i got it working. I had to change the line

Code: Select all

WinMenuSelectItem, ahk_class Notepad++ ahk_exe notepad++.exe,, File, Save
to

Code: Select all

WinMenuSelectItem, ahk_class Notepad++ ahk_exe notepad++.exe,, Datei, Speichern
since I have set npp to use german language. I do not know if there is a more robust way, maybe to send the keys {Ctrl+S}.

I have one question: Is there a plan to catch the feedback from AHK on parsing the file? I had that Feature in PSPad with

Code: Select all

/ErrorStdOut "%File%" > "%Temp%\PSPad_run.log"
. PSPAd parsed the log file, so that AHK basically showed in PSPad where the code had issues including the line number, so it was easy to jump to that line from within PSPad.
ciao
toralf
User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: Run script or selection from Notepad++

31 Jul 2018, 17:31

I don't have any plans to add new features, but feel free to do so if you have the time and inclination.

Return to “Notepad++”

Who is online

Users browsing this forum: No registered users and 9 guests