Prior to now I've only written Leo scripts that operate on things 'out there' somewhere. Here's the first Leo script I've written that modifies content in Leo. No doubt there's things to be done better/differently, important bits are hardcoded, but it actually works and does what I was looking for so I'm happy for today. ;-)
-matt *Put this in an empty node and run with Ctrl-B or [script-button].* @language python ''' Search folder tree for the specified pattern Windows only at present. matt wilkie <[email protected]> Nov 2019 ''' import os import shlex import subprocess # what to search for pattern="dist.leo" #Todo: make this a parameter or prompt! g.es_print('='*40) g.es_print('Running search-dirs-for-string') searchdir=os.path.join(g.app.loadDir, '../../*') command = f'findstr -i -s -p "{pattern}" {searchdir}' # windows specific cmd #g.es_print(shlex.split(command)) # debug p = subprocess.run(shlex.split(command), shell=True, stdout=subprocess.PIPE, stderr=None) out = p.stdout #err = p.stderr() lines = [g.toUnicode(z) for z in g.splitLines(out or [])] lines = [os.path.relpath(z, searchdir[:-2]) for z in lines] # Remove Leo app dir prefix, less noise p2 = c.p.insertAfter() p2.h = f"Find results: {pattern}" p2.b = f"""--- Matches for {pattern} under: --- {searchdir} {''.join(lines)} """ c.selectPosition(p2) c.redraw() g.es_print('Results in next node') '''Sources: Run shell command and capture results: LeoPyRef.leo#Found:shlex.split(command)-->g.execGitCommand Put results into new node and redraw (EKR): https://groups.google.com/d/topic/leo-editor/s16fP2pcxqM/discussion Removing a path prefix (Mitch): https://stackoverflow.com/questions/8693024/how-to-remove-a-path-prefix-in-python ''' -- You received this message because you are subscribed to the Google Groups "leo-editor" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/leo-editor/469157fa-e27e-4fe7-a783-0d74bb38926f%40googlegroups.com.
