如何从我的Mac应用程序中访问终端的$ PATH变量,它似乎使用了不同的$ PATH [英] How to access the Terminal's $PATH variable from within my mac app, it seems to uses a different $PATH
问题描述
我正在尝试制作一个 mac 应用程序,用户可以在其中输入命令,就像他们在 mac 终端中一样,我得到了大部分工作,但是我发现应用程序的 $PATH 变量是从查找程序运行的,或者xcode与终端使用的$ PATH变量不同.
I'm trying to make a mac app where the user can type in commands just like they would in the mac terminal, I got most of it working however I found out that the $PATH variable of apps run from the finder or xcode is different than the $PATH variable the Terminal uses.
我可以运行命令,还找到了一种向应用程序的$ PATH变量添加预定义路径的方法,但是我需要一种自动读取终端的$ PATH变量并将其复制到应用程序的$ PATH变量的方法.这应该是自动化的,因为用户在变量内将拥有不同的路径.
I can run commands and also found a way to add a predefined path to the application's $PATH variable but I need a way to automatically read the Terminal's $PATH variable and copy it to the Application's $PATH variable. This should be automated as users will have different path's inside their variable.
我还发现,当我使用"open appname.app"从终端运行应用程序时,将使用正确的$ PATH变量(终端使用的变量相同)
I also found out that when I run the application from the terminal with "open appname.app" the correct $PATH variable is used (the same one the terminal uses)
到目前为止,这是我的代码:
This is my code so far:
let task = Process()
var env = ProcessInfo.processInfo.environment
var path = env["PATH"]! as String
path = "/usr/local/bin:" + path
env["PATH"] = path
task.environment = env
task.launchPath = "/usr/bin/env"
task.arguments = ["echo","$PATH"]
let pipe = Pipe()
task.standardOutput = pipe
task.standardError = pipe
task.launch()
task.waitUntilExit()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output: String = NSString(data: data, encoding: String.Encoding.utf8.rawValue) as! String
print(output)
这会将"/usr/local/bin"添加到应用程序的$ PATH中,但是我需要一种从终端的$ PATH复制值的方法.
This will add "/usr/local/bin" to the Application's $PATH but I need a way to copy the values from the Terminal's $PATH.
谢谢!
推荐答案
添加bash Shell路径
默认的shell路径可以在/etc/paths
和/etc/path.d/
中找到.读取外壳程序路径的一种方法是使用 path_helper
命令.扩展上面的代码示例并使用 bash 作为 shell:
Adding bash Shell Path
The default shell paths can be found in /etc/paths
and /etc/path.d/
. One way to read the shell paths is to use the path_helper
command. Extending the code example above and using bash as the shell:
let taskShell = Process()
var envShell = ProcessInfo.processInfo.environment
taskShell.launchPath = "/usr/bin/env"
taskShell.arguments = ["/bin/bash","-c","eval $(/usr/libexec/path_helper -s) ; echo $PATH"]
let pipeShell = Pipe()
taskShell.standardOutput = pipeShell
taskShell.standardError = pipeShell
taskShell.launch()
taskShell.waitUntilExit()
let dataShell = pipeShell.fileHandleForReading.readDataToEndOfFile()
var outputShell: String = NSString(data: dataShell, encoding: String.Encoding.utf8.rawValue) as! String
outputShell = outputShell.replacingOccurrences(of: "\n", with: "", options: .literal, range: nil)
print(outputShell)
let task = Process()
var env = ProcessInfo.processInfo.environment
var path = env["PATH"]! as String
path = outputShell + ":" + path
env["PATH"] = path
task.environment = env
task.launchPath = "/usr/bin/env"
task.arguments = ["/bin/bash", "-c", "echo $PATH"]
let pipe = Pipe()
task.standardOutput = pipe
task.standardError = pipe
task.launch()
task.waitUntilExit()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
var output: String = NSString(data: data, encoding: String.Encoding.utf8.rawValue) as! String
output = output.replacingOccurrences(of: "\n", with: "", options: .literal, range: nil)
print(output)
注意:
- 此代码示例以非交互方式调用Shell.这意味着它将不会执行任何用户特定的配置文件;例如
/Users/* userid */.bash_profile
. - 可以在
PATH
环境变量中多次列出路径.它们将从左到右遍历.
- This code example calls the shell in non-interactive mode. This means that it won't execute any user specific profiles; such as
/Users/*userid*/.bash_profile
. - The paths can be listed multiple times in the
PATH
environment variable. They will be traversed from left to right.
参考文献
在OS X的应用程序和Shell PATH上有几个线程,它们提供了更多的上下文如何在OS X小牛和在特立独行者中设置系统范围路径环境变量
这篇关于如何从我的Mac应用程序中访问终端的$ PATH变量,它似乎使用了不同的$ PATH的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!