How to launch another process in sandbox on Mac? - macos

I'd like to launch another own application by LSOpenApplication() in Sandbox on Mac.
Of course, I added a row into 'com.apple.security.temporary-exception.files.absolute-path.read-write' in an entitlements file for launching.
However, when launching, console spat out an error which is Not allowing process xxxx to launch "foo.app" because it has not been launched previously by the user.
It is able to launch without errors after launched the process manually once.
How can I launch the process even if not launch previously?
Is this no relation with sandboxing?

There are very few conditions under which you'll be able to launch another application and have the desired outcome. com.apple.security.temporary-exception.files.absolute-path.read-write doesn't gain you anything with regard to LaunchServices so you can remove that entitlement.
As a sandboxed application you are fairly limited in what you can actually sub-launch, this is an intentional behavior as launching another application is technically a violation of the sandbox model. the ways available to you are:
include an XPC Service in your application and have launchd launch it for you
you can run an application via NSTask, which will cause this application to inherit your sandbox when launched
you can launch an application by name, though from what i've seen this generally only works if the application is in your /Applications folder, i.e. -[NSWorkspace launchApplication:]
you can launch an application that encloses your app, but only if you've been SMLoginItemSetEnabled()
I'd say the osascript call works because its doing roughly the same as -[NSWorkspace launchApplication:]. none of the LS calls that accept bundle identifiers or absolute/relative paths are going to work.

Use osascript
osascript -e 'tell application "foo" to open'

Related

Can't Change Input Method when Launching App with Admin Privileges

I wrote a script using the Mac script editor on Mojave (10.14.15)
The script simply launches an application with Admin privileges:
on run
do shell script ¬
"/Applications/App.app/Contents/MacOS/App" user name "name" password "pw" with administrator privileges
quit
end run
I then saved this as an application and am able to launch successfully using it.
However the application that I launch (Anki study software) can not change the input setting, which means I am unable to type in any language except the default system language.
Note that making a similar application in script editor that launches without admin privileges works without issue,
ie:
on run
do shell script ¬
"/Applications/App.app/Contents/MacOS/App"
quit
end run
This seems to be the case no matter the app I launch, I also tried launching Sublime Text and the same thing occurs.
The issue also happens when using sudo to launch an app from terminal. So it definitely seems to be an issue with trying to launch an app as root.
I am new to working with AppleScript and MacOS. It seems the fault is with launching the application with admin privileges though. Any help would be greatly appreciated.

applicationShouldTerminate: not called when system restarts

I have to run some code when the app is about to terminate. applicationShouldTerminate: runs when quit is selected from the menu or when I press Cmd Q but not when I restart the mac.
Is there a way to force applicationShouldTerminate when a user tries to restart the mac? Or is it another function being called in this scenario?
Your app might have Sudden Termination enabled.
macOS 10.6 and later includes a mechanism that allows the system to log out or shut down more quickly by, whenever possible, killing applications instead of requesting that they quit themselves.
...
Debugging tip
You can determine the value of the sudden termination using the following LLDB command.
print (long)[[NSClassFromString(#"NSProcessInfo") processInfo] _suddenTerminationDisablingCount]

Why don't some programs appear on the task manager startup tab?

Many applications start at startup, however, some of them do not appear on the task manager startup tab. What is that due to?
Is there any way to do this with a program, for example, spotify?
What do I need to do in order for a program to start at startup, but not showing in the startup applications tab?
Setting it in HKCU/Microsoft/Windows/CurrentVersion/Run doesn't seem to work, as it starts, but still shows on the mentioned tab.
Thank you in advance.
Its mostly a factor of how the program itself is written. If its written to run as a service, or as a System Tray application, or otherwise.
I know there are wrappers for running any exe as a service NSSM being the main one I have experience with (but this is mostly for when there is going to be NO user interaction)
I do not know if there is anything that can allow an application to run in the system tray only, not in the taskbar, if it doesn't support it.
But since Spotify does support running minimized to the tray, it does seem like there are some ways to "start spotify minimized", Spotify or other applications might have command line options or other settings to tell them to start "hidden"

OSX: Avoid userprompt when opening embedded binary

I have a sandboxed app that uses an embedded binary to show it's status item.
On first launch of the main app (where it launches the embedded binary like this:
NSWorkspace.sharedWorkspace().launchApplication(statusItemPath)
) OSX displays a user prompt, if the user really wants to start the embedded app:
I find this really confusing for the user - I understand that it is for security reasons but I want to distribute via MAS and so both binaries needs to pass review.
Is there a way to avoid this user prompt (maybe a singing option or entitlement key?)
When an application is downloaded from the internet, or run via another program for the first time, OS X is protecting the user with a mechanism known as 'quarantine'.
Once the user accepts running the application, the quarantine extended attribute on the app is removed.
Removing the quarantine attribute can be done with the following command:
xattr -d com.apple.quarantine /PATH/TO/APPLICATION
So you could call out to the system to run this from your initial application on the embedded binary. However I'm not sure this would be acceptable to Apple for the App Store.
The preferred method would be to use XPC and create a helper app which is launched automatically by launchd. You can read about that here.

How can I know how my app was launched on Mac OS?

I've got a problem, and have been looking for a solution for a long time.
I made a application which has an option which will launch the app automatically when the system starts using Launch Services.
If application is launched by the user (ie. opening the .app) I want to do something.
If application is launch by Launch Services when the system starts I want to do another thing.
So I need to know how the app was launched: by the System (Launch Services loading the app when the computer turns on) or User (User opening the .app in the Finder).
How can I get this information?
Any suggestions are weclome.
prethanks.

Resources