Forum Archive

Check if Pythonista is in background

userista

Is there a callback or some way to tell if the app is in the background/foreground?

import webrowser
webrowser.open('safari-http://')
# I want the next line to execute only when returning back to Pythonista
print "Back here"

I tried this work around but it always returns false (I would've wrapped it in a setTimeout and checked every 30ms)

ui.WebView().eval_js('document.hidden')

ccc

http://omz-software.com/pythonista/docs/ios/scene.html#scene.Scene.pause

http://omz-forums.appspot.com/pythonista/post/5234385359994880

http://omz-forums.appspot.com/pythonista/post/5988988296888320

userista

Ah perfect! thanks - would importing scene just to use scene.Scene().pause() be overkill?

EDIT: Never mind - this only works if I have an instance of a scene - I don't think subclassing scene just for this pause is a good practice. So I can't really use scene.pause

[deleted]

It looks like if Scene is run in SceneView... it doesn't receive the pause/resume...

import ui, scene

class scTest(scene.Scene):
    def pause(self):
        print 'pause'

    def resume(self):
        print 'resume'

if False:
    scene.run(scTest())
else:
    v = ui.View()
    sv = scene.SceneView()
    sv.scene = scTest()
    #sv.hidden = True
    v.add_subview(sv)
    v.present('panel')
ccc

Yes. That is documented at: http://omz-software.com/pythonista/docs/ios/scene.html#integration-with-the-ui-module

JonB

There is no guarantee that your script will continue to run once you launch safari. In fact, there is a guarantee that in a few minutes, the background timer will expire, and your script will be unceremoniously killed.

If you just want to do something on a webpage, it is better to do it within a webview since you could make it modal for example, and continue only when the dialog is closed. Someone should really make a webview based clone of a full browser, with address bar, navigation, reload, stop, etc, in which case you'd never have to launch safari.

userista

I am trying to open Google Authenticator for 2FA - I want to send them there and then when they return I would fill a prompt with clipboard.get() - so in this case a webview doesn't really help me.

(To keep my script running I could use console.set_idle_timer_disabled(flag) like cclaus has shown.)

ccc

The idea in NoDoze.py is that you setup a notification to wake yourself back up just before you shut yourself down. An interesting departure point. ;-)

Sebastian

@JonB I started working on something like that a while ago. I got history and bookmarks working as well. I don't really know how to get tabs working though.

[deleted]

@hyshai It looks like this might do it....

import ui, scene

def AmIBack():
    global gbGone
    if gbGone and not sv.paused:
        gbGone = False
        print "Back here"
    else:
        if sv.paused:
            gbGone = True
        ui.delay(AmIBack, 1)

gbGone = False
v = ui.View()
sv = scene.SceneView()
sv.hidden = True
v.add_subview(sv)
v.present('panel')
AmIBack()

Edit: no need to set the scene... just SceneView will do

JonB

@sebastian. Post to github, others will contrib.

@hyshai. Ok, you originally said you wanted to open safari, not that you wanted to launch the authenticator app. Presumably you want to open a page which has an otpauth:// URI

See this.
Seems like you could create your own implementation in python, no external app required... After all, google authenticator is open source.
You could implement the custom uri handler for this in webview, and even autofill it, or have a little counter on the page showing the code in real time.

userista

@JonB ah sorry - just was trying to give a trivial example. But that's a great idea - though it's more work ;)

@tony hmm gonna try that - looks good

userista

@JonB - I just realized that you were giving me a solution for the opposite problem - I wasn't clear, my bad. I want for them to get the 2FA token so that they can login to their account. So implementing my own Google Authenticator app isn't a great solution because then I would have to store their secret in order to generate the token each time - and that's way too scary for me.