diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..dfe0770 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..496ee2c --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_Store \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..c383552 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +© 2019 Arthur Factor (fqtr.ga) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..466fdf4 --- /dev/null +++ b/Makefile @@ -0,0 +1,28 @@ +PREFIX ?= /usr/local +PWD ?= $(shell pwd) +CMD = barhide + +.PHONY: install all uninstall update upgrade remove + +install: + install $(CMD).sh ${PREFIX}/bin/$(CMD) + +all: + @echo "\nUsage: make [option]" \ + "\n \033[90m# Option left empty performs install\033[39m" \ + "\n uninstall \033[90m# Removes the script\033[39m" \ + "\n update \033[90m# Updates only the repo\033[39m" \ + "\n upgrade \033[90m# Makes update & install\033[39m" \ + "\n remove \033[90m# Moves to trash or deletes\n" + +uninstall: + rm -f $(PREFIX)/bin/$(CMD) $(PREFIX)/bin/$(CMD).old + +update: + git clone --no-checkout https://github.com/DaFuqtor/$(CMD) ~/$(CMD)/$(CMD).tmp && rm -rf ~/$(CMD)/.git && mv ~/$(CMD)/$(CMD).tmp/.git ~/$(CMD)/ && rmdir ~/$(CMD)/$(CMD).tmp && cd ~/$(CMD) && git reset --hard HEAD + +upgrade: + make update && make + +remove: + mv $(PWD) ~/.Trash/$(CMD) || rm -rf $(PWD) \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..69a5744 --- /dev/null +++ b/README.md @@ -0,0 +1,39 @@ +
+

barhide

+

CLI to hide unwanted icons from your menu bar

+
+ +## Usage + +```powershell +barhide [-hV] [options] | + [-u, --update] # Check for update and ask to install + # Options: + [-f, --force] # Force preference change +``` + +- barhide SystemUIServer to hide the notification centre icon + +## Install · [![GitHub release](https://img.shields.io/github/release/dafuqtor/barhide?label=%20&color=gray)](//github.com/DaFuqtor/barhide/releases) + +### Using :beer: [Homebrew](//brew.sh) + +```powershell +brew install dafuqtor/tap/barhide +``` + +### Using `curl` + +```powershell +curl -sL raw.githubusercontent.com/DaFuqtor/barhide/master/install.sh | sh +``` + +### Using Source Code + +```powershell +git clone https://github.com/DaFuqtor/barhide +cd barhide +make +``` + +> Also allows to `make [ upgrade, uninstall ]` diff --git a/barhide.sh b/barhide.sh new file mode 100644 index 0000000..85a890b --- /dev/null +++ b/barhide.sh @@ -0,0 +1,174 @@ +#!/usr/bin/env sh + +VERSION=0.1.0 +CMD='barhide' + +name= +app= +id= +rdAble= +invis= +force= + +usage() { + cat <| + $CMD SystemUIServer to hide the notification centre icon + + Options: + -f, --force Force preference change + -V, --version Output version + -u, --update Check for update and ask to install + -h, --help This message. + +EOF +} + +barhide() { + if ! [ "$1" ]; then + usage + return 0 + fi + for opt in "${@}"; do + case "$opt" in + -h|--help) + usage + return 0 + ;; + -V|--version) + echo "$VERSION" + return 0 + ;; + -u|--update) + echo "$CMD $VERSION" + printf " Checking for the update...\r" + gitver=$(curl -s https://api.github.com/repos/DaFuqtor/$CMD/releases/latest | grep tag_name | head -1 | awk -F: '{ print $2 }' | sed 's/[\",]//g' | tr -d '[:space:]') + if [ "$gitver" = "$VERSION" ]; then + echo " Already up to date. " + else + if [ "$gitver" ]; then + echo " Latest version is $gitver " + read -r -p "Do you want to update? [Enter/Ctrl+C]" response + if [[ $response =~ ^( ) ]] || [[ -z $response ]]; then + if command -v brew > /dev/null && brew list "$CMD" &> /dev/null; then + brew upgrade "$CMD" + else + echo "\n - Downloading latest $CMD ($gitver)" + curl -sL raw.githubusercontent.com/DaFuqtor/"$CMD"/master/install.sh | sh && echo " - Update completed!\n" + fi + else + echo " Enjoy the outdated $CMD" + fi + else + echo " Couldn't check for update!" + wifi_interface=$(networksetup -listallhardwareports | grep -A 1 Wi-Fi | grep Device | awk '{print $2}') + if [ "$(networksetup -getairportpower "$wifi_interface" | awk '{print $4}')" = "Off" ]; then + read -r -p "Wi-Fi Adapter is disabled. Maybe you should consider turning it on? [Enter/Ctrl+C]" response + if [[ $response =~ ^( ) ]] || [[ -z $response ]]; then + networksetup -setairportpower "$wifi_interface" On && sleep 1 && $CMD "${@}" + fi + fi + exit 1 + fi + fi + return 0 + ;; + -f|--force) + force=1 + ;; + *) + if [ "$name" ]; then + name="$name $opt" + else + name="$opt" + fi + esac + done + + if [ $(mdfind kMDItemCFBundleIdentifier = "$name") ]; then + # given bundle id instead of app, get its path + app=$(mdfind kMDItemCFBundleIdentifier = "$name") + id=$name + fi + + if ! [ "$app" ]; then + # search for the app + app=$(mdfind -onlyin /Applications/ -onlyin /System/Library/CoreServices/ "(kMDItemContentTypeTree=com.apple.application) && (kMDItemDisplayName == '$name*'cdw)" | head -1) + fi + + rdAble=$(basename -s .app "$app") + + if [ "$rdAble" ]; then + + if ! [ "$id" ]; then + # fast (0.01s-0.02s) + id=$(/usr/libexec/PlistBuddy -c 'Print CFBundleIdentifier' "$app"/Contents/Info.plist) + fi + + if ! [ "$id" ]; then + # Search for partial match ~4x slower than PlistBuddy + id=$(mdls -name kMDItemCFBundleIdentifier -raw "$app") + fi + + if ! [ "$id" ]; then + # ~8x slower than PlistBuddy + id=$(osascript -e 'id of app "$app"') + fi + + if ! [ "$id" ]; then + # Search in running apps, ~2x slower than PlistBuddy + id=$(lsappinfo info -only bundleid "$rdAble" | cut -d '"' -f4) + fi + + else + echo "No app with this name" + exit 1 + fi + + if defaults read "$id" "NSStatusItem Visible Item-0" &> /dev/null; then + if [[ $(defaults read "$id" "NSStatusItem Visible Item-0") = 0 ]]; then + invis=1 + fi + else + if ! defaults read "$id" "NSStatusItem Preferred Position Item-0" &> /dev/null; then + echo "Seems like the \"$rdAble\" app has no status item at all" + if [ "$force" ]; then + echo 'Forcing preference change...' + else + echo 'To force showing/hiding this status icon, use -f flag' + exit 1 + fi + fi + fi + + printf "$rdAble with id $id was found in $app and " + + if [ "$invis" ]; then + defaults delete "$id" "NSStatusItem Visible Item-0" && \ + echo "shown" + else + defaults write "$id" "NSStatusItem Visible Item-0" 0 && \ + echo "hidden" + fi + + if pgrep "$rdAble" > /dev/null; then + killall "$rdAble" &> /dev/null + if ! echo "$app" | grep /System/Library/CoreServices/ > /dev/null; then + # App is not from Core, so it's not relaunching itself. Do relaunch + if ! pgrep "$rdAble" > /dev/null; then + # LSOpenURLsWithRole() failed for the application /System/Library/CoreServices/Spotlight.app with error -600. + # rare error appears on opening. + open -a "$rdAble" + fi + fi + fi + +} + +if [[ ${BASH_SOURCE[0]} != $0 ]]; then + export -f $CMD +else + $CMD "${@}" + exit $? +fi \ No newline at end of file diff --git a/install.sh b/install.sh new file mode 100644 index 0000000..14f0d21 --- /dev/null +++ b/install.sh @@ -0,0 +1 @@ +curl -# https://raw.githubusercontent.com/DaFuqtor/barhide/master/barhide.sh -o /usr/local/bin/barhide && chmod +x /usr/local/bin/barhide \ No newline at end of file