Skip to content

Instantly share code, notes, and snippets.

@peterxjang
Last active September 25, 2022 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterxjang/a530e72131774a1a8b8ae8f6e86b1bb7 to your computer and use it in GitHub Desktop.
Save peterxjang/a530e72131774a1a8b8ae8f6e86b1bb7 to your computer and use it in GitHub Desktop.
  1. Install xbar

    • (if you have homebrew installed, you can run brew install --cask xbar in your terminal to install it)
  2. Run xbar by double-clicking the application or using Spotlight (cmd space)

    • xbar should show up in your mac menubar
  3. Open a terminal, navigate to a folder for coding projects, make a new folder for your plugin:

    mkdir xbar-jokes-plugin
    cd xbar-jokes-plugin
  4. Make a new file in this folder called jokes.1h.rb and paste the following code:

    #!/usr/bin/env ruby
    require "http"
    
    response = HTTP.get("https://official-joke-api.appspot.com/random_joke")
    data = response.parse
    
    puts data["setup"]
    puts "---"
    puts data["punchline"]
    • This example uses the http gem to make a web request to an API to get a random joke. You'll need to make sure this gem is installed for this particular script to work (gem install http).

    • The name of the file determines how often it will refresh. 10s - ten seconds, 1m - one minute, 2h - two hours, 1d - a day, etc.

    • (Note): if you're using rbenv, you may need to replace the first line with the full path to your Ruby executable, which you can find by running which ruby in your terminal. The first line would look something like this:

      #!/usr/bin/env /Users/___/.rbenv/shims/ruby

  5. In your terminal, run the following command to make the file executable

    chmod +x jokes.1h.rb
  6. In your terminal, run the following command to test the script is executable:

    ./jokes.1h.rb
    • You should see a joke setup and punchline print out in the terminal
  7. In your terminal, run the following command to copy your file to the xbar plugins folder:

    cp jokes.1h.rb ~/Library/Application\ Support/xbar/plugins
  8. In the xbar menubar, click "Refresh all" to run your script

    • You should see a random joke setup in the menubar
    • If you click on the joke setup, you should see the punchline in the menu below

Read more about creating xbar plugins: https://github.com/matryer/xbar-plugins/blob/main/CONTRIBUTING.md If you need to store API keys in environment variables, read this guide.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment