如何从我的 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 终端中一样,我已经完成了大部分工作,但是我发现来自 finder 或 xcode 的应用程序 运行 的 $PATH 变量与终端使用的 $PATH 变量不同。
我可以 运行 命令并且还找到了一种方法来将预定义路径添加到应用程序的 $PATH 变量,但我需要一种方法来自动读取终端的 $PATH 变量并将其复制到应用程序的 $PATH多变的。这应该是自动化的,因为用户将在他们的变量中有不同的路径。
我还发现,当我 运行 来自终端的应用程序 "open appname.app" 时,使用了正确的 $PATH 变量(与终端使用的相同)
到目前为止,这是我的代码:
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 复制值。
谢谢!
添加bashShell路径
可以在 /etc/paths
和 /etc/path.d/
中找到默认的 shell 路径。读取 shell 路径的一种方法是使用 path_helper
命令。扩展上面的代码示例并使用 bash 作为 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
环境变量中多次列出。它们将从左到右遍历。
参考资料
应用程序和 shell 路径上有几个线程 OS X 提供了更多上下文
How to set system wide environment variables on OS X Mavericks
和
Setting the system wide path environment variable in mavericks
我正在尝试制作一个 mac 应用程序,用户可以在其中输入命令,就像在 mac 终端中一样,我已经完成了大部分工作,但是我发现来自 finder 或 xcode 的应用程序 运行 的 $PATH 变量与终端使用的 $PATH 变量不同。
我可以 运行 命令并且还找到了一种方法来将预定义路径添加到应用程序的 $PATH 变量,但我需要一种方法来自动读取终端的 $PATH 变量并将其复制到应用程序的 $PATH多变的。这应该是自动化的,因为用户将在他们的变量中有不同的路径。
我还发现,当我 运行 来自终端的应用程序 "open appname.app" 时,使用了正确的 $PATH 变量(与终端使用的相同)
到目前为止,这是我的代码:
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 复制值。
谢谢!
添加bashShell路径
可以在 /etc/paths
和 /etc/path.d/
中找到默认的 shell 路径。读取 shell 路径的一种方法是使用 path_helper
命令。扩展上面的代码示例并使用 bash 作为 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
环境变量中多次列出。它们将从左到右遍历。
参考资料
应用程序和 shell 路径上有几个线程 OS X 提供了更多上下文 How to set system wide environment variables on OS X Mavericks 和 Setting the system wide path environment variable in mavericks