bashterminaliterm2osascript

how do you force apple script + aws vault to wait for mfa access


I have an apple script like this

#!/bin/zsh

tell application "iTerm"
    activate
    select first window

    # Create new tab
    tell current window
        create tab with default profile
    end tell

    # Split pane
    tell current session of current window
        split vertically with default profile
        split vertically with default profile
        split vertically  with default profile
    end tell

    # Exec commands
    tell first session of current tab of current window
        write text "aws-vault exec my-role -d 12h --no-session"
        write text "start him"
    end tell
    tell second session of current tab of current window
        write text "start her"
    end tell
    tell third session of current tab of current window
        write text "start you"
    end tell
    tell fourth session of current tab of current window
        write text "start me"
    end tell
end tell

the problem is the script doesn't wait for me to fill in the mfa information from aws command. I've also tried aws-command; start him but that just exits and doesn't execute start him at all. Anyone run into this before?


Solution

  • I don't think this is really possible, because Apple Script has no way of knowing that the aws command requires mfa information and if you are done typing that information.

    But there are 2 very hacky ways in which you could achieve this:

    Using delay

    This option is probably very unreliable, but it may do the job. You can use the delay command to make AppleScript wait X seconds until it runs write text "start him". Lets say it takes you around 10 seconds to type out the mfa information, then you would use delay 10. Below is how the code would look like.

    # more code above...
        tell first session of current tab of current window
            write text "aws-vault exec my-role -d 12h --no-session"
            delay 10    # <-- change this value to your liking
            write text "start him"
        end tell
    # more code below...
    

    Using display dialog

    I personally feel this may be the most reliable option for you. What you can do is have a dialog box open and once you have typed out the mfa information, click "Ok" so that the script resumes. So you'd have something like this:

    # more code above...
        tell first session of current tab of current window
            write text "aws-vault exec my-role -d 12h --no-session"
            display dialog "Click Ok once you are done "
            write text "start him"
        end tell
    # more code below...
    

    Just a small warning: I haven't tested the above code as I do not own a macOS computer.