• Login
Community
  • Login

how to run my python script using (pythonscript for npp) to multiple opened files

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
5 Posts 3 Posters 897 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.
  • R
    Redeye123
    last edited by Apr 2, 2022, 1:10 AM

    As of now i got this working in a single file

    def times_2(m):
        return 'unit_reward_per_km: ' + str(int(m.group(1)) * 2)
    
    editor.rereplace('unit_reward_per_km: ([0-9]+)', times_2);
    notepad.save()
    

    But i need to change 200+ files and after 10 i was allready done with doing it manual . after some searching i found this but i dont know how to implement the above to the new script (I kept getting times_2 needs exact 1 argument but none given ) . and only the last file gets changed

    from Npp import *
    
    def forEachFile(func):
    	for file in notepad.getFiles():
    		notepad.activateBufferID(file[1])
    		func()
            
    def times_2(m):
        return 'unit_reward_per_km: ' + str(int(m.group(1)) * 2)
    
    def printFilename():
    	print notepad.getCurrentFilename()
    
    editor.rereplace('unit_reward_per_km: ([0-9]+)', times_2);
    notepad.save()
    
    # store the current buffer id
    currentBufferID = notepad.getCurrentBufferID()
    # run our printFilename function on each open file
    forEachFile(printFilename)
    # restore the current buffer from the stored buffer id.
    notepad.activateBufferID(currentBufferID)
    

    Anyone able to help me out ?

    M 1 Reply Last reply Apr 2, 2022, 8:08 AM Reply Quote 1
    • M
      mpheath @Redeye123
      last edited by Apr 2, 2022, 8:08 AM

      @redeye123

      Looks like half done with print filename… and multiple and single file code tested.

      from Npp import *
      
      def forEachFile():
      	for file in notepad.getFiles():
      		notepad.activateBufferID(file[1])
      		notepad.getCurrentFilename()
      		editor.rereplace('unit_reward_per_km: ([0-9]+)', times_2)
      		notepad.save()
      
      def times_2(m):
      	return 'unit_reward_per_km: ' + str(int(m.group(1)) * 2)
      
      forEachFile()
      

      You had most of the code already done. Just needed are little rearrangement to insert it into the loop.

      M R 2 Replies Last reply Apr 2, 2022, 8:28 AM Reply Quote 2
      • M
        mpheath @mpheath
        last edited by Apr 2, 2022, 8:28 AM

        In code I posted, notepad.getCurrentFilename() will print nothing unless preceded with the print statement or use the console object console.write(notepad.getCurrentFilename()) method. That will output some progress to the console.

        A 1 Reply Last reply Apr 2, 2022, 10:38 AM Reply Quote 2
        • A
          Alan Kilborn @mpheath
          last edited by Alan Kilborn Apr 2, 2022, 10:39 AM Apr 2, 2022, 10:38 AM

          @mpheath

          I would do your for loop this way instead:

          for (filename, bufferID, index, view) in notepad.getFiles():
                  notepad.activateBufferID(bufferID)
                  print(notepad.getCurrentFilename()
                  # ...
          
          1 Reply Last reply Reply Quote 3
          • R
            Redeye123 @mpheath
            last edited by Apr 2, 2022, 9:27 PM

            @mpheath said in how to run my python script using (pythonscript for npp) to multiple opened files:

            from Npp import *

            def forEachFile():
            for file in notepad.getFiles():
            notepad.activateBufferID(file[1])
            notepad.getCurrentFilename()
            editor.rereplace(‘unit_reward_per_km: ([0-9]+)’, times_2)
            notepad.save()

            def times_2(m):
            return 'unit_reward_per_km: ’ + str(int(m.group(1)) * 2)

            forEachFile()

            Yea i copied alot to help myself out and it worked in one so i had to be close the printname was just something that was in a piece of code :P and forgot to remove it when i posted it here . Thank you !

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