Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
    
Please note, I have already posted a thread at the notepad++ forums at sourceforge: sourceforge.net/projects/notepad-plus/forums/forum/1290590/…;. –  Wolfpack'08 Jan 9 '12 at 7:51
    
there's no way to ask for input - IIRC. –  Sathya Jan 9 '12 at 9:12
    
@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. –  Wolfpack'08 Jan 9 '12 at 9:30
    
Intriguing, I'll take a look once I'm home.. –  Sathya Jan 9 '12 at 9:42
1  
if you do figure it out, do post it as an answer. I won't be home for another 3 hours, at least –  Sathya Jan 9 '12 at 9:52

2 Answers 2

up vote 6 down vote accepted

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: ("foo", "bar").

I didn't put a 'default value' function in because I thought it would be annoying. 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 request for one, I will add it.

share|improve this answer

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|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.