MacOS – Service only application

applescriptautomatorjavascript-automationmacosservices

I want to build a simple service that will allow me to keep any app window always on top or always on the same screen, when switching desktops.

Is there a way to build a global MacOS service-only application (without any windows etc.) and how is the preferred way to do this? (Automator, AppKit, SwiftUI).
The app should also be distributable via AppStore (or if not possible just GitHub).

Best Answer

You could build such a service using any tool that results in an application bundle; this includes AppleScript/JavaScript (OSA), Objective-C, and Swift. Apple would likely prefer you used Swift.

Investigate the Accessibility APIs for interacting and manipulating windows owned by other applications.

See the Info.plist value LSUIElement for making an agent application:

LSUIElement (Boolean - macOS) specifies whether the app runs as an agent app. If this key is set to YES, Launch Services runs the app as an agent app. Agent apps do not appear in the Dock or in the Force Quit window. Although they typically run as background apps, they can come to the foreground to present a user interface if desired. A click on a window belonging to an agent app brings that app forward to handle events.

The Dock and loginwindow are two apps that run as agent apps.

To be distributed on Apple's Mac App Store, an application must have a user interface. This can be a simple window that enables your service, or handles preferences.

Search GitHub for status menu app for open source examples.