Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mfniaki

macrumors newbie
Original poster
Nov 24, 2009
10
0
In my application I'm trying to call a function in Cocoa from Javascript to set dock badge number. it partly works.
when my application is active and I receive call from javascript it works and set badge counter.
but when my application is not active and is not in front, then I can't call from javascript cocoa codes.
I would really appreciate if anybody can help me how can I fix this problem.

Code:
#import "WebViewExampleAppDelegate.h"



@implementation WebViewExampleAppDelegate

@synthesize window;
@synthesize webView;


- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
	scriptObject = [webView windowScriptObject];
    
    // add self as an object in javascript
	[scriptObject setValue:self forKey:@"test"];
	
	
}
- (BOOL) applicationShouldOpenUntitledFile:(NSApplication *)sender
{
    [window makeKeyAndOrderFront:self];
	[discoveredServices removeAllObjects];
    return NO;
}




- (void)awakeFromNib {
	
	NSString *urlAddress = @"http://test.com/";
	NSURL *url = [NSURL URLWithString:urlAddress];
	
	[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:url]];
	
	

}




- (void)setBadgeCounter:(NSInteger*)d
{
	//[self move:d];
	NSLog(@"Set Badge Counter = %d", d);
	badgeCounter=d;
	NSNumber *discoveredServicesCount = [NSNumber numberWithInteger:badgeCounter];
	dockBadgeString = [NSNumberFormatter localizedStringFromNumber:discoveredServicesCount numberStyle:NSNumberFormatterDecimalStyle];
	
	
	if (badgeCounter > 0) {
	[[[NSApplication sharedApplication] dockTile] setBadgeLabel:dockBadgeString];
	}
	else {
		[[[NSApplication sharedApplication] dockTile] setBadgeLabel:nil];
	}

	
}


- (void) showNotification:(const NSString*) imageUrl title:(const NSString*) title body:(const NSString*) body {
	
	NSLog(@"Show Notification = %@", body);
	//GROWL
    //m_trayIcon->showMessage(title, body, QSystemTrayIcon::Information, 15000);
	
}

- (void) openExternalUrl:(const NSString*) url {
    //External Link
	//QDesktopServices::openUrl(QUrl(url));
	NSLog(@"Open External Link = %@", url);
}



+ (NSString *)webScriptNameForSelector:(SEL)sel
{
    // change the javascript name from 'setBadgeCounter_' to 'setBadgeCounter' ...
	if (sel == @selector(setBadgeCounter:))
		return @"setBadgeCounter";
	else if (sel == @selector(showNotification:))
		return @"showNotification";
	else if (sel == @selector(openExternalUrl:))
		return @"openExternalUrl";
	
	
	return nil;
}

+ (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector { return NO; }
+ (BOOL)isKeyExcludedFromWebScript:(const char *)name { return NO; }


@end
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.