【问题标题】:Cocoa, output current App pathCocoa,输出当前App路径
【发布时间】:2013-05-06 03:24:50
【问题描述】:

我正在开发 OS X 应用程序。在代码中,我想将当前 Mac OS App 的路径输出到一个变量中以备将来使用。所以后来我可以读取同一文件夹中的文件。谁能告诉我 Xcode 中的命令/方法?

非常感谢。

更新

为了更清楚,我正在使用 xcode 并创建一个可可应用程序。 我的应用程序与applescript连接以控制Mac软件在Mac上读取文件。所以我必须返回文件的目录和名称。

其实我不知道该怎么做。所以就卡在这里了。

【问题讨论】:

  • 这是一个与 xcode 相关的问题吗?
  • @Monolo 感谢您的关注。我认为 obj-c 和 applescript 对我来说都可以。只是一个简单的app,有必要是基于NSDocument的app吗?

标签: macos cocoa xcode4


【解决方案1】:

swift 5,x 我的 2 美分:

let path = Bundle.main.bundlePath
 print(path)
 

如果您需要一条通往 REAL exacutable 的路径:(即您将桅杆传递给 shell 或类似的..)

 let appName =  AppName()

 let exePath = path + "/Contents/MacOS/" + appName 

  print(exePath)

AppName() 在哪里:

func AppName()->String{
let bund = Bundle.main
//print(bund)
if let displayName = bund.object(forInfoDictionaryKey: "CFBundleDisplayName") as? String {
    if displayName.count>0{
        return displayName
    }
}

if let name = bund.object(forInfoDictionaryKey: "CFBundleName") as? String {
    return name
}
return "no AppName"

}

【讨论】:

    【解决方案2】:

    要获取应用程序的 URL,您可以使用:

    [[NSBundle mainBundle] bundleURL]
    

    有关详细信息,请参阅NSBundle Class Reference

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-11
      • 1970-01-01
      相关资源
      最近更新 更多