1

Currently I'm working on the gphoto2 library in Cocoa App. So basally I was trying to access the library through my app. The gphoto2 works on commands So I use Process class to run my terminal commands through my app. So I have the script file which contains the script to access the gphoto2.

Here is my code:

func shellScript ()
    {
        guard let path1 = Bundle.main.path(forResource: "Script",ofType:"sh") else {
            print("Unable to locate Script.sh")
            return
        }
        let path =  "/bin/zsh" //"file:///usr/local/Cellar/gphoto2/2.5.23/bin/gphoto2"
        let arguments = [""]
        let process = Process()
        process.arguments = arguments
        process.executableURL = URL(fileURLWithPath: path)
        let connection = Pipe()
        process.standardOutput = connection
        do {
            let task =  try Process.run(URL(fileURLWithPath: path1), arguments: []) { (process) in
                let data = connection.fileHandleForReading.readDataToEndOfFile()
                if let string = String(data: data, encoding: String.Encoding.utf8) {
                    print(string)
                }
            }
            task.waitUntilExit()
        }catch {
            print(error.localizedDescription)
        }
    }

And here is my script file.

#!/bin/zsh

#  Script.sh
#  CommandsDemo
#
#  Created by Ravindra on 22/05/20.
#  Copyright © 2020 Ravindra. All rights reserved.
#name="Pinto"

echo "*********************************"
echo "Build Started"
echo "*********************************"

echo "*********************************"
echo "Beginning Build Process"
echo "*********************************"
#script typescript bash -c 'ls'
script commandOutputFile gphoto2

Here is the output of that.

*********************************
Build Started
*********************************
*********************************
Beginning Build Process
*********************************
Script started, output file is commandOutputFile
script: gphoto2: No such file or directory

Script done, output file is commandOutputFile

Actually, my system commands (echo, ls, etc.) are working fine. But it comes to like non-system (gphoto2, npm, etc.) commands then it's not able to find out file or directory. I'm specifying the problem that I need to communicate DSLR (for the library gphoto2 I'm using) to my macOS app.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • Where is your `PATH` set? Read this answer https://stackoverflow.com/a/10583324/581190 and consult `man zshall` for more info, where you should place it and what is the difference between shells (login, ...). – zrzka Jun 18 '20 at 13:18
  • I already added the path in zprofile. **Export PATH="/usr/local/bin:/usr/local/Cellar/gphoto2/2.5.23/bin"**. This is path I have set in **zprofile** and **zshenv** and I also try with following path **Export PATH="/usr/local/bin:/usr/local/Cellar/gphoto2/2.5.23/bin:$PATH"** . Please let me that I'm doning right or wrong way to setting my path. – Ravindra Kumar Sonkar Jun 19 '20 at 05:05
  • I found one that helped me. https://stackoverflow.com/questions/49544566/operation-not-permitted-when-executing-killall-with-swift . – Ravindra Kumar Sonkar Jun 22 '20 at 04:29

0 Answers0