how to run my python script using (pythonscript for npp) to multiple opened files
-
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 ?
-
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.
-
In code I posted,
notepad.getCurrentFilename()
will print nothing unless preceded with theprint
statement or use the console objectconsole.write(notepad.getCurrentFilename())
method. That will output some progress to the console. -
I would do your
for
loop this way instead:for (filename, bufferID, index, view) in notepad.getFiles(): notepad.activateBufferID(bufferID) print(notepad.getCurrentFilename() # ...
-
@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 !