Re: Installing a Launchd.plist

Jack Brindle


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.

- Jack

On Jul 22, 2017, at 10:16 PM, John Brownie <john_brownie@...> wrote:

OK, that is useful. What I'm trying to implement is a system to move files around into correct folders without troubling the user about finding the correct places in the file system. So the AppleScript part is going to look something like

set sourceFile to "/path/to/source"
set destinationFolder to "/path/to/destination"
set theScript to "mv \"" & sourceFile & "\" \"" & destinationFolder & "\""
do shell script theScript with administrator privileges

So I'll need to pass the file and destination paths to the app.

But building the lower-level app is new territory for me. How does one create an app that calls such a script? I've only ever done it from Script Editor...


Jack Brindle wrote:
That’s one of the cool things about it. Nothing special about the lower level (main) app. It is simply a standard app. You can either do the installation yourself there, have the top level installer run the command line installer as the privileged app, or a combination where the lower level app calls the command line installer. Remember, once the applescript command is executed, the application it runs is running at elevated level (root), so you can do a lot of both good and damage.

We used this method to replace the standard installer user interface with one of our own, and invoke the command-line interface to do the actual installation job. In another product, we do the actual file copies, privilege setting and other installation work in the lower-level app itself. There are some gotchas there, like handling file download quarantines, but with some effort they can be handled. Both apps have a lot to install, so there is a lot of work happening here. There is a big advantage to being root in this situation...

For the top-level installer app, we use a user-agent (so as not to show icons, menus, etc). It does some checking, then calls the lower-level (main) installer using the applescript. That is not too difficult to set up. I wonder if it would be easier to just use an Applescript app to do that job. The top-level app usually does not have any UI - the exception is if something goes wrong, we tell the user.

If you need some help with the Applescript setup, just ask and I’ll dig up that info!

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

Join { to automatically receive all group messages.