Pasting copied text in a search window in a macro
-
Hi all,
I’m trying to create a macro where I copy a index text in a windows and then I try to find the reference in another window, pasting the text in a search window.
However, during execution, it pastes exactly the same text copied during recording.
Is there any way in a macro to paste the previously copied text in a search window?
Thanks in advance and best regards.
-
@Raúl-ALR said in Pasting copied text in a search window in a macro:
… create a macro … pasting the text in a search window.
A macro cannot record that action, sorry.
The macro language has a limited set of actions it can take. For search-and-replace, it doesn’t record the action of pasting into the search dialog; instead, it records the value that you entered into the search dialog while you were recording.
If you were willing to use a plugin, there are many scripting plugins (like PythonScript) which can automate controlling just about every aspect of the Notepad++ GUI environment, including taking the value highlighted in one file and searching for that term in a different file.
If you say you are willing to use PythonScript, but you need help creating the script, then if give us an exact set of actions, rather than your brief overview, someone here might even be able to find the time to write an initial version for you.
-
@PeterJones Thank you for your answer.
I’ve just installed Python Script plugin.
I’ll explain what I want to do:
I’ve got 2 files.
File 1 has many references to index in many of its lines. This is an example of a line with an index reference:
<ObjectRef ref=“id4843X9273”/>
Fike 2 has a definition for each index in one of it lines. Like this:
<Host id=“id4843X9273” name=“object_name” comment=“” ro=“False”>What I want to do is a macro that search for the next ObjectRef reference in file1, copy the index and then moves to file2 in order to look for index definition. Then, it will copy the object name and moves to file 1 in order to replace index line by its name:
<ObjectRef ref=“id4843X9273”/> ----> object_nameAny help on how to face this with Python Script will be highly appreciated.
Best regards!
-
Hi again,
In order to simplify, I just need an script that will search (in the active window) for a previously copied text.
If I get this, I think will be able to execute the script during a standard macro recording session.
Best regards!
-
@Raúl-ALR said in Pasting copied text in a search window in a macro:
I think will be able to execute the script during a standard macro recording session.
You probably don’t want to attempt this.
Once you get scripting ability, you’ll see it is just way easier to do everything via the script. -
@Alan-Kilborn For sure I’ll will spend time learning how to script.
However time is sort for me for solving this issue… could you please help me in this newbie approach for solving it?
-
However time is sort for me for solving this issue
I am sorry that you are in a rush. This forum is just a Community of fellow Notepad++ users. Some of the regulars here pay attention throughout their day, and answer questions and sometimes hack up a script for a user during their free time… but I know that, at least for me, I am not paid anything to be in this forum, and the employer that is paying for my time would be unhappy if I dropped everything in my job to write a script for a random guy on the internet.
I can usually find time to work on such requests during the day (like on my “coffee breaks” and a few minutes here and then when things are compiling in the background or similar), but my morning was exceedingly busy today. I should have time in the next few hours to try to piece something together, but it won’t be instantaneous.
(And, generally speaking, telling free-help forums that “I’m in a rush” is a virtual guarantee that the helpers won’t find the time to help with that particular problem, because it sounds rather demanding of someone who is asking a non-full-time volunteer to do their work for them.)
-
@PeterJones Thanks for your reply and I’m sorry if my message sounded demanding.
Doing everything in a macro using Python Script demands a hard learning process and I what I wanted to show it that I think it could be a good first approach to insert small scripts during a standard macro recording.
Anyway, sorry again. I’ll continue struggling with scripting! :-)
Best regards
-
The instructions for installing and running this script are in the FAQ: How to install and run a script in PythonScript
The script-specific instructions are found in the script’s code itself.
# encoding=utf-8 """in response to https://community.notepad-plus-plus.org/topic/23339/pasting-copied-text-in-a-search-window-in-a-macro This searches in the "objects" file for every line: <ObjectRef ref="..."/> It grabs the ref ID, and searches for that ID in the "hosts" file <Host id="id4843X9273" name="object_name" comment="" ro="False"> It grabs the name field from the matching line in the "hosts" file It then replaces the whole <ObjectRef.../> with that name field In the lines below, change the paths to the two files: - The first path is the "objects" file, - The second is the "hosts" file. - Both must be opened in Notepad++, but it doesn't matter which view they are in. - Full paths are required. It makes it a single undo action, so if you don't like the results, you can just hit UNDO """ from Npp import editor,notepad,console class Class23339(object): def go(self, object_fname, host_fname): self.object_fname = object_fname self.host_fname = host_fname notepad.activateFile(object_fname) editor.rereplace(r'<ObjectRef ref="([^"]*)"/>', lambda m: self.repl_in_objects(m)) def repl_in_objects(self, m): notepad.activateFile(self.host_fname) editor.research(r'<Host id="{}" name="(.*?)"'.format(m.group(1)), lambda ms: self.find_in_hosts(ms)) notepad.activateFile(self.object_fname) return self.found_in_hosts def find_in_hosts(self, m): self.found_in_hosts = m.group(1) editor.beginUndoAction() Class23339().go( r"c:\users\peter.jones\downloads\tempdata\nppCommunity\objects.xml", r"c:\users\peter.jones\downloads\tempdata\nppCommunity\hosts.xml" ) editor.endUndoAction()
If you want to ever customize these, the
editor.rereplace(...)
line starts with the regex that will match lines in the “objects” file and put the ref-ID value in group(1); theeditor.research(...)
line starts with the regex that will find the specific ref-ID in the “hosts” file, and grab the value of the host name out of there. -