• Login
Community
  • Login

How to copy strings?

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
8 Posts 3 Posters 976 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.
  • N
    NiceGlassesDude
    last edited by Apr 3, 2020, 6:25 PM

    I have a bunch of scripts containing also text within strings like this

    clark
       "He mentioned that already a few weeks ago"
    

    What I want is a simple method to copy those text lines preferable with just a simple click rather than manually moving the mouse which gets bothersome, especially if you’re tired. In notepad++ whenever I double click those lines it marks the the whole code line unfortunately but I just want the text within those strings “”

    A 1 Reply Last reply Apr 3, 2020, 6:54 PM Reply Quote 0
    • A
      Alan Kilborn @NiceGlassesDude
      last edited by Apr 3, 2020, 6:54 PM

      @NiceGlassesDude said in How to copy strings?:

      I want … a simple method

      Hmm, well simple is in the eye of the beholder.

      I don’t think you’re going to get any solution that is native to Notepad++.

      A Pythonscript could do it, if you’re willing to install and use that plugin.

      1 Reply Last reply Reply Quote 0
      • A
        Alan Kilborn
        last edited by Apr 3, 2020, 7:12 PM

        For example, this Pythonscript can do it, if you put your caret somewhere inside a double-quoted region and then run it:

        
        # -*- coding: utf-8 -*-
        
        from Npp import editor, notepad
        
        prematches = []
        postmatches = []
        
        cp = editor.getCurrentPos()
        
        editor.search(r'"', lambda m: prematches.append(m.span(0)[1]), 0, 0, cp)
        editor.search(r'"', lambda m: postmatches.append(m.span(0)[0]), 0, cp, editor.getTextLength(), 1)
        
        if len(prematches) != 0 and len(postmatches) != 0: editor.copyText(editor.getTextRange(prematches[-1], postmatches[0]))
        
        
        N A 2 Replies Last reply Apr 3, 2020, 7:56 PM Reply Quote 5
        • N
          NiceGlassesDude @Alan Kilborn
          last edited by NiceGlassesDude Apr 3, 2020, 7:57 PM Apr 3, 2020, 7:56 PM

          @Alan-Kilborn

          Thank you very much. I never used Python, though. Which plugin do I need in notepad++ for this script?.

          A 1 Reply Last reply Apr 3, 2020, 8:13 PM Reply Quote 0
          • A
            Alan Kilborn @NiceGlassesDude
            last edited by Apr 3, 2020, 8:13 PM

            @NiceGlassesDude

            Gosh. I thought I was fairly explicit about it, but here you go:

            9cd0404a-6a87-43a2-8c36-a5fe0f5906f8-image.png

            1 Reply Last reply Reply Quote 3
            • A
              astrosofista @Alan Kilborn
              last edited by Apr 4, 2020, 1:33 PM

              Hi @Alan-Kilborn, @NiceGlassesDude, All

              As an alternative method, OP’s requirement can also be accomplished with a combination of some internal features of Notepad++, as follows:

              Assuming the caret is inside a doble-quoted string

              1. Run a backward search in normal mode to find the opening quote.
              2. Move one character to the right to put the caret at the beginning of the wanted string.
              3. Run a search in regex mode to match all characters up to the closing quote.
              4. Copy the matched characters.

              The nice thing is that this method is macro recordable:

                  </Macro>
                  <Macro name="Copy quoted strings" Ctrl="no" Alt="no" Shift="no" Key="0">
                      <Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
                      <Action type="3" message="1601" wParam="0" lParam="0" sParam='&quot;' />
                      <Action type="3" message="1625" wParam="0" lParam="0" sParam="" />
                      <Action type="3" message="1702" wParam="0" lParam="256" sParam="" />
                      <Action type="3" message="1701" wParam="0" lParam="1" sParam="" />
                      <Action type="0" message="2306" wParam="0" lParam="0" sParam="" />
                      <Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
                      <Action type="3" message="1601" wParam="0" lParam="0" sParam='[^&quot;]+(?=&quot;)' />
                      <Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
                      <Action type="3" message="1702" wParam="0" lParam="768" sParam="" />
                      <Action type="3" message="1701" wParam="0" lParam="1" sParam="" />
                      <Action type="0" message="2178" wParam="0" lParam="0" sParam="" />
                      <Action type="2" message="0" wParam="42002" lParam="0" sParam="" />
                      <Action type="0" message="2179" wParam="0" lParam="0" sParam="" />
                  </Macro>
              

              Now, you can record a macro following the steps stated above or copy the provided one. For the later approach, please do as follows:

              Open your active shortcuts.xml configuration file —as always, back-up it first—, with another editor like MS Notepad. Copy the macro and paste it in the macro section, save changes, close the file and restart Notepad++. Then you can assign a shortcut to the macro via the Configuration menu.

              As you surely noted, this method is nothing new or original, is quite similar to the Python script posted before, but could look simpler to non programmers users. Anyway, I agree with @Alan-Kilborn in that simplicity is in the eye of the beholder.

              Have fun!

              A 1 Reply Last reply Apr 4, 2020, 2:53 PM Reply Quote 2
              • A
                Alan Kilborn @astrosofista
                last edited by Apr 4, 2020, 2:53 PM

                @astrosofista

                Nice one.

                I had said:

                I don’t think you’re going to get any solution that is native to Notepad++.

                Sometimes it is good to be wrong.

                simplicity is in the eye of the beholder.

                True, but here’s the backstory: I pulled the posted script from something I already had that selects text either between pairs of single quotes or double quotes (and a few other paired-delimiters). Since the OP had a limited need (double quotes), and realizing that most readers that could find it useful have a better chance of understanding a simpler script, I hacked it down.

                I suppose my posted script has “extensibility” going for it, as opposed to the macro solution, that to me seems difficult to extend to even single quotes.

                A 1 Reply Last reply Apr 4, 2020, 10:59 PM Reply Quote 3
                • A
                  astrosofista @Alan Kilborn
                  last edited by Apr 4, 2020, 10:59 PM

                  @Alan-Kilborn said in How to copy strings?:

                  I suppose my posted script has “extensibility” going for it, as opposed to the macro solution, that to me seems difficult to extend to even single quotes.

                  Yes, though my regex solves OP’s problem, lack of extensibility is one of its drawbacks - Regarding this issue, for the moment I run out of ideas, but don’t give up.

                  Also, compared with your PS, my regex has another drawback, as it couldn’t preserve the original position of the caret. What a shame! ha ha

                  Have fun!

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