How to run command line App in Main App using swift/SwiftUI (MacOS).

HI,

i am created command line tool in main swiftui app for MacOS using the following link.

Apple Developer Link

my queries are:

  1. How to run command line app when main swiftui app starts ?
  2. How to check command line app is running or not ?
  3. How to stop command line app using main app?
  4. How to get data from command line app using main swiftui app?
  5. How to terminate command line app when main app terminates?

Have you looked into XPC, NSTask, NSRunningApplication and searched for "SwiftUI app lifecycle"? If things are getting complicated, have you reconsidered using a command line app (i.e. what are you trying to do?)

Is there a reason you need to package this as a command-line tool? Based on your list of requirements, you might be better off with an XPC service.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Hi,

I am implementing the following link code in xpc service which it terminates the xpc service by saying memory issue.

link

I am trying to implement blocking websites in macOS

An XPC service only runs as long as the container app runs. Do you want your program to run after your app has quit? If so, an XPC service isn’t for you.

Reading the link you posted it seems that the code you want to run has to execute with privileges (aka “run as root”). Is that true in your case as well?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Hi,

i am trying to implemented block websites functionality as xpc service.

link

i am getting following error

[22516:3818158] [general] Attempting to perform block on main runloop, but the main thread has exited. This message will only log once. Break on _CFRunLoopError_MainThreadHasExited to debug

[22516:3818158] [general] Attempting to wake up main runloop, but the main thread as exited. This message will only log once. Break on _CFRunLoopError_MainThreadHasExited to debug.

which it terminates the xpc service saying

Message from debugger: Terminated due to memory issue

Hi,

I am trying to run Console App code in Swiftui project.

The Code with consists of SBApplication to get data from bundle identifier which is not working.

      let safariObject: SafariApplication = SBApplication(bundleIdentifier: "com.apple.Safari")!

      let frontWindow = safariObject.windows!()[0] as! SafariWindow
      let activeTab = frontWindow.currentTab!

      let url = activeTab.URL
      let activeTab = BrowserTab.safari(activeTab)

Note: Same code working fine when it is implemented in console app

> The Code with consists of SBApplication to get data from bundle > identifier which is not working.

That code generates and sends Apple events to Safari. There are multiple obstacles to that:

  • If you have the hardened runtime enabled, which you should, you need to set the com.apple.security.automation.apple-events entitlement. Do this in the Signing & Capabilities editor.

  • You’ll also need NSAppleEventsUsageDescription in your Info.plist.

  • If you have the App Sandbox enabled then things get more complex. Let me know if that’s the case and we can talk about your options.

  • When you send the Apple event the user will have to approve the resulting Automation privilege.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Hi,

Let us explain my implementation using above mentioned four points

added first and second point functionality in my app with sandbox disabled. which it is working fine after accepting fourth point permissions.

but my requirement is with first and second are implemented and sandbox enabled with accepting fourth point.

I’m not sure I completely understand what you wrote but my best guess is that:

  • You have this working with the App Sandbox disabled.

  • But things fail with it enabled.

Is that right?

If so, the best path forward depends on why you enabled the App Sandbox:

  • If you enabled it because it’s the right thing to do, use a temporary exception entitlement to get around this restriction.

  • If you enabled it because you plan to submit to the App Store, a temporary exception entitlement won’t work because App Review is very unlikely to approve that. While I don’t work for App Review and can’t make definitive statements on their behalf, it seems to me that your high-level goal — recording what the user is doing in Safari — seems fundamentally incompatible with App Store distribution.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

How to run command line App in Main App using swift/SwiftUI (MacOS).
 
 
Q