FAQ: How to install and run a script in PythonScript
-
Hello, and welcome to the FAQ Desk. You have likely been directed here because someone provided you with a script for PythonScript, and you need instructions on how to install and run the script.
Please read this FAQ in its entirety before trying to follow the instructions. You may miss some important concepts by trying to just quickly follow the instructions without having read this whole FAQ. If you have problems at any stage in this process, go back and re-read this entire FAQ and make sure you haven’t missed anything.
This FAQ is in three sections:
- Installation ⇒ Describes how to install and configure the PythonScript plugin, and how to create a new script to paste the contents of a script that you are copying from somewhere else
- Usage – How to Run the Script ⇒ Describes how to run the script, once you’ve installed the plugin and created the script with its contents.
- Footnotes ⇒ Gives additional details. The FAQ will link to the appropriate footnote with a link like Footnote N, or you can just scroll down to that section to see the footnote referenced.
Installation
These are the installation instructions. If you want to know how to run the script, that is described in the Usage section, farther down in this FAQ.
- Install PythonScript plugin, if you don’t have it already
- There are two versions of the PythonScript plugin – you can install either of them (though some scripts might take some tweaking to work in one or the other).
- PythonScript 2.0.0: This version of the plugin uses the outdated Python 2.7 at its core, but most of the scripts found in the forum were written for and tested with this version of the plugin. To install it:
- Plugins > Plugins Admin
- checkmark
PythonScript
- click Install
- Recommended: After Notepad++ restarts
- go to Plugins > Python Script > Configuration…
- checkmark ☑ Automatically open console on error – this will make sure that if there’s a syntax error or runtime error in the script you run later, the console will be shown so that you can see the error
- set Initialisation: to
ATSTARTUP
so that your startup script will be properly run right when Notepad++ is loaded. (this is also recommended in the startup script section (below)
- PythonScript 3.0.x: This version of the plugin uses a recent Python 3.y version (for example, plugin 3.0.18 uses Python 3.12), which matches modern Python, but some scripts will need to be modified slightly to work with the newer syntax. To install it:
- Close all instances of Notepad++
- Go to https://github.com/bruderstein/PythonScript/releases
- Go to the newest v3.0.y “pre-release” in the list
- Under the “Assets” for that version, pick the “PythonScript_Full” for PluginsAdmin (or whichever variant suits your fancy, really) – making sure to get the x64 version if you have 64-bit Notepad++
- Unzip the contents into the
<npp_install_directory>\Plugins\PythonScript
directory, so the two DLLs are at<npp_install_directory>\Plugins\PythonScript\*.dll
, and the subdirectories from the zipfile are in subdirectories of that directory - Run Notepad++
- Create a new script
- Plugins > Python Script > New script
- Give it the name that was suggested in the original post, or something that makes sense to you. I will call it
ProvidedScript.py
for purposes of this FAQ entry. - It should be placed in your “user scripts” directory (see Footnote 1) … If it doesn’t default to that location, then change to the correct location.
- Save
- Populate the script
- Copy the text from the script that was provided to you, verbatim (including spacing/indentation… that is critical): copy/paste is your friend.
- Caveat: many scripts posted in the forum end up in a grey or black box; if there is a scroll bar on the right side of that box, please make sure you scroll through and grab the whole script, not just the first “page” worth.
- Paste in the
ProvidedScript.py
file - Save
- Copy the text from the script that was provided to you, verbatim (including spacing/indentation… that is critical): copy/paste is your friend.
- If you want to give it a keyboard shortcut or add it to the editor’s right-click context menu:
- Plugins > Python Script > Configuration…
- Select User Scripts
- Select
ProvidedScript.py
- Click the left Add to add the script to the Menu items table
- Note: it needs to be added to this list to be able to be accessed via Notepad++'s Shortcut Mapper
- OK
- Exit Notepad++ completely and restart the application
- Plugins > Python Script will now list
ProvidedScript
- To give it a keyboard shortcut:
- Settings > Shortcut Mapper
- select the Plugin commands tab
- Filter =
ProvidedScript
- Click on
ProvidedScript
in the list, Modify, and set the shortcut as desired, OK - Click Close
- Now that shortcut will be assigned to the
ProvidedScript
script
- Settings > Shortcut Mapper
- To add it to the editor’s right-click context menu
- Choose Edit Popup contextMenu from the Settings menu (noting the info about having to restart Notepad++ for changes to be realized)
- A file named
contextMenu.xml
will open into a new tab - Insert this line between existing lines of the XML, as desired for your menu:
<Item PluginEntryName = "Python Script" PluginCommandItemName = "ProvidedScript" ItemNameAs = "Select to run ProvidedScript" />
- save
contextMenu.xml
- Exit Notepad++ completely and restart for that entry to show up in the context menu.
If the script’s author mentioned putting some commands in your “startup script” or
startup.py
, then between steps #2 and #3, you will need to do the following:- Plugins > Python Script > Scripts
Ctrl+click
(see Footnote 2) onstartup (User)
, which will edit that file- NOTE: not
startup
by itself, which is the “Machine scripts” version (see Footnote 1): you should never need to edit the “machine scripts”startup.py
- If
startup (User)
does not exist, create a New Script as before, but call this onestartup.py
, and save it in the same directory asProvidedScript.py
- NOTE: not
- Put in any code that they recommended for your
startup.py
script - Save
- Plugins > Python Script > Configuration…
- Make sure the Initialisation pulldown is set to
ATSTARTUP
- OK
- See Footnote 3 for info on Import search paths
It is also highly recommended that you go to Plugins > Python Script > Configuration… and checkmark ☑ Automatically open console on error – this way, if something goes wrong, the error message will be visible (so you can ask the original author of the script for help with the error message and details of what went wrong)
Usage – How to Run the Script
You need to start by doing any preparatory work suggested in the original post, including but not limited to:
- opening the right file(s)
- maybe editing the script to customize some regular expression
Run the script using one of the three methods below:
- If you followed step #3 from Installation, you can just use the keyboard shortcut you defined in the Shortcut Mapper
- If you followed step #3 from Installation, you can access the script through Plugins > Python Script > ProvidedScript
- Whether or not you followed step #3 from Installation, you can access the script through Plugins > Python Script > Scripts > ProvidedScript
Footnotes
- PythonScript directory structure is described below. You will want the “User Scripts” when following these instructions:
- “Machine Scripts” go in the
...\Plugins\PythonScript\scripts\
directory:- These scripts will be accessible by all users on the same machine
- For a normal installation, it will be relative to your installation folder (often
C:\Program Files\Notepad++\
or similar) - For a portable installation, it will be relative to your portable folder (whichever folder contains
notepad++.exe
) - Unless you are on a multi-user computer and know what you are doing, there is no reason to create “machine scripts”
- “User Scripts” go in the
...\Plugins\Config\PythonScript\scripts\
directory:- These scripts are individual to the given user
- For a normal installation, it will be relative to your AppData folder, and you can paste
%AppData\Notepad++\Plugins\Config\PythonScript\scripts\
into your Save dialog to get it to change to the right directory - If you have your settings in the Cloud or redirected to another folder based on a command-line-option, as described in the Online User Manual’s “Configuration Files Location” section, you will need to go to the
...\Plugins\Config\PythonScript\scripts\
folder relative to the settings folder - If you have a portable, it will be in
...\Plugins\Config\PythonScript\scripts\
relative to your portable folder (whichever folder containsnotepad++.exe
)- Note the difference in this path compared to the machine scripts: it has
Config\
as an extra level of directory
- Note the difference in this path compared to the machine scripts: it has
- “Machine Scripts” go in the
- General PythonScript hint:
Ctrl+Click
on any script name in the Scripts menu to edit that script. - If you are using the user
setup.py
to load a script with animport
statement, if you are getting errors about not finding the module to import, try something like the following near the top of the usersetup.py
, which will make sure the Python interpreter can see all of your user scripts so that it can import them.import os for (root, dirs, files) in os.walk(notepad.getPluginConfigDir() + r'\PythonScript\scripts', topdown=False): if root not in sys.path: sys.path.append(root)
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-