Run terminal commands in menu bar


(Psykik Hunter) #1

I have no idea how to use Swift, so any help would be greatly appreciated. I am trying to make a menu bar app that makes all the icons on my desktop disappear through the command

defaults write CreateDesktop false
killall Finder

I have been searching for a while, but I cannot find an answer to my question. My code for the menu bar is

 import Cocoa
class AppDelegate: NSObject, NSApplicationDelegate {
    let statusItem = NSStatusBar.system.statusItem(withLength:NSStatusItem.squareLength)
    @objc func printQuote(_ sender: Any?) {
        let quoteText = "Never put off until tomorrow what you can do the day after tomorrow."
        let quoteAuthor = "Mark Twain"

        print("\(quoteText) — \(quoteAuthor)")
    func constructMenu() {
        let menu = NSMenu()

        menu.addItem(NSMenuItem(title: "Print Quote", action: #selector(AppDelegate.printQuote(_:)), keyEquivalent: "P"))
        menu.addItem(NSMenuItem(title: "Quit Quotes", action: #selector(NSApplication.terminate(_:)), keyEquivalent: "q")) = menu

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        // Insert code here to initialize your application
        if let button = statusItem.button {
            button.image = NSImage(named:NSImage.Name("StatusBarButtonImage"))
            button.action = #selector(printQuote(_:))


    func applicationWillTerminate(_ aNotification: Notification) {
        // Insert code here to tear down your application


I grabbed this from a useful website, but I have no idea how to proceed, and any help would be appreciated. I am really sorry if this has been posted before, but I haven't been able to do it after a couple of hours. If you need more info, I will be happy to provide it, to the best of my limited ability.

(Rob Mayoff) #2

Don't write an app for this. Just use AppleScript.

Launch Script Editor (comes with macOS in /Applications/Utilities). Paste this into a new script:

do shell script "defaults write CreateDesktop NO; killall Finder"

Save the script as “Hide Desktop Icons" in the folder ~/Library/Scripts (which you might need to create).

Then, still in Script Editor, from the menu bar choose Script Editor > Preferences. Turn on the “Show Script menu in menu bar” check box.

You now have a script menu doodad in the right half of your menu bar (in all applications). If you open the script menu, you'll see the “Hide Desktop Icons” script.

You probably also want to make another script named “Show Desktop Icons” that sets CreateDesktop to YES instead of NO.

In my testing, I often had to run each script a couple of times to make it work. Possibly inserting some sleep commands would fix that.

(Psykik Hunter) #3

Ok. I will definitely try that when I get on my laptop. I thought about this, but I thought you had to open terminal through AppleScript to run it. This sounds like it will work better. Thanks!
And yes I am going to create a show script. Might be a little difficult to find things otherwise!

So I just tried it, and it works great. I see what you mean about running it multiple times, but otherwise it seems to work. I did try adding a delay, but it didn't really change anything. Any other suggestions to fix this?