2015-08-03 67 views
0

我有一个询问外部进程的工具,我想添加一些在Info.plist文件上工作的objective-c查询。OS X如何获取应用程序的基本路径,超出可执行的完整路径

为了做到这一点,我使用的是应用程序的基本路径下面的代码作为输入(即/Applications/Notes.app/)

NSDictionary* infoDict = [[NSBundle bundleWithPath:vlcFilePath] infoDictionary]; 

然而,我只能得到可执行文件路径从远程过程(即/Applications/Notes.app/Contents/MacOS/Notes),我想转换为基本路径。

我使用的std ::字符串与RFIND为了得到我所需要的,但我不知道是否有是还可以在一些有效的非convensional路径编队运行(我的代码是脆弱的路径更传统的方式它包含几个连续的斜杠,如/Application/Notes.app/Contents/MacOS//Notes)。

我的代码(正好等于倒退到父目录3次):

for (int i=0;i<3;i++) { 
    const size_t last_slash_idx = executable.rfind('/'); 
    if (std::string::npos != last_slash_idx) { 
     executable = executable.substr(0, last_slash_idx); 
    } 
} 

回答

0

好,我已经使用,它可以查找最后一次出现string.rfind方法发现了一些巧妙的解决办法多次尝试后“.app”,然后从该索引开始截断字符串。

std::string app_suffix(".app"); 
int split_idx = executable.rfind(app_suffix); 
executable = executable.substr(0,split_idx+app_suffix.size());