omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Find text in console

    Pythonista
    4
    46
    18128
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • cvp
      cvp @Matteo last edited by

      @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 😢

      1 Reply Last reply Reply Quote 0
      • cvp
        cvp @Matteo last edited by

        @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

        Matteo 1 Reply Last reply Reply Quote 1
        • Matteo
          Matteo @cvp last edited by

          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!
          Regards

          cvp 1 Reply Last reply Reply Quote 0
          • cvp
            cvp @Matteo last edited by

            @Matteo I think that OM... objects are from OMz, thus not in Apple doc

            Matteo cvp 2 Replies Last reply Reply Quote 0
            • Matteo
              Matteo @cvp 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.py that 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?

              Thank you
              Bye

              cvp 2 Replies Last reply Reply Quote 0
              • cvp
                cvp @Matteo last edited by cvp

                @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()
                

                1 Reply Last reply Reply Quote 3
                • cvp
                  cvp @Matteo last edited by

                  @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...

                  1 Reply Last reply Reply Quote 0
                  • cvp
                    cvp @cvp last edited by

                    @Matteo said:

                    do you mean OMxxxxx is something related to omz library, not Apple?

                    Yes

                    1 Reply Last reply Reply Quote 0
                    • cvp
                      cvp last edited by

                      Code edited, crashed if text to search empty (to clean console).
                      I said there were bugs, isn't it? 😀
                      Added

                      				if txt == '':
                      					return 
                      
                      mikael Matteo 2 Replies Last reply Reply Quote 0
                      • mikael
                        mikael @cvp last edited by

                        @cvp, @Matteo, very useful, this should be a standard feature of Pythonista.

                        Matteo 1 Reply Last reply Reply Quote 0
                        • pavlinb
                          pavlinb last edited by

                          Just brilliant.

                          1 Reply Last reply Reply Quote 0
                          • Matteo
                            Matteo @cvp 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
                            Regards

                            cvp 1 Reply Last reply Reply Quote 0
                            • cvp
                              cvp @Matteo last edited by cvp

                              @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.

                              Matteo 1 Reply Last reply Reply Quote 0
                              • Matteo
                                Matteo @mikael last edited by

                                @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!

                                cvp 1 Reply Last reply Reply Quote 0
                                • cvp
                                  cvp @Matteo last edited by

                                  @Matteo said:

                                  Pythonista is Great!

                                  Full agree. I hope you have seen my previous post because we wrote at the same time

                                  1 Reply Last reply Reply Quote 0
                                  • Matteo
                                    Matteo @cvp last edited by Matteo

                                    @cvp Yes! you are right, sorry, two buttons on console line! You created a second button at left of input line, Thank you again!

                                    cvp 1 Reply Last reply Reply Quote 0
                                    • cvp
                                      cvp @Matteo last edited by

                                      @Matteo said:

                                      two buttons on console line

                                      Do you see two buttons on the console line?

                                      Matteo 1 Reply Last reply Reply Quote 0
                                      • Matteo
                                        Matteo @cvp last edited by

                                        @cvp no no, I badly explained myself sorry, I meant I see your button search at left, at right I see the built-in recall command icons and the execute command is the return key of keyboard, yes sorry. I'm excited...

                                        cvp 1 Reply Last reply Reply Quote 0
                                        • cvp
                                          cvp @Matteo last edited by

                                          @Matteo 👍

                                          Matteo 1 Reply Last reply Reply Quote 0
                                          • Matteo
                                            Matteo @cvp last edited by

                                            Hi @cvp, thanks again for your code, it helps me a lot! I tried to change it in order to clear highlighting of the text before each touch of the "Find" button, because if I search different words, the console becomes too colored and I can't understand where the last word is selected.

                                            If you want and have time, can you show me how to change your code in order to remove all highlighting before each touch of your "Find" user key?

                                            Thanks
                                            Bye

                                            cvp 1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post
                                            Powered by NodeBB Forums | Contributors