How to use script to open an app if it's not the front most app, and hide it when it is the front most app in macos? I need a script to open an app if it's not the frontmost app and hide(command+h) it when it is the frontmost app.
e.g 'Kim.app'
how to write this in Applescript or shell script?
Here is some info I can get:
Getfrontmost:
osascript -e 'tell application &quot;System Events&quot; to get name of application processes whose frontmost is true and visible is true'

Open:
open -a 'Kim.app'

Hide:
osascript -e 'tell application &quot;System Events&quot; to set visible of process &quot;Kim&quot; to false'

still don't know how to write this
res=$(osascript -e &quot;tell application \&quot;System Events\&quot; to get name of application processes whose frontmost is true and visible is true&quot;);
if [ $res == &quot;Kim&quot; ]
then
    osascript -e &quot;tell application \&quot;System Events\&quot; to set visible of process \&quot;Kim\&quot; to false&quot;
else
    open -a 'Kim.app'
fi

I did it like this, it works but excutes very slow, how to optimize this?