obj-c bridge issues

this is less about ASStudio than it is about the obj-c bridge… but this appears to ber the more logical place to get a good response…

I am having trouble with the call method handler…

I have made a custom obj-c class (which inherits from a cocoa class) in my project and then I want to create an instance of it with applescript like so:


set myNewObj to call method "alloc" of class "MyClass"
set myNewObj to call method "init" of object myNewObj

It doesn’t seem to like this code however… any tips?

You’re much more likely to get help on something like the Cocoa email list:

http://lists.apple.com/mailman/listinfo/cocoa-dev

Posting at this BBS really assumes one primary prerequisite: AppleScript as the language. AppleScript Studio may use the same developer tools/apps as Cocoa and Obj-C, but it’s still the AppleScript language we’re dealing with in the end.

Hope this helps.

thanks. I thought someone might have something usefull though because I believe the problem is more in the applescript call method technique than the actual obj-c. “call method” is defined as part of the ASStudio set of handlers and IMO a very important piece of it conceptually.

Here’s some more info (perhaps it will help). I am trying to build a dynamic dock menu based on the applescript web site checker developed in a recent thread on the OSX forum (re: Memory issues (I think…) )

I started by creating an applescript application in project builder and editing the nib to get rid of the window and add a menu that I linked the the dockMenu outlet of the files owner (NSApplication). I named the dock menu dockMenu in the applescript dialog and set it to call the on awake from nib handler in my applescript file. I also subclassed NSMenuItem to MCMenuItem, so that I could define the MCMenuItem to have an associated URL that it will open with applescript on its action.

here’s MCMenuItem.h :


/* MCMenuItem */

#import <Cocoa/Cocoa.h>

@interface MCMenuItem : NSMenuItem
{
	NSString* myURL;
}
-(id)initWithTitle:(NSString*)title withURL:(NSString*) url;
-(BOOL)openURL;
@end

here’s MCMenuItem.m


#import "MCMenuItem.h"

@implementation MCMenuItem
-(id)initWithTitle:(NSString*)title withURL:(NSString*) url {
	myURL = url;
	[super initWithTitle:title action:@selector(openURL) keyEquivalent:@""];
	return self;
}
-(BOOL)openURL {
	id mySource = [[NSString alloc] stringWithFormat:@"open location %@", myURL];
	id myScript = [[NSAppleScript alloc]initWithSource];
	[myScript executeAndReturnError:nil];
	return YES;
}
@end

and finally here’s the applescript that should initialize the menu:


property the_sites : {¬
	{site_name:"Macslash", shell_script:"curl macslash.org/search.pl | grep 'article.pl' | sed 's/.*\/\/\(.*\)&.*>\(.*\)<\/A>.*/\1" & return & "\2/'", new_links:{}} ¬
		}

on awake from nib theObject
	set theDockMenu to menu "dockMenu"
	repeat with site in my the_sites
		set the site to the updated (site)
		repeat with link in the site's new_links
			display dialog "will alloc"
			set newMenuItem to call method "alloc" of class "MCMenuItem"
			display dialog "alloc"
			set newMenuItem to call method "initWithTitle:withURL:" of object newMenuItem with parameters {link's the_headline, link's the_link}
			display dialog "init"
			call method "addItem:" of object theDockMenu with parameter newMenuItem
			display dialog "added"
		end repeat
	end repeat
end awake from nib

on updated (the_site)
	set cur_links_raw to every paragraph of (do shell script (the_site's shell_script))
	set cur_links_parsed to {}
	repeat with index from 1 to (count of cur_links_raw) by 2
		set the_link to item index of cur_links_raw
		set the_headline to item (index + 1) of cur_links_raw
		set link_record to {the_link:the_link, the_headline:the_headline}
		copy link_record to end of cur_links_parsed
	end repeat
	set the_site's new_links to cur_links_parsed
	return the_site
end updated

when I run it, everything compiles but It stops running after the display dialog “will alloc” and outputs the error message:

Any hints?

Is it possible to post a copy of the project? OT: you should use the log command instead of displaying dialogs to let you know where the script is (change display dialog “x” to log “x”).

Jon

http://homepage.mac.com/mooresan/hexley.tgz

I’ll try to take a look at it this weekend but I think it’s probably beyond me. Thanks for sharing.

Jon

Haven’t implemented yet… but for those following along (jonn8?) I think I have found a solution (cough workaround cough)…

I am going to add a class as the NSApp’s delegate in the nib file, and give it a method “addMCItem:URL:” and in that method have it create the menu item and attach it… this way I don’t have to do that part in applescript. It makes the code more modular too… (the resulting applescript will be: call method “addMCItem:URL:” with parameters {headline, url} – note default reciever is NSApp… which passes it to it’s delegate)

If it doesn’t work I may implement the whole thing in obj-c, and get it to use the NSApplescript class to do the script for gathering and formatting the text…

(or maybe I’ll just get that stuff in a shell script and get obj-c to run it, outputting to a file, read in through obj-c…)

That I think is my order of preference… more applescript the better…any thoughts?

I looked at your file and I couldn’t get the bridge to work properly but there was another problem that needs to be addressed first if you are going to stick with an AS solution.

Try adding this code in your main script:

--existing code:
set theDockMenu to menu "dockMenu"
--added line
log theDockMenu

You should get an error saying that theDockMenu isn’t defined which means the first line doesn’t work to identify the dock menu.

Also, I think:

id mySource = [[NSString alloc] stringWithFormat:@"open location %@", myURL];

should be:

id mySource = [[NSString alloc] stringWithFormat:@"open location "%@"", myURL];

You can take a look at this project (DockLines) which is a modification of the Recent Tunes project from cocoadevcentral. This creates a menu similar to a menu extra and not a dock menu but it may give you some clues.

Good luck and, as always, I’m very interested to see if you can get this to work (and if so, if you’d be kind enough to share… :wink: ),
Jon

Have you had any luck?

I’ve tried to build a very basic dynamic dock menu app in AS Studio but it isn’t working right. I have solved the problem of identifying the dock menu itself with an “awake from nib” call that sets a property to the dock menu. I also have the Obj-C class instantiated in the mainMenu NIB and that seems to work as the IBAction is connected and working properly (it just displays a dialog with the menu selected–how do I just get the title of the menu selected?).

Note: sometimes this app works when being run from PB but most times it doesn’t. This is a known problem with PB. Try running the built app on its own from the Finder with the Console open to see the logged messages.

The app can be downloaded here: DockMenu.hqx

Any help (from anyone) would be greatly appreciated.

Jon

Well, I just revisited this and finally made some headway. I have successfully built a dock menu submenu on the fly and hooked it into an AppleScript called from Obj-C. The next step is to get the Obj-C to call a subroutine from the main AppleScript…

For those interested, you can see how this is done by taking a look at: DockMenu.hqx.

Jon