0

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 "System Events" to get name of application processes whose frontmost is true and visible is true'

Open:

open -a 'Kim.app'

Hide:

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

still don't know how to write this

1 Answer 1

0
res=$(osascript -e "tell application \"System Events\" to get name of application processes whose frontmost is true and visible is true");
if [ $res == "Kim" ]
then
    osascript -e "tell application \"System Events\" to set visible of process \"Kim\" to false"
else
    open -a 'Kim.app'
fi

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

7
  • If this is shell script, then how is eval useful for the second and the third use etc? wouldn't it work directly ?
    – anki
    Commented Sep 10, 2020 at 9:19
  • Even the first eval isn't strictly necessary, you can just run the command within $() directly.
    – nohillside
    Commented Sep 10, 2020 at 9:33
  • In this use case, there is absolutely no good reason whatsoever to be using eval, it's just isn't necessary here! Commented Sep 10, 2020 at 15:17
  • Removed the unnecessary evals.
    – nohillside
    Commented Sep 11, 2020 at 8:14
  • Thank you, I removed eval, but the shell script still running slow(response slow),
    – user956609
    Commented Sep 14, 2020 at 3:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .