From James Duncan's blog:
As I wrote before, PerlObjCBridge is available in Mac OS X's system perl to provide a calling bridge between Perl, and Objective-C. It ships also with Foundation, which is Apple's fundamental elements of Cocoa library.
However, having access to Foundation doesn't buy you very much, but luckily everything you need to be able to load other frameworks is present.
The trick is using NSBundle to load the frameworks at run time.my $frameworkPath = NSString->stringWithCString('/path/to/a/framework'); my $framework = NSBundle->alloc->init->initWithPath_($frameworkPath); $framework->load();Once this has been done the classes in the framework are available to you, however, you need to perform one last bit of magic to really use them. You need to declare the class in Perl and have it inherit from PerlObjCBridge to have messages passed along.package NSWhatever;And hey presto! You should have access to the class you want.
use base qw( PerlObjCBridge );
Then later:my $path = NSString->stringWithCString_('/System/Library/Frameworks/AppKit.framework');
$appkit = NSBundle->alloc->init->initWithPath_($path);
$appkit->load if $appkit;
if ($appkit->isLoaded) {
no strict 'refs';
for my $class (qw(NSWorkspace NSImage)) {
@{$class . '::ISA'} = 'PerlObjCBridge';
}
} else {
undef $appkit;
}
and:if ($appkit && defined $iconOfApp) {
my $path = NSWorkspace->sharedWorkspace->fullPathForApplication_(
NSString->stringWithCString_($iconOfApp)
);
if ($path) {
my $icon = NSWorkspace->sharedWorkspace->iconForFile_($path);
if ($icon && $icon->isValid) {
$regDict->setObject_forKey_($icon->TIFFRepresentation, GROWL_APP_ICON);
}
}
}
Took me awhile to get it all working, but it is working now. Again, thanks!if ($appkit && defined $image && -e $image) {
my $path = NSString->stringWithCString_($image);
if ($path) {
my $icon = NSImage->alloc->initWithContentsOfFile_($path);
if ($icon && $icon->isValid) {
$noteDict->setObject_forKey_($icon->TIFFRepresentation, GROWL_NOTIFICATION_ICON);
}
}
}
Re:Thanks!
pemungkah on 2005-03-02T23:31:25
Glad I could help. My Cocoa-fu is still almost nonexistent, but that was such a good hack I had to put it somewhere I wouldn't lose it.