last day (15 days later) » 

9:14 PM
1
A: Swift mac app, run terminal command without knowing the path (so it looks in every path in $PATH)?

Martin RYou can execute the command via env: env utility argument ... Example: let path = "/usr/bin/env" let arguments = ["ls", "-l", "/"] let task = Process.launchedProcess(launchPath: path, arguments: arguments) task.waitUntilExit() env locates the given utility using the $PATH variable and then ...

 
hmm, does not seem to work for cowsay & fortune :s, output : env: cowsay: No such file or directory
 
@Steven.B: What is "cowsay" and where is it located?
 
It seems to be in /usr/local/bin/cowsay , this is my output if I echo my $PATH => /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin. the first path should contain the binary :s
 
@Steven.B: Does env cowsay work on the command line? What is the output of type cowsay and file /usr/local/bin/cowsay ?
 
"env cowsay" seems to work, output for "type cowsay" is "cowsay is hashed (/usr/local/bin/cowsay) " when i run file /usr/local/bin/cowsay" I get "/usr/local/bin/cowsay: a /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/b‌​in/ script text executable, ASCII text"
I have to add this was installed via brew package manager but i need the app to work with every command that works in the terminal, Big thanks already :)
 
9:14 PM
@Steven.B: The problem is probably that the Xcode application uses a different PATH variable and not the one from your shell. Try to execute the compiled binary on the command line and not within Xcode.
 
wow, running the .app with "open -a /xxxxxx/appname.app " seems to put show the expected output, however doubleclicking the .app does not seem to work :(
 
So the problem is that programs started from the Finder do not have /usr/local/bin in their path.
 
hmm
 
You could add /usr/local/bin to the PATH in the environment of the launched process. See stackoverflow.com/a/28971510/1187415 for a similar issue.
 
9:35 PM
That works! also running by doubleclicking the .app. you are my hero :)
It also works running .sh scripts thanks!
 
9:51 PM
OK, glad to help. I have added the relevant information to the answer.
 
so to make everything work, i should loop trough every path in the terminals $PATH variable, and add it to the current process variable, but how do i access the terminals path :s
 
The problem is that the Finder does not use or know of .profile, .bashrc, etc where the path is usually customized. I do not have a solution for that.
 

last day (15 days later) »