PythonScript: Script that runs on multiple files not working
-
Rather than replacing ‘value’ according to the ‘mission_tag’, ‘decision_tag’ or ‘religion_group’ found in the document, it instead replaces ‘value’ according to said variables found in the first open document from which the script is first ran from.
The script works on single files. All that’s been added to make it multi-file is:
for (filename, bufferID, index, view) in notepad.getFiles():I am a beginner to python so sorry if I’ve made a very basic mistake.
# encoding=utf-8 from Npp import * import re import os mission_tag = re.search(r'\s*potential = \{[\s\S]*(?<=tag = )(\w+)', editor.getText()) decision_tag = re.search(r'(?<=NOT = { exists = )(\w+)', editor.getText()) religion_group = re.search(r'\s*potential = \{[\s\S]*(?<=religion_group = )(\w+)', editor.getText()) history_files = 'C:\Program Files (x86)\Steam\steamapps\common\Europa Universalis IV\history\countries' base_tech = 3 pattern = re.compile(r'(?<=adm_tech = )(\d+)|(?<=dip_tech = )(\d+)|(?<=mil_tech = )(\d+)') value = pattern.search(editor.getText()) def replace(percentage): amount = float((100 - percentage) * 0.01) editor.rereplace(r'(?<=adm_tech = )(\d+)|(?<=dip_tech = )(\d+)|(?<=mil_tech = )(\d+)', str(int(round(base_tech * ((((float(value.group(0)) / base_tech) - 1) * amount) + 1))))); def adjust(): notepad.open(history_files+'\\'+filename) colonial = re.search(r'(?<=.)(is_former_colonial_nation = yes)|(?<=.)(is_colonial_nation = yes)', editor.getText()) tech_group = re.search(r'(?<=technology_group = )(\w+)', editor.getText()) notepad.close() if tech_group.group(0) == 'western' or colonial: notepad.close() elif tech_group.group(0) == 'eastern': replace(20) elif tech_group.group(0) == 'ottoman': replace(25) elif tech_group.group(0) == 'muslim': replace(37.5) elif tech_group.group(0) == 'indian': replace(40) elif tech_group.group(0) == 'east_african': replace(45) elif tech_group.group(0) == 'central_african': replace(55) elif tech_group.group(0) == 'chinese': replace(50) elif tech_group.group(0) == 'nomad_group': replace(62.5) elif tech_group.group(0) == 'sub_saharan': replace(50) elif tech_group.group(0) in ('mesoamerican', 'andean'): replace(75) elif tech_group.group(0) in ('north_american', 'south_american', 'aboriginal_tech', 'polynesian_tech'): replace(92.5) for (filename, bufferID, index, view) in notepad.getFiles(): notepad.activateBufferID(bufferID) for filename in os.listdir(history_files): if mission_tag and re.match(mission_tag.group(1), filename): adjust() elif decision_tag and re.match(decision_tag.group(0), filename): adjust() elif religion_group: if religion_group.group(1) == 'christian': notepad.close() elif religion_group.group(1) == 'muslim': replace(37.5) elif religion_group.group(1) == 'dharmic': replace(40) elif religion_group.group(1) == 'eastern': replace(50) elif religion_group.group(1) == 'pagan': replace(75) -
As the
mission_tag =… (and others) definition currently involves data only from the “open doc from which the script is first ran from”, I’d say you need to have those statements inside theforloop you added, after thenotepad.activateBufferIDline. -
@Alan-Kilborn Thanks, that worked. I had tried that but forgot to put it after the
notepad.activateBufferIDline.
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login