• Login
Community
  • Login

Script for automatic insertion of modified rows

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
8 Posts 3 Posters 424 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.
  • M
    Martin Steiner
    last edited by Feb 19, 2023, 8:06 AM

    I have an xml document that I need to edit. Using bookmarks I can extract certain lines marked <Text> and </Text>.
    I’ll edit those lines in the adjacent document, but then I’d need to put them back in the same place, but somehow in bulk so I don’t have to do it manually line by line.Couldn’t this be solved with some script?

    Original document:
    0001.png

    Edited text that needs to be returned:
    0002.png

    Thank you for any help with my problem…

    E 1 Reply Last reply Feb 19, 2023, 10:20 AM Reply Quote 0
    • E
      Ekopalypse @Martin Steiner
      last edited by Ekopalypse Feb 19, 2023, 10:21 AM Feb 19, 2023, 10:20 AM

      @Martin-Steiner

      Theoretically yes, like this

      • bookmark the lines that need to be edited ->the bookmarks should contain only one line
      • Copy the bookmarked lines to another document and move them to the other view of Npp
      • Edit the newly created data as you wish, but NO NEW lines should be inserted.
      • Then run a script that replaces each line in the edited view with the corresponding line in the other view.

      If this is NOT how you want it to work, then you need to explain your exact procedure so it can be understood if this is easily possible.

      By the way, the PythonScript plugin would have to be installed if I were to provide a scripting solution.

      M 1 Reply Last reply Feb 19, 2023, 10:34 AM Reply Quote 0
      • M
        Martin Steiner @Ekopalypse
        last edited by Feb 19, 2023, 10:34 AM

        @Ekopalypse
        Yes, this looks like just what I need. I’ll bookmark the rows I need to edit. I copy these rows from the new NPP view, there they get edited (won’t change their number), I need to get these edited rows back into the original file. I have the PythonScript plugin installed.
        Can you provide me a python script that can do this?

        E 2 Replies Last reply Feb 19, 2023, 10:36 AM Reply Quote 0
        • E
          Ekopalypse @Martin Steiner
          last edited by Feb 19, 2023, 10:36 AM

          @Martin-Steiner

          OK, I’ll put something together, but since I have to prepare lunch now, I’ll post something in 1-2 hours.

          M 1 Reply Last reply Feb 19, 2023, 11:26 AM Reply Quote 0
          • M
            Martin Steiner @Ekopalypse
            last edited by Feb 19, 2023, 11:26 AM

            @Ekopalypse

            If you need to test something, I send a sample file.

            Sample file here

            1 Reply Last reply Reply Quote 0
            • E
              Ekopalypse @Martin Steiner
              last edited by Feb 19, 2023, 12:06 PM

              @Martin-Steiner

              As mentioned earlier, this script assumes that the number of bookmarked lines is identical to the number of lines edited in the other view.

              For example, int this case when using copy bookmarked lines

              2a3ffc38-97e8-4d3e-8c45-e0c1d4908f8b-image.png

              you need to get rid of the 5 line in the second view.

              60ff404f-fa2b-43c5-9d87-59b7c306f882-image.png

              from Npp import notepad, editor1, editor2
              
              class BookmarkLineReplacer:
                  def __init__(self):
                      self.BOOKMARK_MASK = 1 << 20  # 20 = Npps' bookmark id
                      # assuming these are the defaults
                      self.ED1 = editor1
                      self.ED2 = editor2
              
                  def get_bookmarked_lines(self):
                      self.bookmarked_lines = []
                      line_number = self.ED1.markerNext(0, self.BOOKMARK_MASK)
                      while line_number != -1:
                          self.bookmarked_lines.append(line_number)
                          line_number = self.ED1.markerNext(line_number + 1, self.BOOKMARK_MASK)
              
                  def replace_lines(self):
                      # sanity check
                      replacement_lines_count = self.ED2.getLineCount()
                      if len(self.bookmarked_lines) != replacement_lines_count:
                          msg = 'bookmarked lines: {} BUT the number of replacements is: {}'.format(len(self.bookmarked_lines), replacement_lines_count)
                          notepad.messageBox(msg, "Number of lines do not match, aborting ...", 0)
                          return
              
                      replacement_lines = self.ED2.getText().splitlines()
                      for i, line_number in enumerate(self.bookmarked_lines):
                          self.ED1.replaceLine(line_number, replacement_lines[i])
              
                  def main(self):
                      if notepad.isSingleView():
                          notepad.messageBox('This script NEEDS both views', "Aborting...", 0)
                          return
              
                      # check which view (0=editor1 and 1=editor2) has the bookmarks
                      if editor1.markerNext(0, self.BOOKMARK_MASK) == -1:
                          if editor2.markerNext(0, self.BOOKMARK_MASK) == -1:
                              notepad.messageBox('No bookmarks found', "Aborting...", 0)
                              return
                          else:
                              self.ED1 = editor2
                              self.ED2 = editor1
              
                      self.get_bookmarked_lines()
                      self.replace_lines()
              
              
              BookmarkLineReplacer().main()
              
              M 1 Reply Last reply Feb 19, 2023, 12:48 PM Reply Quote 2
              • M
                Martin Steiner @Ekopalypse
                last edited by Feb 19, 2023, 12:48 PM

                @Ekopalypse

                Tested. Yes, it works exactly as I needed. Thank you for your help. Nice work.

                1 Reply Last reply Reply Quote 0
                • G
                  guy038
                  last edited by guy038 Feb 20, 2023, 8:54 AM Feb 19, 2023, 10:44 PM

                  Hello, @martin-steiner, @ekopalypse and All,

                  Of course, the @ekopalypse’s python script seems the best bet ! However, you can just achieve it with the native regex engine of N++ !

                  I was able to download your Test_File.zip archive and extract the Cooper_CH1_Call.xml file

                  Just execute each point of the method below :


                  • Open your Cooper_CH1_Call.xml file in N++

                  • Open the Mark dialog ( Ctrl + M )

                  • Uncheck all the box options

                  • Check the Wrap around option

                  • SEARCH (?x-si) (?<= <Text> ) .+ (?= </Text> )

                  • Select the Regular expression search mode

                  • Click on the Mark All button

                  • Click on the Copy Marked Text button

                  • Move to the very end of the file

                  • Add an empty new line

                  • Paste the bookmarked lines with Ctrl + V

                  => Near the end of the XMl file, you should get this text :

                    </DialogCues>
                    <Name>Cooper_CH1_Call</Name>
                  </DialogCueSheetData>
                  
                  Ahoj. Tady je opět Cooper.
                  You cannot open that door \nwith your current version of \nthe OMNI View software.
                  You will need to update your \nsoftware at a Daemon server.
                  Upgrade to version 1.0 and \nyou can open this door.
                  Then, you can get the girl to the Library.
                  Nejbližší Daemon se nachází v jídelně,
                  na druhé straně kolejí.
                  I will add this waypoint to your OMNI View.
                  I will also highlight this location on your map.
                  I will add this waypoint to your map.
                  ...
                  ...
                  ...
                  Zobrazíš ji výběrem ikony mapy v telefonu. Buď opatrný.
                  The girl must feel a strong sense of deja vu, but fortunately you have been down this road before, too.
                  Each confinement room contains a power unit in case of power failure.
                  Find the unit to open the door.
                  It is in a different location inside every room, so check every corner in OMNI-View.
                  Tohle je pepřový sprej značky P-Zero.
                  Equip it with the phone, and select a guard for her to attack.
                  Knowing the girl, she'll defend herself even without the spray equipped.
                  But be careful, each canister has a single shot.
                  And guards will eventually return to patrol angrier than ever.
                  

                  Note that I assume that your initial XML file does not contain any empty line !


                  Now, simply add the desired translation of every line, after some space characters. For instance :

                    </DialogCues>
                    <Name>Cooper_CH1_Call</Name>
                  </DialogCueSheetData>
                  
                  Ahoj. Tady je opět Cooper.                                                                                       Line 001
                  You cannot open that door \nwith your current version of \nthe OMNI View software.                               Line 002
                  You will need to update your \nsoftware at a Daemon server.                                                      Line 003
                  Upgrade to version 1.0 and \nyou can open this door.                                                             Line 004
                  Then, you can get the girl to the Library.                                                                       Line 005
                  Nejbližší Daemon se nachází v jídelně,                                                                           Line 006
                  na druhé straně kolejí.                                                                                          Line 007
                  I will add this waypoint to your OMNI View.                                                                      Line 008
                  I will also highlight this location on your map.                                                                 Line 009
                  I will add this waypoint to your map.                                                                            Line 010
                  ...
                  ...
                  ...
                  Zobrazíš ji výběrem ikony mapy v telefonu. Buď opatrný.                                                          Line 244
                  The girl must feel a strong sense of deja vu, but fortunately you have been down this road before, too.          Line 245
                  Each confinement room contains a power unit in case of power failure.                                            Line 246
                  Find the unit to open the door.                                                                                  Line 247
                  It is in a different location inside every room, so check every corner in OMNI-View.                             Line 248
                  Tohle je pepřový sprej značky P-Zero.                                                                            Line 249
                  Equip it with the phone, and select a guard for her to attack.                                                   Line 250
                  Knowing the girl, she'll defend herself even without the spray equipped.                                         Line 251
                  But be careful, each canister has a single shot.                                                                 Line 252
                  And guards will eventually return to patrol angrier than ever.                                                   Line 253
                  

                  Then :

                  • Open the Replace dialog Ctrl + H

                  • Uncheck all the box options

                  • Check the Wrap around option

                  • SEARCH (?x-si) (?<= <Text> ) ( .+ ) (?= </Text> (?s: .+ ) ^ \R (?-s: .+ \R )* \1 \x20{2,} ( .+ ) ) | (?s) ^ \R .+

                  • REPLACE ?2\2

                  • Select the Regular expression search mode

                  • Click on the Replace All button

                  Here you are ! All the lines <Text>..........</Text> should now contain their corresponding translation Line ### !

                  Best Regards

                  guy038

                  P.S. :

                  In case, your XML file contains empty lines, change, for instance, the uniq empty separation line with a line of @ characters and use the regex replacement :

                  • SEARCH (?x-si) (?<= <Text> ) ( .+ ) (?= </Text> (?s: .+ ) ^ @+ \R (?-s: .+ \R )* \1 \x20{2,} ( .+ ) ) | (?s) ^ @+ \R .+

                  • REPLACE ?2\2

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