Menu

Run pythonscript outside npp

Help
liloojedi
2015-09-18
2019-04-13
  • liloojedi

    liloojedi - 2015-09-18

    hello,

    I would like to run a pythonscript script without having to open notepad++. Unfortunately, I get an "ImportError: no module names Npp" on execution. I have tried moving the script around without success.

    Thank you for guiding me into success.

     
  • Dave W.

    Dave W. - 2015-09-19

    What is the function of this "pythonscript script", if not to automate some function in Notepad++?
    What would be the point of running it outside of NPP?

     
  • liloojedi

    liloojedi - 2015-09-21

    The fuction of my script is indeed to automate some function that uses Notepad++. The script I wrote opens all the files in a folder path (writen in the script) and does some treatment using TextFX then closes all the files. I would like to do this with a simple double click on the script and not have to open Notepad++, browse through the menu then click on my script to only close Notepad++ right after.

     
    • Dave W.

      Dave W. - 2015-09-21

      OK, I guess I mis-read your statement. I read it as you wanted to run the
      script completely outside of Npp, like just in standard Python.
      So, it's not that you don't want Npp to open at all, you just don't want to
      have to open it MANUALLY. Obviously, it has to "open" (start up) to load
      PythonScript to run your script.
      I've been playing around with this some ( slowly, since I've got 7 stitches
      in my right middle finger at the moment. Cut it pretty bad out in the
      garage Sat. night. Long story...)

      I have an idea for a "simple" way to do this, without having to resort to
      full-blown XMLRPC or such, that I will pass along to you for your
      consideration.
      If anyone has a better idea or knows of a script/facility already available
      to do this more efficiently or simply, please speak up. ;)

      First of all, Npp has some commandline options you can use to pass
      information to Npp when it starts up.
      Fortunately, PythonScript can read those CLI options or at least some of
      them.
      UNfortunately, they are not all preserved, in various cases - not even the
      filename.
      And it throws away anything it doesn't 'understand'. So you cannot pass
      arbitrary information after the filename option and then retrieve it using
      the PythonScript notepad.getCommandLine() function. All you get is SOME of
      the -options like -nosession and/or -systemtray. Sure would be nice if it
      would pass that filename in...
      Oh well.
      Worse, if you pass a non-existent filename, it prompts you and says it
      doesn't exist, and asks if you want to create it. That's no good.
      So, what I did is create a zero-length file named "RunMyPythonScript1.txt"
      in my home directory and issued the following commandline in the Start->Run
      window:

      notepad++ -nosession MyPythonScript1.txt

      This opens Notepad++ and loads my 'dummy' file.
      I use -nosession because I don't want ANY other files to open.
      I then opened the PythonScript console and issued the
      "notepad.getCommandLine()" function.
      Sure enough, I get '"C:\Notepad++\notepad++.exe" -nosession'
      NO FILENAME. Damn.
      But, I can call "notepad.getCurrentFilename()" and get it. I can then parse
      it for the filename portion and test it.
      Why did I call it "RunMyPythonScript1.txt"?
      Because I want the filename to be a flag to my secondary PythonScript
      startup file (that will have to be written if you don't already have one)
      that I want it to run "MyPythonScript1" or whatever.
      (The less modification you do to your primary startup.py file, the better.)
      So here's what has to happen:
      1. Make sure your PythonScript startup option is set to ATSTARTUP, not LAZY.
      You want your PythonScript to always startup immediately and call your
      secondary startup file to check the current filename for (one of) your
      script(s) to run.
      2. Create the secondary startup file that will be called by your startup.py
      module. It will need to get the current filename, using the
      notepad.getCurrentFilename() command, parse it out to see if it starts with
      "RunMyPythonScript" or whatever you decide to call it, get the number
      suffix, and run the corresponding script. Or you could just name it the
      same as the script you want to run, and then run it using
      notepad.runMenuCommand(). That part is up to you.
      3. Your target functional PythonScript will need to call:
      notepad.runMenuCommand('File', 'Exit')
      when it finishes. I have tested this from the console command line and it
      works, so I believe it would work in a script.
      4. If you expect to already have an instance of Npp running, then you will
      need to make sure your startup commandline includes the -multiInst option.

      Now, do you mind if the GUI shows up on the screen while your script runs,
      or do you require that it not show at all?
      If you don't want it to show at all you can use the Npp commandline options
      "-x10000 -y1" to immediately place the window outside of the visual GUI
      range when it opens. It will still show up in the taskbar, but you will not
      see it on the screen.

      You would then need to create a Windows command file or batch file
      containing the commandline corresponding to the PythonScript you want to
      run, and place a shortcut to it on your desktop or some convenient location
      to double-click on to run it.

      That's about it. I hope this gives you a boost toward getting your solution
      in place.

      Best regards,
      Dave W.

       

      Last edit: Dave W. 2015-09-22
  • Sundeep

    Sundeep - 2019-03-31

    HI Dave,

    Could you please explain in detail as i was bit confused. I am new to python.

    I want to run this script from noteapd++ periodically via bat file. Please suggest

    import sys;
    from Npp import notepad

    filePathSrc="C:\Users\vnsan\Desktop\Unicode\" # Path to the folder with files to convert

    for root, dirs, files in os.walk(filePathSrc):
    for fn in files:
    if fn[-4:] == '.txt': # Specify file types, taking care to change the fn[number] to correspond to length of the file's extension including the .
    notepad.open(root + "\" + fn)

              notepad.runMenuCommand("Encoding", "Convert to ANSI")
             notepad.save()
             notepad.close()
    
     
  • Dave W.

    Dave W. - 2019-03-31

    Hey man. How ya doin'? ;)
    I just woke up, but I did a cursory re-reading of that post and think it may be overkill for what you need. I think there's a simpler way. Lemme get some coffee in me and I'll be back.

     
  • Sundeep

    Sundeep - 2019-03-31

    Thanks, Please share the same. Muchappreciated

     
    • Dave W.

      Dave W. - 2019-03-31

      Well, it appears I have a major problem here. I just got a new PC a week ago, Win 10 Pro 64-bit, I installed the latest version of Npp, and now I can't install PythonScript. Apparently all the plugin handling changed drastically at version 7.6.3, and I have version 7.6.4.
      The old Plugin Manager fails to install it now, and the PythonScript MSI does not work any more. It appears to, it seems to install all the PS modules and folders, but then when Npp restarts it says, module GUP.exe is missing and can't load any plugins. It's been a good while since I messed with this stuff, so it may be a while before I get it all set back up properly.
      There's a whole long discussion of the plugin 'problems' over in Npp Community I'm going to have to read through. I'll likely have to install PS manually I guess, if that's even possible now. I hope so.
      I hate to make suggestions that I can't test myself, but at at any rate, I'll go ahead and say this.
      I wrote the above narrative as a generalized approach to starting up ANY python script by simply double-clicking a batch (.cmd) file.
      All YOU need to do however is set up a simple bat file that starts up Npp and points it to a specific PythonScript (.py) file. Then, all you need to do is create a Windows Task Scheduler task to kick off that bat file hourly.
      DO NOT mess with "Threading" and timer. That is infinite overkill for this. And being new to Python, as you mentioned, just forget that for now.

      In the above narrative, points 1, 3, and 4 DO still apply (if those haven't changed in Npp as well, due to the Win 10 64-bit issues). I would imagine they are still OK. Also, the note about the GUI being visible or not should still be valid.
      Hope this helps. I'll check back on it when I get up and going with PS again. Wish me luck... ;)

       
  • Sundeep

    Sundeep - 2019-04-01

    HI,

    Thanks for info, I have understood that Bat file need to point out to a specfific file (Baed on inital post). How i could not able to point the same. PLease can you help me with the code.

    I Work on SAP, some files genereate from SAP and store in windows network path and file format is unicode, i hvave to change the format for all these files periodically.

    Since i never worked on python, can you please write a code in the file (for bat how to point out) and provide. Also am not sure, how to make chnages based on points 1 , 2& 3.Also please provide the code for that well (much appreciated). I have installed latest 7.6.4 npp only and i have add the python script plugin and tested the script manually and it worked for me.

     
    • Dave W.

      Dave W. - 2019-04-01

      No, I will not write your code for you. That's not how this works.
      It's a simple change to the PythonScript config file startup option as noted in point 1 above.
      Point 2 is not required.
      Points 3 and 4 are self-explanatory as well.
      Then, a simple, one-line Windows batch (command) file, and a simple Windows Task Scheduler task. There is ample documentation available for both.
      The direction and guidance provided thus far should be sufficient.

       
  • Sundeep

    Sundeep - 2019-04-13

    HI Thanks,

    I was able to wirte it and scripts works fine. Howeevr i need an help on one thing. Let suppose if i want to convert the files under the folder1 ( C:\A\folder1) and move those files to folder2 (C:\A\folder1\folder2) it worked fine. however at the same time next time scipt runs it is checking for the file even under folder2 which i don't want again......can you help on this to only serach files under folde1 and not subfolders.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.