Re: How to send myself an AppleEvent?
Re: How to send myself an AppleEvent?
- Subject: Re: How to send myself an AppleEvent?
- From: Pierre Bernard <email@hidden>
- Date: Mon, 6 Mar 2006 00:16:34 +0100
Hi!
Looking at the stack in the debugger I get the impression that there
really is an AppleEvent being sent. I however couldn't figure out the
AppleEvent stuff.
Below are two ways of achieving what I want.
For the archives (reopen, activate, bring to front):
Solution 1:
NSString *applicationName = [[NSFileManager defaultManager]
displayNameAtPath:[[NSBundle mainBundle] bundlePath]];
NSString *activateString = [NSString stringWithFormat:@"tell
application \"%@\" to activate", applicationName];
NSAppleScript *activateScript = [[[NSAppleScript alloc]
initWithSource:activateString] autorelease];
NSString *reopenString = [NSString stringWithFormat:@"tell
application \"%@\" to reopen", applicationName];
NSAppleScript *reopenScript = [[[NSAppleScript alloc]
initWithSource:reopenString] autorelease];
[activateScript executeAndReturnError:nil];
[reopenScript executeAndReturnError:nil];
Solution 2:
NSString *applicationPath = [[NSBundle mainBundle] bundlePath];
NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
NSDictionary *activeApplication = [workspace activeApplication];
if ([applicationPath isEqualToString:[activeApplication
objectForKey:@"NSApplicationPath"]]) {
if ([[[NSDocumentController sharedDocumentController] documents]
count] == 0) {
[[NSDocumentController sharedDocumentController]
openUntitledDocumentAndDisplay: YES error: nil];
}
}
else {
[workspace launchApplication:applicationPath];
}
The AppleScript solution is more straightforward but slower. I went
with the second solution.
Pierre
On 28 Feb 2006, at 15:03, Pierre Bernard wrote:
Mike,
thanks for your reply. I might investigate the AppleScript route.
I'd rather send my app the very same event it receives upon a
click on the Dock rather than recreating the desired behavior myself.
Pierre
On Feb 28, 2006, at 10:56 AM, Mike Abdullah wrote:
It would probably be easier just to use a small NSAppleScript and
execute that. try something like:
activate application "myApp"
Where myApp is the name of your application. However | am
slightly confused as to what else you want. Are you basically
trying to simulate the act of the user clicking on the dock icon
for your app? If so, and you want to open a new doc, simply do
something like this after running the script:
if ([[[NSDocumentController sharedDocumentController] documents]
count] == 0)
[[NSDocumentController sharedDocumentController]
openUntitledDocumentAndDisplay: YES error: nil]
openUntitledDocAndDisplay is only available in 10.4, but there is
a 10.3 and earlier equivalent.
Mike.
On 27 Feb 2006, at 18:07, Pierre Bernard wrote:
Hi!
I want my application to send itself an AppleEvent.
Specifically I have a document based application that I want to
send a reopen event so it becomes active and creates a new
document as needed.
I came across some Carbon functions and one hardly documented
Cocoa method. Nothing I can make much sense of. Yet I figure
sending an AppleEvent ought to be something easy.
TIA for your help
Pierre _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40gmail.com
This email sent to email@hidden
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden