Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send event or message to external process in Mac?

In Windows I can post custom message to another process and inform it to do an action like:

PostMessage(WindowOfAnyProcess, WM_CUSTOM_MESSAGE, param1, param2)

What is the alternative on Mac OS? Does Carbon Events help me? How? Thankyou.

like image 952
mh taqia Avatar asked Jun 19 '12 10:06

mh taqia


1 Answers

Assuming that both of the processes are yours, you can use NSDistributedNotificationCenter to send notifications and data to each process.

To do this do something like:

[[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"HelloFromProcessOne" object:nil]

If you want to include data you can use:

[[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"HelloFromProcessOne" object:nil userInfo:[NSDictionary dictionaryWithObject:@"some info here" forKey:@"data"]]

A note should be added that: Sandboxed apps can send notifications only if they do not contain a dictionary. If the sending application is in an App Sandbox, notificationInfo must be nil. This means that you won't be able to provide information with the notification if you intend on targeting the Mac AppStore.

To make the application receive the notifications do something like:

[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(someNotificationUpdate:) name:@"HelloFromProcessOne" object:nil]

someNotificationUpdate: would be declared like:

- (void)someNotificationUpdate:(NSNotification *)note;
like image 185
nosedive25 Avatar answered Nov 05 '22 06:11

nosedive25