如何从 AppleScript 访问 Cocoa-AppleScript 应用程序中的捆绑文件?
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于资源文件我使用:
For resource file I use:
“作为字符串的路径”似乎是正确的,但您可能还有其他两个问题。 1) 音频文件的路径应包含“Contents”而不是“Content”。 2)在显示对话框中,您不需要“引用形式”,因为 posix 路径是一个字符串,可以毫无问题地添加到您的“Path:”字符串中。因此,请检查这两件事,看看是否有帮助。
编辑:
如果上述方法没有帮助,可以使用“cocoa 方法来获取应用程序的路径。我不知道 applescript-objc 语法,但这就是您通过 cocoa 方法获取它的方法......
并获取路径资源的直接使用这个...
"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...
And to get the path of a resource directly use this...
答案是,在 Cocoa-AppleScript 应用程序中,正确的语法不是:
而是:
me
作为自引用无效。正确的是当前应用程序
。希望这对其他人有帮助:)
And the answer is that, in a Cocoa-AppleScript Application, the right sintaxis is NOT:
BUT:
me
is not valid as self-reference. The correct one iscurrent application
.Hope this helps others :)
对于简单的情况,您可以将文件放在 XCode 项目中文件树的根级别中,然后执行以下操作:
在我的情况下,我正在执行:
然后它会在关联的应用程序中打开。
当您分发应用程序时,“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:
In my case I'm then doing:
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")
?