0

So I thought I'd have a bit of fun writing a small app in Swift to batch convert .py files to .mpy, using mpy-cross. mpy-cross works fine from the command line - it takes a .py file and outputs a new .mpy file in the same location - but I get an error when I call it using Process in Swift. Here's the code -

    func convert(filePath: String) -> Bool {
        
        let task = Process()
        let pipeOut = Pipe()
        let command = "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/mpy_cross/mpy-cross " + filePath
        
        task.standardInput = nil
        task.standardOutput = pipeOut
        task.standardError = pipeOut
        task.arguments = ["-c", command]
        task.executableURL = URL(fileURLWithPath: "/bin/zsh")
        
        do {
            try task.run()
            
            let data = pipeOut.fileHandleForReading.readDataToEndOfFile()
            let output = String(data: data, encoding: .utf8)!
            
            print(output)
            
        } catch {
            return false
        }
        
        return true
    }

The error I get is OSError: (1, "can't open {path to myfile.mpy}") and the .mpy file isn't created, but the task seems to have run ok. Any suggestions much appreciated!

1
  • 1
    Is your app in a sandbox? Which project template did you use to create the project? Commented Mar 18 at 7:03

1 Answer 1

0

Turns out it was really simple - just disable the App Sandbox in the .entitlements - you live & learn

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

Comments

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.