如何从 AppleScript 访问 Cocoa-AppleScript 应用程序中的捆绑文件?

发布于 2024-11-05 15:41:49 字数 337 浏览 4 评论 0原文

在 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 项目中拥有此代码。它编译得很好,但脚本根本没有运行。该对话框永远不会显示。

没有(作为字符串的我的路径)它可以工作,但没有路径。

In AppleScript I'm used to call:

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)

I have now this code inside a Cocoa-AppleScript project in Xcode. It compiles well, but the script is not running at all. The dialog never shows.

Without the (path to me as string) it works, but without the path.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

淡忘如思 2024-11-12 15:41:49

对于资源文件我使用:

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

For resource file I use:

set fname to POSIX path of (path to resource "filename")
碍人泪离人颜 2024-11-12 15:41:49

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

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

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

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

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

"Path to me as string" seems correct but you could have 2 other problems. 1) the path to your audio file should contain "Contents" and not "Content". 2) In display dialog you don't need "quoted form" because posix path is a string and can be added to your "Path: " string with no problems. So check those 2 things and see if it helps.

Edit:
If the above doesn't help there's a "cocoa way to get the path to the application. I don't know the applescript-objc syntax but this is how you would get it via a cocoa method...

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

And to get the path of a resource directly use this...

NSString* resourcePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"mp3"];
顾北清歌寒 2024-11-12 15:41:49

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

(path to me as text)

而是:

(path to current application as text)

me 作为自引用无效。正确的是当前应用程序

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

And the answer is that, in a Cocoa-AppleScript Application, the right sintaxis is NOT:

(path to me as text)

BUT:

(path to current application as text)

me is not valid as self-reference. The correct one is current application.

Hope this helps others :)

鯉魚旗 2024-11-12 15:41:49

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

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

在我的情况下,我正在执行:

    tell application "Finder"
        open theFilePath
    end tell

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

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

不是 100% 确定如何指定子文件夹,因为到目前为止我还不需要它,但我想应该是
(资源“Audio:SomeFile.mp3”的路径)

For the simple case though, you can just put the file in the root level of of the file tree in the XCode project, then do:

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

In my case I'm then doing:

    tell application "Finder"
        open theFilePath
    end tell

and it opens in its associated application.

When you distribute your application, 'SomeFile.ext' ends up in the Resources folder of your application package.

Not 100% sure how to specify a sub-folder as I haven't needed that so far, but I guess it'd be
(path to resource "Audio:SomeFile.mp3")?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文