3

Im attempting to build a swift OSX app that can run some shell commands to mount the users requested VPNs. I tried using NSAppleScript but got an error every time I tried to run with Admin privileges so I decided to use NSTask to call an apple script that will run as admin and mount the VPNs.

In order to debug getting the shell commands working in swift through NSTask I made a test script file that just has a simple whoami command that works when I run it in the script editor and in the terminal using osascript /Users/amaloney/Desktop/test.scpt

However in my Swift file

import Foundation

let task = NSTask();
task.launchPath = "/usr/bin/osascript"
task.arguments = ["/Users/amaloney/Desktop/test.scpt"]

task.launch()

I continue to get the following error

script error -54. Couldn't get error text because of error -1700.

Any thoughts on how I can get this working?

Thanks!

For reference - test.scpt

do shell script "whoami"

5
  • Are you waiting for the command to finish with task.waitUntilExit()? Commented Jul 15, 2015 at 20:22
  • @Kametrixom I've tried running with that as well, same error Commented Jul 15, 2015 at 20:28
  • lists.apple.com/archives/applescript-users/2001/Mar/… Commented Jul 15, 2015 at 20:31
  • @ThomasKilian Ive read thorough that already, that user is getting error -2741 which I am not. Commented Jul 15, 2015 at 20:43
  • Have you tried using bash or Perl instead of Applescript? Commented Jul 15, 2015 at 21:03

2 Answers 2

1

I was running this in an Xcode Playground, which apparently was the source of the problems. Once I went ahead and integrated this code into my OSX app it worked fine.

Sign up to request clarification or add additional context in comments.

Comments

0

The issues appear to be related to the AppleScript portions of your code:

  1. script error -54

    ↳ -54 File permission error

  2. Couldn't get error text because of error -1700.

    ↳ Can’t make ... into type item.

It's likely your script tries to perform an operation that it doesn't have permission to do, and also tries to assign the wrong type — Without seeing the script or knowing what it does it's unknown.

4 Comments

I suspected this as well but I cant seem to find any problems with my script -- do shell script "whoami"
If you want to use AppleScript with NSTask then the command needs to be absolute: do shell script "/usr/bin/whoami"... The whoami utility is obsolete anyway, so you could use /usr/bin/id with NSTask instead, then AppleScript is not needed.
My end goal is not to use whoami, that was just something simple for a test. My plan is to execute some jamf commands from within my swift app
Most NSTasks can work similarly; hopefully this has given you a better understanding of the prcoess.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.