Andrew
Andrew

Reputation: 11405

How to use system functionality programmatically via swift?

If we look on hotkeys/shortcuts of the macOS, we can find there a lot of functionality. Some functionality is assigned to hotkeys, some is not.

As example:

make screenshot(cmd+shif+F5)

or to show desktop (hide all apps) (F11)

and lot of other built-in into the OS functions:

enter image description here

Question is the following: how to use this functionality from my code without using of hotkeys pressing imitation?

The Goal is do not write custom code in case of such functionality already excist in OS.


I have found only how to open some sort of system preferences, maybe this is direction that I need to go, but I didn't found any details via google.

import Cocoa

// Open Siri prefs
NSWorkspace.shared.open(URL(fileURLWithPath: "/System/Library/PreferencePanes/Speech.prefPane"))

// Open Siri prefs with another way
NSWorkspace.shared.open(URL(fileURLWithPath: "x-apple.systempreferences:com.apple.preference.speech"))

// Open System Preferences window
NSWorkspace.shared.open(URL(fileURLWithPath: "x-apple.systempreferences:com.apple.preference"))

Upvotes: 3

Views: 444

Answers (1)

Asperi
Asperi

Reputation: 258277

The area you are looking at is System Services, and actually there is no one answer on your question, because some of those items available via own API, some of them are private macOS features, some of them available via actually services.

You can start your findings with:

BOOL NSPerformService(NSString *serviceItem, NSPasteboard *pboard)

and with Services Implementation Guide, which depicts details of that.

The other mechanism to use those things was AppleScript... very wide area.

Upvotes: 3

Related Questions