NSTask Tutorial for OS X

Hi

I dont know

The rules for a sandboxed app are listed here

So anything that requires sudo is right out. Iā€™ll have a poke around and if it isnt possible then ill edit the article.

The evidence seems to point to some degree of interaction being possible.

Cheers
Warren

Thanks! Looking forward to reading about your findings. I spent most of last week trying to do just that without any luck so far :confused:
The thing that bothers me most is the presence in the MAS of apps that implement shutdown option.

Ok, got that all wrong. The MAS developers probably donā€™t use NSTask to do their bidding, they use instead the scripting bridge to send Apple Events to System Events. I tested that and it works alright inside a sandbox. You only need to use that temporary entitlement thing to get things done.

Hi,

I love the tutorial, and it is great you rewrote a version in Swift, but is there are reason the original Obj-C example is not available? I have projects where I do not want to mix idioms, no matter what Apple says, and re-transliterating back to Obj-C seems like a wasteful and potentially buggy process. :slight_smile:

Why not keep a copy of Andy Pereiraā€™s code too? I know people want to evangelize and ā€˜move with the timesā€™ , and I even like Swift, but really this is counter productive. If you Swiftify, great, but donā€™t delete the old code at the same time, diversity is good!

I wonder it the way back machine will work on this oneā€¦
Edit:
And it does, so all good, I have the Obj C version, cheers

I enjoyed the tutorial. Helped me create an app that uses a expect script to SSH to a wireless LAN controller and get the configuration. However; Iā€™m having an issue with the output hanging the app. I assume that the Pipe() object is buffering the data being ported over to the TextView. My captureStandardOutputAndRouteToTextView method is verbatim to your code. Any thoughts on how to handle large amounts of data being piped from stdout to the text view?

Thanks for this awesome post.

Just as a sidenote regarding SUDO. I was building an app (for internal use in my workplace) and I really really needed to run a couple of commands with SUDO privileges.

What I found out is that I could run them as an AppleScript which in turn calls the shell script with sudo commands. It works, and the AppleScript call (using executeAndReturnError) takes care of the authorization process.

Now, the downside with this is that the execution of this AppleScript takes place on a different thread and I cannot stop it with the Stop button since I donā€™t have a reference to it. Am I right?

Anyhow, this helped a lot!

For anyone asking about using NSTask to run other command line operations, I followed this tutorial and then adapted the code to run arbitrary PHP code with very minimal changes.

  1. Change launchPath to your PHP binaryā€™s path (e.g. let path:String = ā€œ/usr/bin/phpā€). This can be any binary that accepts command line arguments.
  2. Add flags to arguments array:
    arguments.append(ā€œ-rā€)
  3. Add values to arguments array:
    arguments.append(inputText.string!)

I put the Xcode project for a super basic but functional macOS app that will run arbitrary PHP from a text view input or bundled example.php file on github, adapting the code from this tutorial here: GitHub - tonyjohnston/swift-phpRunner: Run PHP scripts in a native macOS app

Are there any plans to update this valuable tutorial now that the build script no longer works in the absence of PackageApplication in xcode? I have tried to work around the problem with xcodebuild, but I am not sure I have been successful because I eventually get hung with the following error message:

Check dependencies
No profiles for ā€˜com.razeware.SuperDuperAppā€™ were found: Xcode couldnā€™t find a provisioning profile matching ā€˜com.razeware.SuperDuperAppā€™.
Code signing is required for product type ā€˜Applicationā€™ in SDK ā€˜iOS 10.3ā€™

It appears that SuperDuperCode needs a teamID, but the options None and MyName(Personal Team) donā€™t work with com.razeware.SuperDuperApp. Is this also a problem introduced by the newer version of xcode?

Hi

The signing issue has always been there, youā€™ll need to replace the bundle ID with your own one and set your own team for this to work.

PackageApplication has been deprecated but you can find a good explanation of what to set the switches to here. Using -exportArchive instead of Package Application to export an IPA Ā· Subathra Thanabalan

The article will get updated in the future as we do like to keep our tutorials up to date.

Thanks
Warren

Thank you for replying. Unfortunately, I am a newbie and the signing problem remains, even though I have changed bundle identifier and specified my personal team, both for TasksProject and SuperDuperApp. TasksProject seems perfectly happy. The problem seems to lie with SuperDuperApp. As downloaded, SuperDuperApp shows an iPhone 7 Plus as a target device. Xcode then wants a signing certificate for ā€œiOS Developer.ā€ But the tutorial is a purely macOS project and Xcode canā€™t find a match for a provisioning profile because I am identified (rather loosely, I admit) as a macOS Developer. I tried to work around that problem, but never found a solution that worked. If I tried to redefine SuperDuperApp as a macOS/Cocoa app, I could get the pre-compile warning about the lack of signing certificate to go away, only at the expense of generating a ā€œAssets.xcassets does not existā€ error (???). I havenā€™t found a solution, but I pass this along for anyone who might stumble on this thread.

Donā€™t worry , we all started somewhere.

You donā€™t need to sign Tasks Project but you need to sign SuperDuperApp as a iOS developer with your own bundle ID.
You can create a iOS developer account on the Apple portal if you donā€™t have one already.
SuperDuperApp is nothing special. Its just the template Xcode iOS single view app with a name and bundleID supplied.

The tutorial is really meant to cover how to use NSTask and connect Pipes so by getting to this stage you have probably picked up the essentials. Maybe you could try something else in the script and UI thats more specific to mac activity. If you know any scripting languages you could think something up.

This tutorial is more than six months old so questions are no longer supported at the moment for it. We will update it as soon as possible. Thank you! :]