@Fuel-DZN said in PythonScript: Script not working:
How is it matching the contents of the file if it just closed it?
Either a bug in your logic, or a race condition. If it’s a bug in your logic, it’s not really on-topic for a Notepad++
But if it’s a race condition, it’s got to do with the way that Plugins ask Notepad++ to perform actions: In case you didn’t know, Notepad++ actions take time (dozens or hundreds of milliseconds are not uncommon action times), and the Plugin Communication messages that the notepad and editor objects wrap around often don’t wait until the action is complete before returning control to the plugin (which in this case is the PythonScript plugin, which then continues executing your script).
So your script tells Notepad++ to close the file, the application says “okay, I will”, and then proceeds to take a significant fraction of a second to close the file. In the meantime, your script gets the “okay, I will” message and then says “good, I’ll continue running the next commands”.
If you were going to continue to do something more after the file were closed, I would say that you should probably put in a 500ms - 1000ms wait after doing the close, just to make sure that Notepad++ has time to finish it.
If you are not planning on doing anything after, then I would suggest adding in logic to exit the script (warning: exit() doesn’t do what you think it does in PythonScript [when it kills the Python interpreter, it also closes Notepad++], so you’ll want flags and if-statements to just quickly exit out of your script, or throw an exception, which PythonScript handles more gracefully (without exiting Notepad++).
How can it have a match and also no match?
Either because of the race condition (above) or because of another logic bug. If it’s the race condition, then it’s explained above. If it’s a logic bug on your part, that’s just part of programming, and not specific to Notepad++ or the PythonScript interface with Notepad++, so is not on topic here.