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.