在Cocoa中找出可执行文件的位置 [英] Find out location of an executable file in Cocoa

查看:90
本文介绍了在Cocoa中找出可执行文件的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单的问题是:如何找出可执行文件在Cocoa应用程序中的位置。



请记住,在许多类似Unix的操作系统中,使用PATH环境为其可执行文件分配首选位置,尤其是当他们在系统中有多个版本的相同应用程序时。作为一个好的做法,我们的Cocoa应用程序应该找到它需要的可执行文件的PREFERRED位置。



例如,在/ usr默认配置中有一个SVN 1.4 / bin,并且您安装了一个更新的版本,比如通过MacPorts在/ opt / local / bin下说SVN 1.5.3。您可以使用/etc/path.d或.bash_profile或.zshrc来设置您的PATH:



export PATH = / opt / local / bin:$ PATH

因此,您可以使用新版本的svn,而不是系统中的旧版本。它在任何终端环境中工作良好。但不是在Cocoa应用程序中。 Cocoa应用程序,据我所知,只有一个默认的PATH环境如下:



export PATH =/ usr / bin:/ bin:/ usr / sbin: / sbin



默认情况下,它不会使用/etc/path.d,.bash_profile,.profile,.zshrc等配置。



那么我们该怎么做呢?



ps我们有这里的半解决方案,但它不能完全满足目标。

解决方案

尝试这样做的棘手部分是,用户可以将其shell设置为任何东西:sh,bash,csh,tcsh等,并且每个shell设置其终端环境不同。我不知道我是否会自己去解决这个问题,但如果你真的想要,这里是我要走的路线。



第一步是找出用户的shell。在OS X上,此信息存储在目录服务中,可以通过DirectoryService.framework中的API或使用 dscl 命令行工具来访问。 DirectoryService API是一个皇家的痛苦的屁股,所以我可能去CLI路线。在Cocoa中,你可以使用NSTask来执行带有参数的工具来获取用户的shell(我将在其他地方留下这个细节)。该命令看起来像:



dscl -plist localhost -read / Local / Default / Users / username UserShell



这将返回可以解释为plist并转换为NSDictionary的XML文本,也可以省略 -plist 选项并自己解析文本输出。



一旦知道用户shell的路径,下一步就是执行该shell并让它运行 env 命令打印出用户的环境。看起来大多数shell接受一个 -c 命令行选项,让你传递一个字符串来执行 - 我想你只需要假设作为公共接口对于用户选择的任何shell。



一旦你有了用户的环境,你就可以抓住他们的路径列表,并搜索任何可执行文件从那里寻找。就像我说的,我真的不知道这是否值得的麻烦,但这是我会去,如果我实施这个方向。


The simple question is: how to find out the location of an executable file in a Cocoa application.

Remember that, in many Unix-like OS people use PATH environment to assign the preferred location for their executables, especially when they have several versions of same application in their system. As a good practice, our Cocoa application should find the PREFERRED location of the executable file it needs.

For example, there was a SVN 1.4 in Leopard default configuration at /usr/bin, and you installed a much newer version, say SVN 1.5.3 via MacPorts at /opt/local/bin. And you set your PATH using /etc/path.d or .bash_profile or .zshrc like that:

export PATH=/opt/local/bin:$PATH

So you can use the new version of svn instead of the old one from the system. It works well in any terminal environment. But not in Cocoa applications. Cocoa application, as far as I know, only has a default PATH environment like this:

export PATH="/usr/bin:/bin:/usr/sbin:/sbin"

By default it will not using the configuration in /etc/path.d, .bash_profile, .profile, .zshrc, etc.

So how exactly can we do?

p.s. We have a semi-solution here, but it cannot fully satisfied the objective for this question.

解决方案

The tricky part of trying to do this is the fact that the user could have their shell set to anything: sh, bash, csh, tcsh, and so on, and each shell sets up its terminal environment differently. I'm not sure if I'd go to the trouble for this myself, but if you really want to, here's the route I would take.

The first step is to figure out the user's shell. On OS X, this information is stored in Directory Services, which can be accesed either through the APIs in DirectoryService.framework or by using the dscl command line tool. The DirectoryService API is a royal pain in the ass, so I would probably go the CLI route. In Cocoa, you can use NSTask to execute the tool with arguments to get the user's shell (I'll leave the details of this for elsewhere). The command would look something like:

dscl -plist localhost -read /Local/Default/Users/username UserShell

This will return XML text that you can interpret as a plist and transform into an NSDictionary, or you can omit the -plist option and parse the textual output yourself.

Once you know the path to the user's shell, the next step would be to execute that shell and tell it to run the env command to print out the user's environment. It looks like most shells accept a -c command line option that lets you pass in a string to execute - I guess you'll just have to assume that as being the common interface for whatever shell the user has chosen.

Once you have the user's environment, you can then grab their list of paths out of that, and do the search for whatever executable you're looking for from that. Like I said, I really don't know whether this is worth the trouble, but that's the direction I would go if I were implementing this.

这篇关于在Cocoa中找出可执行文件的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆