Search code examples
applescriptxcode6applescript-objc

Can't get relative location of image to use, having to use absolute path


Im building my first app in Xcode using Applescript Objc - I have managed to get it to swap an image (in an outlet called imageViewOne) based on the code below:

-- Declarations
property ImagePath : missing value
property imageViewOne : missing value
property NSImage : class "NSImage"
property parent : class "NSObject"


-- set path as variable
set my ImagePath to "/Users/monkey/XCode/testapp/green.png"

set theImage to initWithContentsOfFile_(POSIX path of ImagePath) of alloc() of class "NSImage"     of current application
setImage_(theImage) of imageViewOne

While this works, the problem is that I need to find a way to use a relative path, not "/Users/monkey/XCode/testapp/green.png" (the png is included in the XCode project).

Any help would be well received - as you can see I am new to this!


Solution

  • I worked it out.

    set ImagePath to current application's NSBundle's mainBundle()'s bundlePath() as text & "/Contents/Resources/imageName.png"
    

    Thanks.