matryer / xbar-plugins

Plugin repository for xbar (the BitBar reboot)
https://xbarapp.com
2.44k stars 1.04k forks source link

Open App #2015

Open donzee529 opened 6 months ago

donzee529 commented 6 months ago

How do I make an icon show in the menu bar and when clicked it toggles open/close a certain app?

sprak3000 commented 6 months ago

@donzee529

Have you read through the Writing plugins guide? Start there to get a general overview of how plugin development works.

donzee529 commented 6 months ago

I tried but it wasnt working

#!/bin/bash

# Specify the path to the Pictures folder
pictures_folder="~/Pictures"

# Specify the name of the app to toggle
app_name="MyApp"

# Check if the app is running
if pgrep -xq "$app_name"; then
    # App is running, so we'll stop it
    killall "$app_name"
    echo "Start $app_name | shell=/usr/bin/open | param1=-a | param2=$app_name | emojize=false"
    echo "----"
    echo "Pictures Folder | href=file://$pictures_folder | image=base64_icon_data_here"
else
    # App is not running, so we'll start it
    open -a "$app_name"
    echo "Stop $app_name | shell=pkill | param1=-x | param2=$app_name | emojize=false"
    echo "----"
    echo "Pictures Folder | href=file://$pictures_folder | image=base64_icon_data_here"
fi
sprak3000 commented 6 months ago

@donzee529

One thing that jumps out is having too many hyphens for the menu separator.

This

    echo "----"

should be

    echo "---"

Three hyphens... That will at least get the plugin to have your Pictures Folder menu item show up. If I run your plugin and substitute for MyApp, the plugin loads up the application at startup and then shows the Stop <app name> in the menu bar. However, I don't think the menu bar item does anything beyond opening up the menu when you click on it. If you want an action to stop the application, that would need to go in the menu after the echo "---" line, as you have the Pictures Folder link.