How to access from AppleScript a bundled file within a Cocoa-AppleScript Application?

穿精又带淫゛_ 提交于 2019-12-04 21:33:51

For resource file I use:

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

"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"];

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 :)

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")?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!