Re: Installing a Launchd.plist


Sandor Szatmari
 

Sounds like this would be a good approach for me.  Sounds like I could build a helper app, make it available in my main app's bundle, and call it with arguments to install/uninstall and load/unload the Launchd script.  

I'm assuming when you run the AppleScript to launch the privileged helper you are prompted for an administrative password...

Sandor

On Jul 23, 2017, at 02:51, John Brownie <john_brownie@...> wrote:

Thanks, that's very helpful. I will try it out when I get the time (my day job is getting in the way of my programming these days).

John

Jack Brindle wrote:
I suspect you are putting more into the scripting idea than is really needed. The only thing I use AppleScript for is to launch the installer application. That (standard Mac) application is written in Objective-C/Cocoa (OK, someday I’ll move to Swift). It _does_ take arguments on the command line. Those are picked up with the NSProcessInfo arguments method, which returns an array, just like you would get with main(arg, arg), except the strings are NSStrings. You can directly use those in the installer application after validating them. Everything you need to do, such as moving files, setting their privilege level and so on can be done with Cocoa calls here. Let me emphasize this a bit more - there really is nothing special to this app other than the fact that it runs at an elevated privilege level.

The magic lines that make this work run in the top-level program. They are also incredibly simple, just create the arguments you want to pass, then craft the AppleScript string, set up the environment to run the AppleScript, and do it.

// At this point the argument string is already set up. It can contain something like —from /Wherever/the/file/comes/from…
// likewise the path argument is a pointer to the main installer. It usually is placed inside the top level app’s bundle to make packaging much easier. You would get it from NSBundle calls...
NSString *script = [NSString stringWithFormat: @"do shell script quoted form of \"%@\" & \" %@\" with administrator privileges", path, arguments];

NSDictionary *errorDict = [NSDictionary dictionary];
NSAppleScript *appleScript = [[NSAppleScript alloc] initWithSource:script];
[appleScript executeAndReturnError:&errorDict];

That is the entire applescript usage. As I said, amazingly simple.

--
John Brownie
SIL-PNG, Ukarumpa, Eastern Highlands, Papua New Guinea
Mussau-Emira language, New Ireland Province, Papua New Guinea

Join cocoa@apple-dev.groups.io to automatically receive all group messages.