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

518 Views Asked by At

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!

3

There are 3 best solutions below

0
On BEST ANSWER

I worked it out.

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

Thanks.

2
On

Would something like this work for you?

set parentDirectory to POSIX path of ((path to me as text) & "::")
1
On

AppleScript was originally created for the Classic Mac OS, which did not have a concept of a current directory, as UNIX does. It had "working directories", which are often confused for "current directory" but are quite different.

What you'll need to do (I expect) is find the absolute path of something you can depend on, such as the location of your script, or of an application bundle if your script is inside a bundle, then append your relative path to that absolute path.

In the case of my own iOS App, in Objective-C with Cocoa Touch, I can get my app's Documents folder with:

- (NSString*) documentDirectoryPath
{
    return [NSHomeDirectory() stringByAppendingPathComponent: @"Documents"];
}

Can AppleScript call NSHomeDirectory, or does it provide a functional equivalent?

Note that NSHomeDirectory provides the current app's directory, not the user's home.

edit: I don't actually know whether AppleScript provides UNIX-style current directories. Possibly it does. However, a GUI app cannot count on having any specific current directory at the time it launches. Usually it will be the Finder's current directory, for OS X, or for iOS it would be the current directory of the program that presents the app icons and launches them - the program that displays the home screen. I don't know what it's called.

Only Classic Mac OS, as well as Carbon on Mac OS X have the concept of working directories. I don't think Windows has them. UNIX processes can have only one current directory, but Classic Mac OS programs can have as many working directories as memory allows.

Working directories were originally a hack to enable very old - Mac 128k - executables to do file I/O on Heirarchical Filesystem floppy disks. The 1984 Superbowl Mac had "Macintosh Filesystem" floppies that were flat and not heirarchical. That is, one could not have folders within folders.

To be completely clear, I'm discussing working directories because the ORIGINAL AppleScript used them. I really do not know whether AppleScript has been revised to add the concept of the - one per-process - UNIX-style current directory.

Even if AppleScript does provide the current directory, you'll need to set it explicitly, because as I said you cannot count on the current directory at launch being what you expect.