#!/usr/bin/env python3
from controlfloor import *

def open_url( url ):
    launchApplication( "com.apple.mobilesafari" )
    wait(4)
    retries = 4
    while retries:
        retries -= 1
        tap( "TabBarItemTitle" )
        wait(1)
        if not getKeyboards():
            continue
        # If the URL was empty, this button is not present
        tap( "ClearTextButton", allow_errors = True )
        break
        wait(1)

    typeText( url + "\n" )

def add_options( parser ):
    parser.add_argument( "--url", help="URL to open" )

def run( opts ):
    open_url( opts.url )

cfrun(__name__, run, add_options )

