PythonScript icon indicating copy to clipboard operation
PythonScript copied to clipboard

Autostart of py script

Open ComFreek opened this issue 10 years ago • 7 comments

What is the best way to make a script run upon every start of Notepad++?

I currently use the solution below:

# I added the following line in startup.py
exec(open("C:/Users/-----/AppData/Roaming/Notepad++/plugins/config/PythonScript/scripts/AutoTabSpace.py").read())

I don't consider it very elegant:

  1. It relies on a hard-coded path.
  2. It always loads from the user-wide set of scripts. Ideally, one would simply write loadScriptWithName(...) and a matching script would be dynamically loaded (either from the machine-wide or user-wide set of scripts).
  3. One must manually add a line to a file. It would be very nice if scripts could be shipped with configuration files.

Anyway, thanks for the great plugin! It really makes a difference if you don't have to worry about window events, handles or C strings :)


Edit I've just came across the note in the documentation (Plugin Installation and Usage / Startup). After having changed the initializing type to ATSTARTUP, my script still doesn't get run when Notepad++ starts (without the line in startup.py). It works however if I start it manually.

ComFreek avatar Jun 09 '14 15:06 ComFreek

The initialisation type only changes how the plugin starts up, when it's "LAZY", it means python is initialised only the first time a script is invoked (this includes running startup.py). When set to "ATSTARTUP" python is initialised and startup.py is run.

You can do "loadScriptWithName" with a simple import AutoTabSpace as that will search through user,. then machine scripts. You still need to do the import in startup.py though.

How about an autostart directory, either in user or machine scripts (or both), that runs the machine, then the user scripts contained in there? That would mean placing the script in the right place, but I can imagine it would be reasonably useful for a number of use cases. What do you think?

bruderstein avatar Jun 09 '14 17:06 bruderstein

That would be a nice solution, however, I would still love to see something automated like "Plugin Manager" for Python Script.

I've just inspected Plugin Manager's PluginManagerPlugins.xml file and it seems to support arbitrary download and copy instructions. It can also support Python scripts, can't it?

ComFreek avatar Jun 10 '14 12:06 ComFreek

Another problem arises from the fact that Notepad++ only supports keyboard shortcuts for native plugins. What do you think about wrapping Python scripts in native Notepad++ DLLs?

The workflow would be as follows:

  1. A developer creates a Python script for the use with this Python Script extension.
  2. The developer uses the 'wrapper tool' to wrap his Python script in a DLL and therefore in a native plugin.
  3. A potential user installs the native plugin (through Plugin Manager or by manual copying the DLL).
  4. The native plugin extracts the original Python script and registers it inside the Python Script environment. This could be done by simply copying the wrapped script to the Python Script directory.
  5. The user can assign a shortcut to the native plugin.
  6. The native plugin forwards any shortcuts to the underlying Python script. (An interface for this to work is yet to be built.)

Advantages:

  • The plugin can be easily incorporate in the PluginManager. → The user gets automatically informed about updates,
  • A shortcut is assignable.

ComFreek avatar Jun 10 '14 18:06 ComFreek

This exists - F. R. Boyer wrote this: https://sourceforge.net/projects/npppythonplugsq/ (albeit without the PM integration)

bruderstein avatar Jul 15 '14 22:07 bruderstein

Can't this be closed? if I understand correctly, then

  • shortcuts can be already assigned to scripts directly
  • to run multiple scripts from within startup.py is a matter of writing it "correctly" in the first place and then use import statement and
  • concerning a possible manager for scripts, why not using python? Isn't that why we love python because it is easy to do? A workflow might be like this.
  1. A new script gets added to samples directory
  2. PS uses a python script to check the sample directory for changes and acts accordingly

Ekopalypse avatar Oct 27 '19 21:10 Ekopalypse

to run multiple scripts from within startup.py is a matter of writing it "correctly" in the first place and then use import statement

What is the "correct" way? I have a method using exec but I have never felt it was the "right" way, although it works...

sasumner avatar Oct 28 '19 18:10 sasumner

A script is from Python's point of view a module, so I would say import is the correct way. If scripts are written having this in mind, each script could have a method main which executes the script.

startup.py would then contain

import MyScript MyScript.main()

or if you trust yourself and the source just import and execute it within MyScript.

Ekopalypse avatar Oct 28 '19 19:10 Ekopalypse