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

梦想的初衷 提交于 2019-12-06 16:04:40

问题


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.


回答1:


For resource file I use:

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



回答2:


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



回答3:


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




回答4:


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



来源:https://stackoverflow.com/questions/5930726/how-to-access-from-applescript-a-bundled-file-within-a-cocoa-applescript-applica

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