I
I
Intenditore2019-05-28 20:29:04
C++ / C#
Intenditore, 2019-05-28 20:29:04

MacOS - how to write a utility to run in the background?

Greetings.
I am an absolute noob. I recently learned C++, before that I was in Python, I wrote in JS/PHP for a very long time. That is, I have no experience in system programming.
The task arose - to make one utility for working in the background. I understand in principle how, but there are a lot of nuances - how to register a service and run it? How to hang its icon in the menu bar and add a settings window? And most importantly - how to debug all this, because in the playground this will not start?
The question is not idle - the courses reviewed by me describe the creation of window applications "in themselves". Interaction with the system, if described, is very brief and not detailed.
I would be grateful for a tip on the materials that will allow you to quickly get up to speed, even in general terms.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Kerimov, 2019-05-30
@Intenditore

Info.plist:
LSUIElement = YES
AppDelegate.swift:

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
    let statusMenu: NSMenu = .init()
    let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength)

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        
        statusItem.button?.image = NSImage.init(named: NSImage.actionTemplateName)
        statusItem.menu = statusMenu
        
        let quitMenuItem: NSMenuItem = .init(title: "Quit", action: #selector(quit), keyEquivalent: "")
        statusMenu.addItem(quitMenuItem)
    }
    
    @objc func quit() {
        NSApp.terminate(self)
    }
}

The rest is in the courses.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question