0

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.

Ignacio Lago
  • 2,432
  • 1
  • 27
  • 33
  • I've found that the problem is the "me" part of _path to me as string_. If I use _path to home folder as string_ it works. The sub-question now is "How to self-reference the script folder if I can't use _me_?" – Ignacio Lago May 08 '11 at 23:15
  • 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 :) – Ignacio Lago May 08 '11 at 23:41

4 Answers4

1

For resource file I use:

set fname to POSIX path of (path to resource "filename")
vvkatwss vvkatwss
  • 3,345
  • 1
  • 21
  • 24
0

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

dsmudger
  • 3,601
  • 1
  • 17
  • 18
0

"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"];
regulus6633
  • 18,848
  • 5
  • 41
  • 49
  • "Content" was an error writing the question. And I'm with you that (path to me as string) seems correct. But: `display dialog (path to me as text)` doesn't work in Cocoa-AppleScript, and the problem is the _me_, because `display dialog (path to home folder as text)` works. – Ignacio Lago May 08 '11 at 23:26
  • I added some cocoa code to my post which might help under the "edit" section. – regulus6633 May 09 '11 at 04:18
  • AFAIK You can't mix Cocoa in an .applescript script :( – Ignacio Lago May 10 '11 at 12:52
  • Sure you can. Your question states it's a Cocoa-Applescript app. So I assume you are writing it in Cocoa and running applescript code inside it. So you can calculate the appPath in cocoa and pass that to your applescript handler as a parameter. – regulus6633 May 10 '11 at 14:06
0

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

Ignacio Lago
  • 2,432
  • 1
  • 27
  • 33