Find text in console
Hello, do you know a way (I searched in the forum but not found answer) to find text (also with regex) in console output field, select it, copy it in clipboard after the execution of a script or a command?
Simple example: I write in the console command line
print("hello dear")and press run: Pythonista executes the command and prints
hello dearin the console output window. Well, I'd like, when I'm in console environment and as post-processing, to find only word
dear(with direct searching of word
dearor with regex, with pattern that selects all after word
hello), select and copy it, or simply select it with a colored highlighting (in the same way user can do it in editor environment of Pythonista, where Pythonista highlights the searched text).
So, can Pythonista allow user to find text in console output? If not, do you know some existing obj-c script written by someone to create a wrench button to search and highlight text in console (maybe with pop-up in order to insert the word to be searched)?
@cvp Hi, yes remember, I use your neat code to send to server some short commands that I write in console to obtain answer to be printed in console output, but I think it cant help me.
I explain better: I don't need to copy in clipboard the text that I write in console input line, but I need to search/find/highligth some text in console output window (lets call it: the console history). For example, suppose I have a lot of text in console output, because I executed a lot of scripts that print a lot of things in console output: how can I find some specific text by using the built-in function Find (like in editor environment)?
@Matteo I've understood your request. It was only to remember that if we can access the console content, we should be able to scan it to find your words, but not sure we could change the attributes (color) because the console is not a real TextView but an OMTextView which does not have the needed methods to change attributes.
Edit: this find is easy but to show where they are is not
@cvp Ok, thank you for your reply, so I understand that console has not a
Findfield like the editor and by using your code user can keep track of what has been written in console, but the problem could be how to keep track of what has been written directly from script execution (so from editor, not from console).
However do you know some obj-c method to interact with Pythonista console (in this specific case with history of the console, not with input editable line of console environment)?
@Matteo before iOS 13 and/or beta version, my little GetConsoleText script did get the console content, thus the historic.But, if I run it now, this is empty, thus my script does not work anymore. I (would) have to understand why 😢
@Matteo I think I'm wrong, very sorry. I never got full console history, at least it seems.
I'll search for you, but not free immediately
Hi @cvp , thank you so much for your time but don't worry, don't search for me, only if it is also your interest, no problem, really :-).
My interest is if a code already exists that can search text in console, when there is a lot text in it and user doesn't want to search some specific words by scolling down the screen.
I will try to search in official Apple doc about some obj-c for interaction with OMTextView. Maybe I will find something here (found now).
Thank you for your always lively kindness in answering in this forum!
@Matteo I think that OM... objects are from OMz, thus not in Apple doc
Matteo last edited by Matteo
@cvp Hi, do you mean OMxxxxx is something related to omz library, not Apple?
However, if it is programmatically impossible to interact with console output via objc, do you know something for
pythonista_startup.pythat can automatically save in a file (named for example
console_output.txt) the output of any command executed in console or any script executed from editor environment, in a consecutive mode, in the same way the output in console appears? This file should contain the exact text user sees in console output/history, and this file should update itself after each execution where there is at least one print statement (that is when the console output changes its content by adding new text output).
Do you know if something already exists?
@Matteo Please could you try this code as a Pythonista tool.
First, you run the tool,
then you run a script with a console output,
then, in console mode, you type the text fo search and tap the 🔍 icon, and you will watch the miracle 😀
Sure that the code is not bug free, but it is good to start, if interested
The OMTextView does not allow to set text attributes as an UITextView but you can draw on it
from objc_util import * import clipboard import ui @on_main_thread def test(sender): import console import re import ui txt = str(sender.console.text()) if txt[-1] == '\n': txt = txt[:-1] win = ObjCClass('UIApplication').sharedApplication().keyWindow() main_view = win.rootViewController().view() ret = '' def analyze(v): for tv in v.subviews(): if 'OMTextView' in str(tv._get_objc_classname()): su = tv.superview() if 'OMTextEditorView' in str(su._get_objc_classname()): continue for sv in tv.subviews(): if 'SUIButton_PY3' in str(sv._get_objc_classname()): sv.removeFromSuperview() if txt == '': return t = str(tv.text()) #print('search',txt,'in',t) for m in re.finditer(txt, t): st,end=m.span() p1 = tv.positionFromPosition_offset_(tv.beginningOfDocument(), st) p2 = tv.positionFromPosition_offset_(tv.beginningOfDocument(), st+len(txt)) rge = tv.textRangeFromPosition_toPosition_(p1,p2) rect = tv.firstRectForRange_(rge) # CGRect x,y = rect.origin.x,rect.origin.y w,h = rect.size.width,rect.size.height #print(x,y,w,h) l = ui.Button() l.frame = (x,y,w,h) l.background_color = (1,0,0,0.2) l.corner_radius = 4 l.border_width = 1 tv.addSubview_(l) ret = analyze(tv) if ret: return ret ret = analyze(main_view) @on_main_thread def FindTextInConsole(): global console_tv win = ObjCClass('UIApplication').sharedApplication().keyWindow() main_view = win.rootViewController().view() ret = '' next_is_console = False def analyze(v,indent): global next_is_console ret = None for sv in v.subviews(): #print(indent,sv._get_objc_classname()) if 'UILabel' in str(sv._get_objc_classname()): #print(indent,sv.text()) if str(sv.text()) == '>': next_is_console = sv else: next_is_console = False elif 'OMTextView' in str(sv._get_objc_classname()): if next_is_console: su = next_is_console.superview() for ssv in su.subviews(): if 'SUIButton_PY3'in str(ssv._get_objc_classname()): # rerun of this script, remove previous button ssv.removeFromSuperview() b = ui.Button(name='clipboard') b.tint_color ='red' b.image = ui.Image.named('iob:ios7_search_32') b.background_color = 'white' h = su.frame().size.height b.frame = (2,2,h-4,h-4) b.action = test b.console = sv #print(dir(sv)) retain_global(b) su.addSubview(ObjCInstance(b)) ret = analyze(sv,indent+' ') if ret: return ret ret = analyze(main_view,'') return ret if __name__ == '__main__': r = FindTextInConsole()
@Matteo Sorry, I did not see your last post, just before my script.
If you check this one, you will see the line
t = str(tv.text())
it contains the full console...
do you mean OMxxxxx is something related to omz library, not Apple?
Code edited, crashed if text to search empty (to clean console).
I said there were bugs, isn't it? 😀
if txt == '': return
mikael last edited by
pavlinb last edited by
@cvp Hi, I haven't tried it yet your code but it is impressive! Thank you :-)
I sometimes feel that there is a possibility to do something with programming, but every time I see that one of you programmers really manages to do it, I am fascinated.
I want to try your code by saving it in the StatusBarMenu (by JonB) as an action to run with the little buttons of it. So my dream is to associate your script to a user key of StatusBarMenu in order to enable/disable the search text function.
So do you know if it is possible to disable your search function (with a click) if user wants to return to use the console to run python commands instead of to search text?
Thank you very much
@Matteo It is not needed. Even if you activate the search, the normal use of the console command still works by pressing enter. The search is only done if you tap the 🔍 icon.
@mikael Hi, I still use Pythonista version 3.1 (I have an old iphone with ios 10.3.3) and it is a great software, I find it very exciting to think that user can even customize the interface of this application as you wish, if knows how to do it ;-)
Pythonista is Great!
Pythonista is Great!
Full agree. I hope you have seen my previous post because we wrote at the same time