Hi, looking for a regex that calculates distance between 2 numbers, then adjust the 2nd number to a minimum of 30.
-
@Jeff-Michaels said in Hi, looking for a regex that calculates distance between 2 numbers, then adjust the 2nd number to a minimum of 30.:
This is the error I’m getting at the moment
Are you using a 2.x version of PythonScript?
If so, then it is probably caused by it not linking the f-string used in that line, as that is a Python3 feature.In Python2 you could write that line as:
return '{n1}/{n2}'.format(n1=n1, n2=n2)
-
(no success yet) I’m also trying to run the python code from Terminal on a Mac. Each time I run it, it comes up with a NameError. I placed the script in a folder on the desktop along with one of the text files I want to fix. As it cannot define the editor, the script will not run. Does anyone have any advice on how to get the script to run in Terminal with files in a folder on the desktop?
jeff@jeff ~ % python3 /Users/jeff/Desktop/test/kbpfix2.py
Traceback (most recent call last):
File “/Users/jeff/Desktop/test/kbpfix2.py”, line 14, in <module>
editor.rereplace(r’ (\d+)/(\d+)(?=/\d+[\r\n]±+$)', fix_distance)
NameError: name ‘editor’ is not defined -
UPDATE: I am so close, but I still cannot get this to work. I’m trying to stick with Terminal on macOS, but it produces an error at ‘editor.rereplace’. Is there an alternative to this part of the code to get it to work on a Mac? (I usually use grep in Bbedit as my application)
I was able to point to a folder on my desktop that will process all the files in it with .kbp extensions. The Python script fails as it doesn’t know what ‘editor.rereplace’ is.
Here is my new terminal command to process the files in a folder (python script is located inside this folder as well):
for f in *.kbp; do python3 kbpfix.py "$f" "${f%-fixed.kbp}" done
This is once again the original Python script that fails at the editor portion:
def on_match(m): n1s, n2s = m.groups() n1, n2 = int(n1s), int(n2s) n2 = max(n1 + 30, n2) return f'{n1}/{n2}' editor.rereplace(r'(?-i)(\d+)/(\d+)(?=/0\R\R-{29}\RPAGEV2)', on_match)
this is the error that appears in the macOS terminal after running:
editor.rereplace(r'(?-i)(\d+)/(\d+)(?=/0\R\R-{29}\RPAGEV2)', on_match) NameError: name 'editor' is not defined
Is there a suggestion as to what I can replace for the editor to get it to work on macOS in terminal?
-
I think you’re off-track.
This forum is about Notepad++ and its plugins.
We thought we were talking about PythonScript.
But apparently this has evolved into something that doesn’t relate to Notepad++, so we can’t continue to support you here.If you want to continue on with Python, suggest you find a Python forum to keep going with your questions.
-
The script I wrote above can’t be run except through PythonScript in Notepad++. It does not work like a normal Python script. If you want to start a chat with me, I can help you write a normal, non-PythonScript-based Python script to solve your problem in a way that’s unrelated to Notepad++.
-
@Mark-Olson Hi again Mark. I was just successful at getting the script to run in Notepad++ and the PythonScript plugin (yeah). I had a folder of 77 files and I needed to open each file separately and run the script on each and save. It corrected my files perfectly. I have hundreds more to process though. Is there anyway to run the script on the entire folder at once?
-
@Jeff-Michaels
Glad you got it to work! Unfortunately, PythonScript isn’t very well-equipped to manipulate the file system outside of Notepad++. You could in principle write a solution that opened every file in the directory in Notepad++, applied my script above, closed the file, and moved on to the next. However, that would be very slow compared to just writing a normal Python script that doesn’t use Notepad++ at all.Since my preferred solution to your larger problem doesn’t use Notepad++, I’ll turn to the chat.
-
@Mark-Olson Do you have a preferred way to chat or email? my email is jeff@sybersound.com
-
Just want to close by making a general note for posterity.
Out of curiosity, I tried using ChatGPT with Jeff’s problem, and it provided a solution that was suboptimal but serviceable nonetheless. And of course it was much much faster than me.
So I guess what I’d say is this:
If you’re looking for guidance on how to solve a problem that requires Notepad++, you’ve come to the right place. Not only that, I would very strenuously advise against trusting anything that ChatGPT says about Notepad++. However, if you don’t have any particular reason to believe that Notepad++ needs to be in the loop, ChatGPT might potentially be useful, as it excels at coming up with decent solutions to simple, routine problems, so long as you check its work. -
To keep the thread on-topic, I see that the OP has things working within Notepad++ and apparently has fixed some of his files using Notepad++ with Notepad++'s flavor of PythonScript.
The OP then asked if there is a way to do the script on all of the files in a folder. I believe it’s possible but don’t know the best practice for 1) How can someone open dozens, hundreds, or thousands of files in Notepad++? 2) How can someone then run a particular PythonScript from within Notepad++ on all open tabs?
To address #1 something I do is to construct a Notepad++ session file that has all of the files that I want to work on and then run Notepad++.exe with
-openSession filename
. While that works for me I don’t know if that’s the best practice. It may well be the OP is satisfied with runningNotepad.exe *.txt
from the command line and letting Notepad++'s build in wild card expansion take over. I just tried it on a folder with 963 files and … a minute later had 963 new tabs open. I was surprised that “close all tabs to the right” took about 45 seconds but it worked.I don’t know how to address #2 - Is it possible to run a PythonScript on all open tabs?
-
@mkupper said in Hi, looking for a regex that calculates distance between 2 numbers, then adjust the 2nd number to a minimum of 30.:
Is it possible to run a PythonScript on all open tabs?
No. What you would do is, in a single run of a single script, iterate over the open file tabs:
for (filename, bufferID, index, view) in notepad.getFiles(): notepad.activateFile(filename) ....
After a file is “activated”, then the
editor
object can manipulate the data of the activated tab, e.g., perhaps do aneditor.replace()
, etc. -
This post is deleted! -
This post is deleted!