Community
    • Login

    Running external app on window content?

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    3 Posts 2 Posters 219 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Lars KvanumL
      Lars Kvanum
      last edited by

      Hi.

      Long time user, first time poster. I hope someone in here can help me out.

      I use notepad++ for coding and as python’s reliance on indents instead of brackets in the code, I use reformatting of code pretty often. Is it possible t ohave external apps like autopep8 or black change the contents in the active window? Or are there other pluging that does this? I’ve been searching, but haven’t been able to find anything - and I don’t want to switch to VS code…

      Thanks!

      PeterJonesP 1 Reply Last reply Reply Quote 0
      • PeterJonesP
        PeterJones @Lars Kvanum
        last edited by

        @Lars-Kvanum ,

        It’s quite easy to do from inside a plugin.

        The example plugin “pork2sausage”, which is available in Plugins Admin, actually demonstrates this. You use its Plugins > Pork to Sausage > Edit User Commands, and set up the path to the external program (autopep8 or whatever), the command line required, and a working directory for temp files. You can see the examples shown when the plugin is installed for how to do it (and the config file explains the various parameters needed. It works on the selection, rather than the whole file, but if you select-all first, that will work.

        Alternatively, since I use NppExec for other things, I use that, rather than pork2sausage, for my “filtering” of active data. One example I have is decrypting a file using GPG:

        cls
        npp_save
        cmd.exe /c exit %RANDOM%														// cannot access $(SYS.RANDOM) directly through NppExec, but can tell cmd.exe to return it as a value
        set tempfile = $(SYS.TEMP)\NppGpgFile_$(EXITCODE).tmp							// create random tempfile name
        set ascfile = $(SYS.TEMP)\NppGpgFile_$(EXITCODE).asc							// create associated ascfile name
        sci_sendmsg SCI_SELECTALL														// select all
        sel_saveto $(ascfile) :a														// save selection to the ascfile	(use :a to save as ansi, to prevent unicode prefix ÿþ getting embedded)
        gpg.exe --output "$(tempfile)" --decrypt "$(ascfile)"							// decrypt
        sel_loadfrom $(tempfile)														// replace selection with results
        sci_sendmsg SCI_DOCUMENTSTART													// deselect
        rm -rf "$(tempfile)" "$(ascfile)"												// cleanup temp files
        npp_save
        

        So the first clears the NppExec console (not really needed), then saves the active file; to avoid overwriting things, it generates a random number, and uses that for setting up a few tempfile names in the %TEMP% direcotry, then it selects everything in the active document and writes it to a temporary document, then runs it through gpg.exe into a temporary output file, then takes the contents of that output and overwrites the current buffer.

        This actually makes it look more confusing that it really is, but if autopep8 gets its input from a file passed as an argument, and can output to another file defined as an argument, this is one way to accomplish it.

        1 Reply Last reply Reply Quote 4
        • PeterJonesP
          PeterJones
          last edited by

          Ah, right. I’ve written so many FAQ for the Community, I forgot FAQ: How do I … Convert my Text exists. That might explain things better, since it had more time put into writing it.

          1 Reply Last reply Reply Quote 3
          • First post
            Last post
          The Community of users of the Notepad++ text editor.
          Powered by NodeBB | Contributors