First, doing ANYTHING with icons is a recognized and admitted weakness of applescript. There is no direct support via applescript, standard additions, finder, etc. There may be an osax out there, but your best and likely only bet will be to use obj-c…as you’ve tried already. The “NSInternalScriptError (8 )” errors you’re getting probably mean that your applescript version simply isn’t capable of doing what you’re trying to do the way you’re trying to do it.
I’m running panther, so I can’t guarantee this will work in jag but here’s what works for me…
I have a basic app with an image view, a text field, and a button. Note that the button will not be able to send messages to any applescript code, because a cocoa connection supercedes any applescript calls, and any handlers (such as a clicked handler) connected to the button will never be called. This is something I’ve been trying to work around by creating a custom class that responds to call method calls, but I’m not obj-c savy enough to work that out yet. You can provide an AS name for the image view and the text field and control their contents with AS, but the button will only respond to cocoa once it’s connected.
- Create the image view, a text field, and a button. Make sure to set the image view to something about 150x150 pixels, so it has room to display a full-size 128x128 icon. The text field will provide the path to the file you want the icon of. You may want to connect some AS code to another button that will set the value of the text field like…
set contents of text field "text" of window "main" to (choose file)
-
In IB, click on the “Classes” pane in the nib window. Select NSObject (at the root of the classes). In the “Classes” menu in the IB main menu, select “Subclass NSObject”. A new subclass will be created to the right…name it “Controller”.
-
Select the new “Controller” subclass, and then go to ‘attributes’ pane of the IB info window. Select ‘Outlets’, then click ‘Add’ (at the bottom). Name the new outlet “theFilePath”. Add another outlet and name it “theImageView”. Then, click on ‘Action’, and create an action named “setImageViewContents” in the same way.
-
In the IB “Classes” main menu, select “Instantiate Controller”, and then select “Create Files For Controller”. This will create an IB object called “Controller” in the nib window, and will create “Controller.h” and “Controller.m” in your project.
-
In IB, hold the ‘control’ key, and drag from your button to the ‘Controller’ object in the nib window. Under ‘Target/Action’, you should find “setImageViewContents:” as an option. Select it, and click “Connect” at the bottom. Now whenever the button is pushed, it will execute the code we provide soon in the “setImageViewContents” method.
-
Again in IB, while holding ‘Control’, drag from the ‘Controller’ object to the image view. Under ‘Outlets’ select “theImageView” and then click ‘Connect’. Repeat this proces for the text field, connecting it to the “theTextField” outlet.
-
In xcode (or project builder) go into the “Controller.h” file we created, where you should see something very similar to this…
/* Controller */
#import <Cocoa/Cocoa.h>
@interface Controller : NSObject
{
IBOutlet id theFilePath;
IBOutlet id theImageView;
}
- (IBAction)setImageViewContents:(id)sender;
@end
8.) Then go into the “Controller.m” file, where you’ll find a default implementation. Add any of the following code necessary to make the file look like this…
#import "Controller.h"
@implementation Controller
- (IBAction)setImageViewContents:(id)sender
{
NSImage* img = [ [ NSWorkspace sharedWorkspace ] iconForFile : [ theFilePath stringValue ] ];
// Make the icon it's full 128x128 pixel size; otherwise it's 32x32 //
[ img setSize:NSMakeSize(128.0,128.0)];
[ theImageView setImage: img ];
}
@end
- Unless I forgot something, you should now be able to build and run the app. The text field contains the path to the file you want the icon for. So, if you enter “/”, it should display the icon for the HD. If you enter “/Applications/” you’ll get the icon for your applications folder. You should be able to get the icon for anything you provide a valid posix path for.
From here you’ll have to figure out a way to put your path into the text field. If this doesn’t fit your exact needs, post what your exact configuration is and we can try to hammer it out. Also, there’s a lot more code you could put into this, like error-checking and such that I’m just not capable of doing in obj-c.
Good luck, hope this helps…
j