【问题标题】:Path to Resources folder in plugin on OS XOS X 插件中资源文件夹的路径
【发布时间】:2011-05-25 08:21:32
【问题描述】:

我正在为 OS X 编写 FileMaker Pro 插件,我需要能够访问 /Applications/FileMaker Pro 11/Extensions/blah.fmplugin/Contents/Resources 目录中的文件。 (但这可能是/apps/FileMaker Pro 11 Advanced/Extensions/xyz.fmplugin/Contents/Resources 或其他内容,具体取决于用户安装 FMP 的位置以及他们是否重命名了插件等。)因为我无法控制用户安装 FMP 的位置,或者他们是否正在运行 FMP 或 FMPA 或他们是否重命名了我的插件(尽管这比 FMP 与 FMPA 的情况不太可能),我知道插件包的路径。

我找到了这样的答案:Relative Paths Not Working in Xcode C++,但这给了我 FileMaker.app 中资源文件夹的路径,而不是我的插件中的路径。

即我得到 /Applications/FileMaker Pro 11/FileMaker Pro.app/Contents/Resources 而不是插件中 Resources 文件夹的路径。

我需要在 Xcode 中配置什么才能使上述解决方案正常工作吗?还是有其他方法可以到达路径? CFBundleGetMainBundle() 调用听起来像是要返回应用程序本身的包而不是插件。

我根据 Bundle Programming Guide 和 CFBundleGetBundleWithIdentifier(由 mipadi 提到)找到了一些可能性,但我还没有弄清楚我需要哪些功能以及先决条件是什么。

【问题讨论】:

    标签: xcode macos bundle


    【解决方案1】:

    这似乎做到了。 CFBundleGetBundleWithIdentifier 也应该可以工作,但我决定使用 NSBundle 而不是 CF*。我把它放在一个 .mm 文件中,并将其包含在项目中。然后我可以调用 get_resources_path() 函数并将路径作为字符串获取。

    #include <Foundation/Foundation.h>
    
    #include <string>
    
    std::string *get_resources_path()
    {
        NSBundle *bundle;
    
        bundle = [NSBundle bundleWithIdentifier: @"com.example.fmplugin"];
        if (bundle == NULL) {
            // error handling
        }
    
        NSString *respath = [bundle resourcePath];
        if (respath == NULL) {
            // error handling
        }
    
        const char *path = [respath UTF8String];
        if (path == NULL) {
            // error handling
        }
    
        return new std::string(path);
    }
    

    【讨论】:

    • 这样一个有用的解决方案。谢谢!!
    【解决方案2】:

    如果你知道你的包的路径,你可以使用CFBundleCreate;或者您可以通过 CFBundleGetBundleWithIdentifier 使用捆绑标识符获取捆绑。

    【讨论】:

      猜你喜欢
      • 2014-03-07
      • 1970-01-01
      • 2010-12-02
      • 2014-07-12
      • 1970-01-01
      • 1970-01-01
      • 2023-01-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多