Skip to content

Instantly share code, notes, and snippets.

@mittnavnermike
Created September 6, 2022 13:41
Show Gist options
  • Save mittnavnermike/3330b95b21f4702de60097b2c06308ae to your computer and use it in GitHub Desktop.
Save mittnavnermike/3330b95b21f4702de60097b2c06308ae to your computer and use it in GitHub Desktop.
// Name: Show/Hide desktop files
// Description: Hides your desktop files on macos. By default it prompts you if you'd like to restart Finder. You can override this behaviour in .env set `ASK_BEFORE_RESTART_FINDER=<boolean>`
// Author: Mike Patterson
// Github: @mittnavnermike
import '@johnlindquist/kit'
const shouldPrompt = (await env('ASK_BEFORE_RESTART_FINDER')) || 'true'
const desiredSettingArg = await arg('What would like to be the default?', [
'Hide desktop files',
'Show desktop files',
])
let shouldRestartArg: string
// Options for shouldRestartArg. This is separated out to have on source of truth
const shouldRestartOptions = ['Yes, restart', `No, I'll restart Finder myself`]
// Respect the .env ASK_BEFORE_RESTART_FINDER
if (shouldPrompt.trim() === 'true') {
// Warn user that we need to restart Finder
shouldRestartArg = await arg(
'For this to take effect we need to restart Finder',
shouldRestartOptions
)
}
// shouldRestartArg converted to an actual boolean (was string)
const shouldRestart = shouldRestartArg === shouldRestartOptions[0]
// desiredSetting converted to an actual boolean (was string)
const desiredSetting = desiredSettingArg === 'Show desktop files'
// Setting the desiredSetting to show or hide desktop files in macos
await $`defaults write com.apple.finder CreateDesktop ${desiredSetting}`
// Function to restart finder
const restartFinder = async () => {
// Restart Finder
await $`killall Finder`
// Display notification
notify(`Desktop files are now ${desiredSetting ? 'showing' : 'hidden'}`)
}
if (shouldPrompt) {
if (shouldRestart) {
restartFinder()
} else {
// Display notification
notify(
`Restart finder for new setting to take effect. Copied command to your clipboard.`
)
// Copy restart Finder command to clipboard
copy(`killall Finder`)
}
} else {
restartFinder()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment