Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
macOS Desktop Finder Files Toggle
#!/bin/bash
# vim: ai ts=2 sw=2 et sts=2 ft=sh
#
# macOS Desktop Finder Files Toggle
#
# Toggle the use of Finder to display the contents of the Desktop folder as
# icons on the macOS desktop. Hides or Shows all Finder files icons on the
# Desktop.
#
# https://ss64.com/osx/defaults.html
# https://ss64.com/osx/killall.html
set -o errexit
set -o nounset
set -o pipefail
# Check OS type.
if [[ "${OSTYPE//[0-9.]/}" != "darwin" ]]; then
(>&2 echo "Error: This script is for macOS not '${OSTYPE}'.")
exit 1;
fi
# Check command exists.
# https://ss64.com/osx/defaults.html
if ! command -v defaults 1>/dev/null; then
(>&2 echo "Error: macOS user defaults system not found")
exit 1;
fi
# Read state for Finder view of desktop.
CreateDesktop="$(defaults read com.apple.finder CreateDesktop)"
# Set state for Finder view of desktop.
if [[ "$CreateDesktop" != "false" ]]; then
toggle="false"
else
toggle="true"
fi
defaults write com.apple.finder CreateDesktop "$toggle"
# Restart Finder.
# https://ss64.com/osx/killall.html
Killall Finder
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.