0
votes

Iam working on an application that tries to extend some Spotify functionality (not another client). I would like to show Spotify's main window when my icon is clicked on the dock - even if the main window in Spotify is closed.

This is my code now:

- (void) applicationDidBecomeActive:(NSNotification *)notification {
// Causes Spotify to hit the front when selecting it!
[[[NSRunningApplication
   runningApplicationsWithBundleIdentifier:@"com.spotify.client"] lastObject]
 activateWithOptions:NSApplicationActivateAllWindows];
}

It works when the window is open but not in focus (background), but not when I close the Spotify main window (which I people tend to do). Is there any way to re-open this window if it's closed from another application?

The Spotify icon can do this (obviously). Try to hit the close button (the red x) and press the icon (it will reappear). Is that possible from another app?

1

1 Answers

0
votes

The Dock sends the reopen Apple Event to the application you clicked on, which then executes code as it sees fit. To simulate a Dock click, you need to send the reopen event to the application manually.

The Apple Documentation on the matter states that the reopen event has the ID of rapp code (kAEReopenApplication) and is part of the kCoreEventClass class.

From there, it's relatively simple to construct the Apple Event in code and send it off to the application. Note that you really should do some error checking from the AESendMessage call, as the event will likely fail if the application isn't launched, etc — I haven't really tested those cases.

Here is my solution. Note that you'll need to keep your original code to bring the application to the front — the Apple Event doesn't change application order.

NSAppleEventDescriptor *target = [[NSAppleEventDescriptor alloc]
                                    initWithDescriptorType:typeApplicationBundleID
                                                      data:[@"com.spotify.client" dataUsingEncoding:NSUTF8StringEncoding]];

NSAppleEventDescriptor *event = [[NSAppleEventDescriptor alloc]
                                   initWithEventClass:kCoreEventClass
                                              eventID:kAEReopenApplication
                                     targetDescriptor:target
                                             returnID:kAutoGenerateReturnID
                                        transactionID:kAnyTransactionID];

AESendMessage(event.aeDesc, NULL, kAEWaitReply | kAENeverInteract, kAEDefaultTimeout);