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.
How to prevent commands execution in Pythonista console
-
@JonB ok with scripts, but I'm searching for a way to use only Pythonista console with sage sarver (it is sometimes more convenient and fast to stay in console environment than to swipe from/to console and script editor).
Sorry for patience, I ask if Pythonista console module (ore some extra objc code) can be used by a wrench action to read console input written by user without pressing the return key on keyboard. I mean:
- I write in console the following:
>>> a=1.5
- I don't press return key on keyboard but I press a wrench action C that reads the input and save it as a string in a variable (for example named "console_input"), so regardless of whether or not I pressed the return button on keyboard, if I write in console:
>>> console_input
and press return, I'd like to see the output as a string (not evaluated, like if I do print('a=1.5')):
>>> a=1.5
Can it be possible?
Thank you
Regards -
Hi, I’ve found something related to my question in this forum and I’ve tried to search in the web something about objc, but without success.
We know that the following code (found thanks to this forum) works for editor environment (as a common wrench action or user key defined with StatusBarMenu by JonB):
import editor, clipboard i=editor.get_line_selection() t=editor.get_text() clipboard.set(t[i[0]:i[1]-len(t)+len(editor.get_text())])
So I can, with one single click (two clicks if built-in wrench action is used), to copy in clipboard the entire line within the active cursor. How can I do the same in console environment?
I mean: in console environment I’m interested in searching a way (sequence of objc commands) to simulate in one single action the following actions:
- the touch with finger on the active text field in console (where user inserts the command to be launched);
- the selection of all text when pop-up is shown (with pop-up key
Select All
); - the copy of the text into clipboard (with pop-up key
Copy
) in order to post-process the string.
Thanks for any help about programmatic use of the built-in keys in the Pythonista pop-up (see here) when user touches screen on text in editor and console (probably through the use of objc).
Regards
-
@Matteo I think this code, as a tool, does what you want, try and tell me
from objc_util import * import clipboard def GetConsoleText(): 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()): if str(sv.text()) == '>': next_is_console = True else: next_is_console = False elif 'OMTextView' in str(sv._get_objc_classname()): #print(sv.text(),next_is_console) if next_is_console: return str(sv.text()) ret = analyze(sv,indent+' ') if ret: return ret ret = analyze(main_view,'') return ret r = GetConsoleText() clipboard.set(r)
-
@Matteo Still a better script: you run it once as a tool or not and it installs a "copy" button at left of the console
from objc_util import * import clipboard import ui def GetConsoleText(): 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()): #print(sv.text(),next_is_console) 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_copy_outline_32') b.background_color = 'white' h = su.frame().size.height b.frame = (2,2,h-4,h-4) def test(sender): t = str(sender.console.text()) if t[-1] == '\n': t = t[:-1] clipboard.set(t) b.action = test b.console = sv retain_global(b) su.addSubview(ObjCInstance(b)) return str(sv.text()) ret = analyze(sv,indent+' ') if ret: return ret ret = analyze(main_view,'') return ret if __name__ == '__main__': r = GetConsoleText()
-
Hi @cvp, WOW, I am always fascinated when some people (like you) can write certain codes, here I feel like a child in a toy store! This forum is really full of creative people!
Sorry for delay of my response, your first script works very well, I was trying to understand it (without much success), I added it in @JonB StatusBarMenu as an action (I use this version, slightly modified, on my old little iPhone 5s with Pythonista version 3.1 - 301016, yes I still use that version ;-) ), but it is really too advanced for me, it is enough for me it works very well.
Then I tried your second script and I didn't think such that thing was possible: that is the customization of the input field in console environment!
Now (after some dive in objc) it is possible to process with one single touch each console input.
My next step is to understand how to recover functions, definitions and global variables like numbers, strings, arrays, etc... created in built-in Pythonista interpreter in order to pass them in the string in console as arguments of any function that must be executed by a remote interpreter (for example when the function is not available in Pythonista, I use a lot Scipy for example).
Many thanks for your help!
Regards -
@Matteo Happy to help.
My last code has perhaps some lines too much because I did not remove (old) part for copying the console line in the clipboard without tapping the icon.
The action is actually named "test" but you can of course write there your own code -
With this
def test(sender): t = str(sender.console.text()) if t[-1] == '\n': t = t[:-1] sender.console.setText_('console_input="'+t+'"') clipboard.set(t)
You type a=1.5, then tap the "copy" icon, and you get
And you still have to type the return key...
-
Thank you @cvp , I think
console.text()
should beGetConsoleText()
, based on your previous post. Am I right? And I can't understandconsole.setText_
: what is it?
Bye -
@Matteo no, no.
When I create the button b, I save as an attribute b.console, the associated textview sv.
When the button is pressed, its action is executed.
Sender is the button, thus sender.console is the associated textview.
sender.console.text is thus textview.text
We change the text and setText_() is a method of the textview to change its content
That's all folks 😀Edit: this code is IN GetConsoleText thus I can't call it-self
-
@cvp thank you for explanation, unfortunately I have not familiarity with words like "attribute" and "method" (object-oriented programming). Anyway your code works so it is enough for me :-)
Bye