How to access "Standard Output" when running macOS executable? - swift

Problem:
I'm trying to hook up the standard input/standard output from a unix executable file to the user interface in a MacOS application. But, I can't seem to access the values - nothing shows up.
Background:
I've implemented every solution that I could find, but none of them have worked [1][2][3][4][5][6][7][8][9]. I've completed a Python 3 course [1], so that I could customize the standard output in the python script of the executable file [1]. And, I've reviewed and implemented several working MacOS repositories that update their user interface with data from standard output [1][2][3].
Code: Full
func runExecutable() {
let desktop = fileManager.urls(for: .desktopDirectory, in: .userDomainMask)[0]
let url = Bundle.main.url(forResource: "youtube_dl_custom", withExtension: "")
let arguments = [
"--format",
"bestvideo[ext=mp4]+bestaudio[ext=m4a]",
"--output",
"\(desktop)/%(title)s.%(ext)s",
"\(videoUrlTextField.stringValue)"
]
process.arguments = arguments
process.executableURL = url
process.standardInput = inputPipe
process.standardOutput = outputPipe
openConsolePipe()
inputPipe.fileHandleForReading.readabilityHandler = {
[weak self] fileHandle in
let data = fileHandle.availableData
self?.buffer.append(data)
if let buffer = self?.buffer,
let string = String(data: buffer, encoding: .utf8),
string.last?.isNewline == true {
self?.buffer.removeAll()
print("## \(string)")
self?.standardOutputTextView.string += string + "\n"
self?.outputPipe.fileHandleForWriting.write(data)
}
}
try? process.run()
closeConsolePipe()
}
func openConsolePipe() {
dup2(STDOUT_FILENO, outputPipe.fileHandleForWriting.fileDescriptor)
dup2(inputPipe.fileHandleForWriting.fileDescriptor, STDOUT_FILENO)
dup2(inputPipe.fileHandleForWriting.fileDescriptor, STDERR_FILENO)
}
func closeConsolePipe() {
freopen("/dev/stdout", "a", stdout)
}
Results:
The standard output appears to automatically print to the console, but I can't seem to access the values.
Misc:
Used the youtube-dlrepository to download videos [1].
Used a custom python script for youtube-dl [1].
Converted youtube-dl to an executable file using pyinstaller [1].
Posted the project to GitHub for troubleshooting [1].

Related

Swift on MacOS: cannot open a file from my app

I'm writing an app that displays a list of PDF files in a NSTableView that the user should be able to double-click to open in the default application (Preview, Adobe Reader, ...).
I've tried using NSWorkspace.shared.openFile and NSWorkspace.shared.open(_: withAppBundleIdentifier: options: additionalEventParamDescriptor: launchIdentifiers:), but none of them work.
I can't use the new func open(URL, configuration: NSWorkspace.OpenConfiguration, completionHandler: ((NSRunningApplication?, Error?) -> Void)?) as this is targeted at pre-Catalina computers.
Here are the two code snippets:
1.
#objc func doubleClickOnResultRow() {
let clickedRow = resultsTableView.selectedRow
if ( clickedRow > -1 ) {
let myURL = foundItems[clickedRow] as URL
if (!NSWorkspace.shared.openFile(myURL.path)) {
print("Unable to open : ", myURL.path)
}
}
}
This first one does nothing, and I get a "Unable to open : url/to/my/file.pdf" in the console.
2.
#objc func doubleClickOnResultRow() {
let clickedRow = resultsTableView.selectedRow
if ( clickedRow > -1 ) {
let myURL = foundItems[clickedRow] as URL
NSWorkspace.shared.open([myURL], withAppBundleIdentifier: "com.apple.Preview", options: NSWorkspace.LaunchOptions.withErrorPresentation, additionalEventParamDescriptor: nil, launchIdentifiers: nil)
}
}
With this one, however, when I double-click on a file, I get a error window with the Finder icon that says :
"The application “My app” does not have permission to open “myfile.pdf.” Here is the screenshot:
Finder error
I don't understand what I'm doing wrong. Eventually I could build a lightweight PDF viewer inside my app, but I would really like to avoid it if possible.
EDIT 03/31/2020, 16:10
I've tried a third way, by calling the shell command "open" with this (found here):
func shell(_ command: String) -> String {
let task = Process()
task.launchPath = "/bin/bash"
task.arguments = ["-c", command]
let pipe = Pipe()
task.standardOutput = pipe
task.launch()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output: String = NSString(data: data, encoding: String.Encoding.utf8.rawValue)! as String
return output
}
and then
let command = "open \""+myURL.path+"\""
print(shell(command))
and I get
LSOpenURLsWithRole() failed with error -54 for the file path/to/my/file.pdf.
You have to construct the URL to file using init(fileURLWithPath:) of URL not the usual init?(string:)
let url = URL(fileURLWithPath: "/path/to/file.pdf")
Then you can open it with default application using NSWorkspace.open instance method
NSWorkspace.shared.open(url)

NSUserUnixTask ignores script quietly

I'm writing a sandboxed app in Swift 4 on macOS High Sierra using Xcode 9. My app has a shell script file (say, myscript) copied to the Bundle.main.resourceURL directory when installing. I want to run this shell script using NSUserUnixTask as follows:
if let scriptUrl = URL(string: "myscript", relativeTo: Bundle.main.resourceURL) {
do {
let task = try NSUserUnixTask(url: scriptUrl)
task.execute(withArguments: [ "hello" ])
debugPrint("OK")
} catch let error {
debugPrint(error)
}
} else {
debugPrint("Script not found")
}
// output: "OK"
This gives no error messages and "OK" is correctly displayed. But the myscript script seems to be completely ignored. When I run myscript from Terminal instead, it runs as intended.
The test myscript file looks like:
#!/bin/sh
/usr/bin/osascript -e "tell app \"System Events\" to display dialog \"Message: $1\""
(The is only a test script; the real one calls emacsclient.) The shell script file is set executable (permission 755). Not only this shell script but also any other shell script seems ignored. For example, if the second line is replaced with /usr/bin/printf "\a", it does not beep. I tried many other shell scripts but nothing seems to work.
How can I solve this issue?
EDIT:
I was hasty. The class document says "If the application is sandboxed, then the script must be in the applicationScriptsDirectory folder." Now I want to know how to copy the script file upon installation.
EDIT 2:
I manually copied myscript to ~/Library/Applications Scripts/com.mydomain.myApp/ and then changed the first line of the above Swift codes (if let scriptUrl ...) to
let scriptFolderUrl = try! FileManager.default.url(for: .applicationScriptsDirectory,
in: .userDomainMask, appropriateFor: nil, create: true)
if let scriptUrl = URL(string: "myscript", relativeTo: scriptFolderURL) {
https://tutel.me/c/programming/questions/43741325/swift+3++sandbox++how+to+create+applicationscriptsdirectory+if+it+doesn39t+exist
The script runs. Now I need to figure out how to copy the script to the scripts folder when installing or running the app the first time.
pkamp requested a proper answer. :)
As the class document says, the script must be in the applicationScriptsDirectory folder. This folder is not writable by a sandboxed application, as vadian pointed out. The script (myscript) must be copied manually to the applicationScriptDirectory folder (e.g., ~/Library/Applications Scripts/com.mydomain.myApp/).
After this done, change the first line to the following:
// --------------------------------------------------------
let scriptFolderUrl = try! FileManager.default.url(for: .applicationScriptsDirectory,
in: .userDomainMask, appropriateFor: nil, create: true)
if let scriptUrl = URL(string: "myscript", relativeTo: scriptFolderURL) {
// --------------------------------------------------------
// same below
do {
let task = try NSUserUnixTask(url: scriptUrl)
task.execute(withArguments: [ "hello" ])
debugPrint("OK")
} catch let error {
debugPrint(error)
}
} else {
debugPrint("Script not found")
}
I benefited from this link for the first line.

How to create plugins in swift

I found article describing how to create plugin using Swift and Cocoa. It uses NSBundle to load plugin, but that, as far as I know, is not available in pure swift (no Cocoa). Is there way how to achieve same result without using Cocoa?
More info:
In case it's relevant, here is what I want to achieve. I create app in swift that runs on linux server. User can connect to it using their browser. I want to be able to have other people write "plugins" that will implement functionality itself (what user can see and do once they connect), from printing out hello world, through chat programs to games without having to worry about low level stuff provided by my app. Some sort of dll, that my server application loads and runs.
Solution to this is not trivial, but it's not impossible to do either. I prefer to use swift package manager to manage dependencies and Xcode as IDE. This combination is not perfect as it needs a lot of tinkering but there is not any other useable free swift IDE as of now.
You will need to set up two projects, let's call them Plugin (3rd party library) and PluginConsumer (app that uses other people plugins). You will also need to decide on API, for now we will use simple
TestPluginFunc()
Create Plugin.swift file with TestPluginFunc implementation in your Plugin project:
public func TestPluginFunc() {
print("Hooray!")
}
Set the project to build framework, not executable and build[1]. You will get Plugin.framework file which contains your plugin.
Now switch to your PluginConsumer project
Copy Plugin.framework from your Plugin project somewhere where you can easily find it. To actually load the framework and use it:
// we need to define how our plugin function looks like
typealias TestPluginFunc = #convention(c) ()->()
// and what is its name
let pluginFuncName = "TestPluginFunc"
func loadPlugin() {
let pluginName = "Plugin"
let openRes = dlopen("./\(pluginName).framework/\(pluginName)", RTLD_NOW|RTLD_LOCAL)
if openRes != nil {
// this is fragile
let symbolName = "_TF\(pluginName.utf8.count)\(pluginName)\(initFuncName.utf8.count)\(initFuncName)FT_T_"
let sym = dlsym(openRes, symbolName)
if sym != nil {
// here we load func from framework based on the name we constructed in "symbolName" variable
let f: TestPluginFunc = unsafeBitCast(sym, to: TestPluginFunc.self)
// and now all we need to do is execute our plugin function
f()
} else {
print("Error loading \(realPath). Symbol \(symbolName) not found.")
dlclose(openRes)
}
} else {
print("error opening lib")
}
}
If done correctly, you should see "Hooray!" being printed to your log.
There is a lot of room for improvement, first thing you should do is replace Plugin.framework string with parameter, preferably using some file library (I am using PerfectLib). Another thing to look at is defining plugin API in your PluginConsumer project as a protocol or base class, creating framework out of that, importing that framework in your plugin project and basing your implementation on that protocol/base class. I am trying to figure out exactly how to do that. I will update this post if I mange to do it properly.
[1]: I usually do this by creating Package.swift file and creating xcode project out of it using swift package generate-xcodeproj. If your project doesn't contain main.swift, xcode will create framework instead of executable
What you will want to do is create a folder your program will look in. Let's say it's called 'plugins'. It should make a list of names from the files in there, and then iterate through using them, passing parameters to the files and getting the output and making use of that in some way.
Activating a program and getting output:
func runCommand(cmd : String, args : String...) -> (output: [String], error: [String], exitCode: Int32) {
var output : [String] = []
var error : [String] = []
let task = Process()
task.launchPath = cmd
task.arguments = args
let outpipe = Pipe()
task.standardOutput = outpipe
let errpipe = Pipe()
task.standardError = errpipe
task.launch()
let outdata = outpipe.fileHandleForReading.readDataToEndOfFile()
if var string = String(data: outdata, encoding: .utf8) {
string = string.trimmingCharacters(in: .newlines)
output = string.components(separatedBy: "\n")
}
let errdata = errpipe.fileHandleForReading.readDataToEndOfFile()
if var string = String(data: errdata, encoding: .utf8) {
string = string.trimmingCharacters(in: .newlines)
error = string.components(separatedBy: "\n")
}
task.waitUntilExit()
let status = task.terminationStatus
return (output, error, status)
}
`
Here is how a swift plugin would accept arguments:
for i in 1..C_ARGC {
let index = Int(i);
let arg = String.fromCString(C_ARGV[index])
switch arg {
case 1:
println("1");
case 2:
println("2")
default:
println("3)
}
}
So once you have the program and plugin communicating you just have to add handling in your program based on the output so the plugins output can do something meaningful. Without cocoa libraries this seems the way to go, though if you use C there are a couple of other options available there as well. Hope this helps.

How to do a "say" in Swift 3 inside command-line program?

What should I do in this case with the following code?
func convertToM4A(filename: String, voice: String) -> Bool {
let full_string = speaking_queue?.joined(separator: " ")
let command_string: [String] = [/"-v \"\(voice)\"",*/ "--progress", "--output-file=\"\(filename)\"","-i", " \"\(full_string!)\""]
print(command_string)
/
let DocumentsDirectory = FileManager().homeDirectory(forUser: "shyamalchandra")
print((DocumentsDirectory?.absoluteString)!)
*/
let task = Process()
task.launchPath = "/usr/bin/say"
task.arguments = command_string
let pipe = Pipe()
task.standardOutput = pipe
task.launch()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output: String? = String(data: data, encoding: String.Encoding.utf8)
task.waitUntilExit()
if let output = output {
if !output.isEmpty {
print(output.trimmingCharacters(in: .whitespacesAndNewlines))
}
}
return true
}
At run-time, it complains about the TERM environment being not set and furthermore, doesn't write the file to disk. What to do?
The main error is how you build the argument array. The given arguments
are passed directly to the process. Process does not use the shell to
interpret the arguments, therefore you must not enclose them in quotation
marks.
Another problem is that the "-i" (interactive) option cannot be used
when writing to a file.
So your code should look like this:
func convertToM4A(filename: String, voice: String) -> Bool {
let fullString = "Hello world"
let task = Process()
task.launchPath = "/usr/bin/say"
task.arguments = [ "-v", voice, "-o", filename, fullString]
task.launch()
task.waitUntilExit()
return true
}
The "--progress" option causes a progress meter to be displayed on
standard error. If you want to display that then you would have to
read asynchronously from standard error.
If you're writing a native Mac app and want to record synthesized speech to an audio file, don't go trying to wrap a shell command — there's native API
for that. NSSpeechSynthesizer is the macOS API for text-to-speech in general, and it has a method startSpeaking(_:to:) that records output to an audio file.
This API outputs to an AIFF file, but there are numerous APIs you can use to convert/encode that to M4A: AVAssetReader/AVAssetWriter, AVAudioFile, lower-level CoreAudio C APIs, etc.
(Generally, if you're writing a native Mac program and there's something you want to do, check to see if there's an API for it before you go trying to wrap a shell command. Usually those shell commands are using the same API, so you're just punishing yourself with all the indirection, I/O parsing, etc.)
Yes, NSSpeechSynthesizer is an AppKit API, but you can use it in a command line tool.
Take a look at this lib, I've used it before and it is very capable of running shell script. With that then you can use the "say" command and send in some arguments. https://github.com/kareman/SwiftShell
You could try it this way for instance
import SwiftShell
try runAndPrint("say", "Hello world", 4, "arguments")
let array = ["Hello world", "we", "are"]
try runAndPrint("say", array, array.count + 2, "arguments")

Getting success result on shell script executing installer pkg within an NSTask Swift

I am building an installer for an OS X application in Swift 2.0. The installer does little more than take user input, sends it to a server, and builds a desktop shortcut determined by server response. This shortcut (the 'application') opens up another application (FileMaker). As part of the installation process I am installing FileMaker silently using NSTask which runs a shell script executing installer.app on the FileMaker .pkg. This works fine.
I need to determine whether this installation is successful or not in order to progress to the next step in the installer. I can easily get the string response from the terminal, ie "installer: The install was successful," but I dont feel a hard coded string condition is robust enough. Any other possibilities?
n.b. I'm a beginner Swift developer (1 week!) and have only a year of Web Development behind that. ie. I'm blindingly green.
P.S. Ideally I'd be displaying a progress indicator for the FileMaker installation rather than a message, but that'd be overextending myself (further) if it's even possible.
func installFileMaker() {
let fileMakerFileName = "FileMaker Pro 14"
let fileMakerDirectory = "Resources/FileMaker14_MacClient"
// get resource path
let bundle = NSBundle.mainBundle()
let resourcePathResult = bundle.pathForResource(fileMakerFileName, ofType: "pkg", inDirectory: fileMakerDirectory)
if let resourcePath = resourcePathResult {
displayWebViewMessage("Installing Briefcase Now...")
let command = "installer -pkg \"" + resourcePath + "\" -target /"
Utilities.runAsCommandInBackground(command, callback: installUpdateWebviewCallback)
} else {
// error
displayWebViewMessage("Installation error")
}
print("rrr")
}
Runs shell command
static func runAsCommandInBackground(command: String, callback: (((success:Bool, message:String)) -> Void)?) {
let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
dispatch_async(dispatch_get_global_queue(priority, 0)) {
let pipe = NSPipe()
let task = NSTask()
task.launchPath = "/bin/sh"
task.arguments = ["-c", String(format:"%#", command)]
task.standardOutput = pipe
let file = pipe.fileHandleForReading
task.launch()
var result = ""
if let tmpResult = NSString(data: file.readDataToEndOfFile(), encoding: NSUTF8StringEncoding) {
result = tmpResult as String
} else {
// error
}
dispatch_async(dispatch_get_main_queue()) {
print(result)
// Give me a more robust result!!
if let unwrappedCallback = callback {
unwrappedCallback((true, result as String))
}
}
}
}