How to open applications in fullscreen via Terminal

How can I keep Terminal in foreground while the other apps are being open?

Use the -g or --background flag to open:

 -g  Do not bring the application to the foreground.

for example:

open -g -a Notes

will open Notes.app in the background.

Unfortunately not all apps appear to respect this. For those apps you may need to hide them manually using AppleScript:

osascript -e 'tell application "System Events" to set visible of process "FOO" to false'

where FOO is the name of the app.

p.s. - instead of $(whoami) you can use $LOGNAME


Here's an answer without using keystrokes (as I use Rectangle which uses the command + option + f keystroke).

oinfs() {
    for app in "${@}"
    do
        # get application name (applicationName) from case insensitive input
        # activate application (delay while app opens.  may need to be changed to delay 3)
        # set application to fullscreen
        osascript \
            -e "tell application \"System Events\"" \
                -e "set applicationName to name of my application \"${app}\"" \
            -e "end tell" \
            -e "tell application applicationName" \
                -e "activate" \
                -e "delay 1" \
                -e "tell application \"System Events\" to tell process applicationName" \
                    -e "set value of attribute \"AXFullScreen\" of window 1 to true" \
                -e "end tell" \
            -e "end tell"
    done
}

To use this you can run

oinfs Safari "Google Chrome" # or `oinfs Safari Google\ Chrome`

The first three lines of the script gets the application name from a case insensitive input, so you can also run

oinfs safari google\ chrome