0

I tested out some code in a playground and it works as I would expect.

I built an extremely basic (one function, one button, one textfield) project to test the code in and it doesn't work – in fact it hangs up (beach balling).

What might cause this to happen? Both the playground and the project import Cocoa and Foundation.

The code is below.

It appears to get hung up on this line:

let data = pipe.fileHandleForReading.readDataToEndOfFile()

Here's the code as it is written in the playground (and copied into the project):

import Cocoa
import Foundation




// *** Getting exiftool version number

func exiftoolVersion() -> String {

    let task = Process()
    let pipe = Pipe()

    task.standardOutput = pipe
    task.arguments = ["-ver"]
    task.executableURL = URL(fileURLWithPath: "/usr/local/bin/exiftool")
    do {
        try task.run()
        task.waitUntilExit()
    }
    catch {

    }

    let data = pipe.fileHandleForReading.readDataToEndOfFile()
    var output = String(data: data, encoding: .utf8)!

    output = output.filter { !$0.isWhitespace }
    return output
}
psb
  • 89
  • 5
  • Running an external tool via Pipe is going to require that the application have appropriate privileges. Without more error detail or stack trace it's hard to say, but you likely didn't encounter an error in Sandbox because Xcode itself has fairly permissive system privileges. I suggest reading up on Sandbox and elevated-privileges topics. Also I assume you are on Mac OS, you might tag or update your post to confirm. Here's a potentially related post with a lot of replies (Obj-C as well as Swift): https://stackoverflow.com/questions/412562/execute-a-terminal-command-from-a-cocoa-app – Corbell Jun 17 '20 at 00:12

1 Answers1

0

The one way that I know would solve your issue is to go to your .entitlements file and switch turn off App Sandbox, by setting the value to false, as shown below.

    <key>com.apple.security.app-sandbox</key>
    <false/>

This is because in sandboxed mode you aren't allowed to execute an external program.

Edit: Since it didn't work for you, can you try replacing catch {} with

        catch {
            print(error)
        }

and see what/if there's any error?

vincent
  • 227
  • 2
  • 13
  • Still no success. – psb Jun 17 '20 at 13:31
  • @psb interesting - it worked for me. How about try that edit that I've stated, and see if any errors are thrown? that should make us know better of what went wrong. – vincent Jun 17 '20 at 13:36
  • Code=13 "Permission denied" So it's a permissions issue with running exiftool. That's strange to me, as I have no issues running exiftool via the terminal or when running it from within a shell in program written in xojo. – psb Jun 17 '20 at 13:50