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?
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?