How do I add "input" to my macro, to replace text in Notepad++?

8,507

Solution 1

First:

  1. Go to the "Plugins" drop-down menu.
  2. Open the Plugins Manager.
  3. Download PythonScript for Notepad++.
  4. Via the prompt, restart Notepad++.

Next:

  1. Go to Plugins in the drop-down menu.
  2. Click PythonScript -> new script.
  3. Save the script with a .py extension.

Copy and paste this code, then edit it:

#***   IMPORTS   ***
from Npp import *

#*** DEFINITIONS ***
#*** http://docs.python.org/release/1.4/tut/node70.html (.h triple-string variables)***
initial_string="""

  THIS IS A HUGE
  STRING
  WITH COMMENTS
  AND FUNCTIONS
  AND
    NESTING varr
    MISSPELLEDD WORDS
    VARIABLES Varr
  ALL KINDS OF CRAZY STUFF""" 

#***  I am using a case-sensitive replacement;     ***
#***  therefore, I use two replacement operations  ***

#***  First replacement, lowercase ***
print initial_string
user_replacement_string=notepad.prompt(notepad, 'Replace', "")

#*** Second replacement, uppercase ***
editor.replace("varr", user_replacement_string) 
user_replacement_string=notepad.prompt(notepad, 'Replace', "")
editor.replace("Varr", user_replacement_string)

Save.

Now, create a new file ctrl-n and test the script. Go to Plugins->PythonScript->Run Last Script. If it works, you can put it to action in the file you're working on.

For convenience, I made ctrl-shift-e a shortcut key for the Run Previous Script (#yourscriptname.py) command. In your drop-down menu, go to Settings->Shortcut Mapper. Click the Plugin commands tab. Near the end, Run Previous Script. Very handy.

Interestingly, you can use this script to duplicate itself for different patterns. I would really like to know how to make that possible. Please see the edit on my question.

IMPORTANT EDIT

The below code will make it so that your Notepad++ will create its own scripts. Run this script, and save the file in your c:/program files/Notepad++/plugins/config/PythonScript/scripts directory {Windows default}:

#imports
from Npp import *
#variables
pattern_string=notepad.prompt(notepad, 'Pattern String')
number_of_replacements=int(notepad.prompt(notepad, 'Number of Replacements'))
variable_to_replace = []
replacement_title = []
the_header = """#imports
from Npp import *

#definitions
"""
a = "pattern_string = """ + '"' + '""' + pattern_string + '""' + '"'
b = """

print pattern_string
user_replacement_string"""
c = """=notepad.prompt(notepad, 'Replace', "Replaces: """
d = '"' + """)
editor.replace(""" + '"'
e = """", user_replacement_string"""
f = ")"
#function
for i in range (0, number_of_replacements):
    replacement_title.append(str("Replacement Item ") + str(i+1))
    variable_to_replace.append(notepad.prompt(notepad, replacement_title))

print the_header + a
print b + str(i) + c + variable_to_replace[i] + d + variable_to_replace[i] + e + str(i) + f
#thanks Wolfpack08.

It will make it so that you can import strings that you often paste into your file, along with variables that you often have to replace. Write your input pattern with variables (i.e.,

This is my stupid input pattern foo, and I use the common variable bar.

When you run the script, you specify that input pattern in the first pop-up. You then specify the number of variables in the input pattern: foo and bar (e.g., "2" variables). In the resulting pop-up input boxes, you specify the variables in the order you would like to replace them in: ("", "").

I didn't put a 'default value' function in because I thought it would be annoying to replace what I can only imagine being truly inside of lexis. I think it would be very useful to have default values for people who do a lot of data entry and need this kind of macro, though. If anyone makes a for one, I will add it.

Solution 2

Important note: this breaks once for each array item that contains spaces within itself. It also breaks if the second variable contains the first variables (e.g., [(bird), (birds)] and [(var), (variables)] do not work in place of [(foo), (bar)]).

Assume you have a file you want to paste data into. I'll call it the "Left View"/Operative. Create a new file. Right click the new file's tab and click "Move to other view". I'll designate this the "Right View"/"Scratchpad". In the file, put down the pattern and the array of replacement strings, like this.

#Title: Scratchpad
#pattern
I replaced (foo) and (bar) in this pattern.

#array
toe, foot; nose, head; ...; m, n

Now, go to the drop-down menu, Macro. Click on "Record macro" button and execute the following actions, starting in the Scratchpad.

  1. Highlight the pattern in the scratchpad press ctrl+c.
  2. Place your cursor where you want each new string to be pasted in the operative (ideally, click the operative and press ctrl+end or ctrl+home). Press ctrl+v.
  3. In the scratchpad, set your cursor to the beginning of the array. Press ctrl+shift+right. Press ctrl+x.
  4. Click on the operative. Press ctrl+h. Input 'foo' into the find pattern. Highlight the replace pattern and press ctrl+v. Press alt+a.
  5. Click on the beginning of the array (should now start with a comma). Press ctrl+right. Press delete. Press ctrl+right. Press ctrl+x.
  6. In the operative, ctrl+h. Find string foo. Replacement string ctrl+v. Alt+a.
  7. In the scratchpad, place the cursor at the beginning of the array (should begin with a semicolon). Press ctrl+right. Press delete.

Now, Macro->stop recording. You can now replay the macro. It will paste the pattern, eat 2 of your array values up, and replace foo and bar with those array values, every time it loops through. That is, of course, assuming the variables are named well, and the array items do not contain spaces. It's a very nice solution because it helped me to get used to the utility of the macro function.

Share:
8,507

Related videos on Youtube

Wolfpack'08
Author by

Wolfpack'08

Updated on September 18, 2022

Comments

  • Wolfpack'08
    Wolfpack'08 over 1 year

    I'm trying to make a macro with an 'input' pop-up dialog that synergizes with the replace feature. It should go something like: "Paste text->ask for input->replace variable in pasted text with input value". Does anyone know how to do this? Especially interested in finding a variable/input option.

    Current code:

    import * from npp
    my_prompt = Notepad()
    initial_string="""
    
      THIS IS A HUGE
      STRING
      WITH COMMENTS
      AND FUNCTIONS
      AND
        NESTING
        MISSPELLEDD WORDS
        VARIABLES
      ALL KINDS OF CRAZY STUFF"""
    
    
    print initial_string
    user_replacement_string=Notepad.prompt(my_prompt) //RuntimeError: This class cannot be instantiated from Python
    Editor.replace("varr", user_replacement_string) //untested
    

    IMPORTANT EDIT

    Also, I want a general form that generates scripts like this, similarly to how a macro generator generates macro. See the answer. I took out almost all of the coding. All you have to do is create a pattern string with variable names that you can remember embedded into it and save the resulting macro files in the right directory. Basically, it allows the user to macro pasting a value in and then easily replace multiple variables within the pasted content.

    • Wolfpack'08
      Wolfpack'08 over 12 years
      Please note, I have already posted a thread at the notepad++ forums at sourceforge: sourceforge.net/projects/notepad-plus/forums/forum/1290590/…‌​.
    • 100rabh
      100rabh over 12 years
      there's no way to ask for input - IIRC.
    • Wolfpack'08
      Wolfpack'08 over 12 years
      @Sathya There's a function in the PythonScript plugin, "Notepad.prompt()" which prompts the user for an input, but it requires something called "Notepad" to be instantiated. I don't know where to import it from, or if it is instantiated by some custom function. The documentation isn't much help to me, but it may be to someone else: npppythonscript.sourceforge.net/docs/latest/notepad.html.
    • 100rabh
      100rabh over 12 years
      Intriguing, I'll take a look once I'm home..
    • Wolfpack'08
      Wolfpack'08 over 12 years
      @Sathya Cool. I got the import working with some help. Now trying to solve runtime errors. Much closer.
    • 100rabh
      100rabh over 12 years
      if you do figure it out, do post it as an answer. I won't be home for another 3 hours, at least
    • Wolfpack'08
      Wolfpack'08 over 12 years
      @Sathya cool. I've got it, and it's very handy. So, I'll answer the question as soon as the site opens my permissions.
    • Wolfpack'08
      Wolfpack'08 over 12 years
      @sathya If you check my solution out, I put a general form in that I hope you will find handy. If you know anybody who uses Notepad++ for transcription work or who works in appraisals, you should hand it off.
  • u01jmg3
    u01jmg3 almost 8 years
    Causes N++ to crash in latest version (v6.9.2)
  • Wolfpack'08
    Wolfpack'08 over 7 years
    Sorry. I'm overseas, or I would have responded sooner. Can you give me an error code and screenshot? I'm going to be testing this, sir.