开发者

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

In AppleScript I'm used to call:

set audio_file to (path to me as string) &开发者_如何转开发amp; "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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜