How to open a app with bettertouchtool and press again to hide it when it is the front most app

As the image shows, I set a button in touch bar and when I click, will launch "Kim.app",
what i want is touch it again to hide "Kim.app"(or pressing command + h when Kim.app is in the front most), how to properly achieve this?

I dont know how to properly write a shell script, i searched some, and
this is what i write, it works,but runs very slow and i don't know how to optimize this:

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

in karabiner-elements, can archive this goal very good, and running very fast, not slow like my shell script,

 {
                        "description": "f5 to open Kim.app when not in kim",
                        "manipulators": [
                            {
                                "conditions": [
                                    {
                                        "bundle_identifiers": [
                                            "^Kem$"
                                        ],
                                        "type": "frontmost_application_unless"
                                    }
                                ],
                                "from": {
                                    "key_code": "f5"
                                },
                                "to": [
                                    {
                                        "shell_command": "open -a 'Kim.app'"
                                    }
                                ],
                                "type": "basic"
                            }
                        ]
                    },
                    {
                        "description": "f5 to hide Kim.app when in kim",
                        "manipulators": [
                            {
                                "conditions": [
                                    {
                                        "bundle_identifiers": [
                                            "^Kem$"
                                        ],
                                        "type": "frontmost_application_if"
                                    }
                                ],
                                "from": {
                                    "key_code": "f5"
                                },
                                "to": [
                                    {
                                        "key_code": "h",
                                        "modifiers": [
                                            "command"
                                        ]
                                    }
                                ],
                                "type": "basic"
                            }
                        ]
                    },

But I want to use touchbar button to trigger this!

Even if I map a touchbar button to F5 , this don't work, only work in real keyboard,

How to properly done this, thanks

answer: I just find show/hide app event in btt

Imprint | Privacy Policy