【问题标题】:How to access from AppleScript a bundled file within a Cocoa-AppleScript Application?如何从 AppleScript 访问 Cocoa-AppleScript 应用程序中的捆绑文件?
【发布时间】:2015-08-22 04:07:58
【问题描述】:

在 AppleScript 中我习惯调用:

set audio_file to (path to me as string) & "Contents:Resources:Audio:Music.mp3"
display dialog "Path: " & (quoted form of POSIX path of audio_file)

我现在在 Xcode 的 Cocoa-AppleScript 项目中拥有这段代码。它编译得很好,但脚本根本没有运行。对话框永远不会显示。

没有(path to me as string) 它可以工作,但没有路径。

【问题讨论】:

  • 我发现问题出在 path to me as string 的“我”部分。如果我使用 home 文件夹的路径作为字符串 它可以工作。现在的子问题是“如果我不能使用 me,如何自引用脚本文件夹?”
  • 答案是,在 Cocoa-AppleScript 应用程序中,正确的 sintaxis 不是:(path to me as text) 但:(path to current application as text) me 作为自引用无效.正确的是当前应用程序。希望这对其他人有帮助:)

标签: cocoa applescript applescript-objc


【解决方案1】:

对于我使用的资源文件:

set fname to POSIX path of (path to resource "filename")

【讨论】:

    【解决方案2】:

    “我的路径作为字符串”似乎是正确的,但您可能还有其他 2 个问题。 1) 音频文件的路径应包含“内容”而不是“内容”。 2)在显示对话框中,您不需要“引用表单”,因为 posix 路径是一个字符串,可以毫无问题地添加到您的“路径:”字符串中。所以检查这两件事,看看是否有帮助。

    编辑: 如果上述方法没有帮助,则有一种“可可方法来获取应用程序的路径。我不知道 applescript-objc 语法,但这是您通过可可方法获取它的方式......

    NSString* appPath = [[NSBundle mainBundle] bundlePath];
    

    要直接获取资源的路径,使用这个...

    NSString* resourcePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"mp3"];
    

    【讨论】:

    • "Content" 是写问题的错误。我同意你的观点(作为字符串的路径)似乎是正确的。但是:display dialog (path to me as text) 在 Cocoa-AppleScript 中不起作用,问题在于 me,因为 display dialog (path to home folder as text) 起作用。
    • 我在帖子中添加了一些可可代码,这可能对“编辑”部分有所帮助。
    • AFAIK 你不能在 .applescript 脚本中混合 Cocoa :(
    • 当然可以。您的问题表明它是一个 Cocoa-Applescript 应用程序。所以我假设你正在用 Cocoa 编写它并在其中运行 applescript 代码。因此,您可以计算 cocoa 中的 appPath 并将其作为参数传递给您的 applescript 处理程序。
    【解决方案3】:

    答案是,在 Cocoa-AppleScript 应用程序中,正确的 sintaxis 不是:

    (path to me as text)
    

    但是:

    (path to current application as text)
    

    me 作为自引用无效。正确的是current application

    希望这对其他人有帮助:)

    【讨论】:

      【解决方案4】:

      不过,对于简单的情况,您可以将文件放在 XCode 项目中文件树的根级别,然后执行:

          set theFilePath to (path to resource "SomeFile.ext")
      

      就我而言,我正在这样做:

          tell application "Finder"
              open theFilePath
          end tell
      

      它会在其关联的应用程序中打开。

      当您分发应用程序时,“SomeFile.ext”最终会出现在应用程序包的 Resources 文件夹中。

      不是 100% 确定如何指定子文件夹,因为到目前为止我还不需要,但我想应该是 (path to resource "Audio:SomeFile.mp3")?

      【讨论】:

        猜你喜欢
        • 2010-11-02
        • 1970-01-01
        • 1970-01-01
        • 2016-11-04
        • 2011-05-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-09
        相关资源
        最近更新 更多