0

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?

4

1 回答 1

0

Dock 将reopenApple 事件发送到您单击的应用程序,然后应用程序执行它认为合适的代码。要模拟 Dock 单击,您需要reopen手动将事件发送到应用程序。

有关此事的Apple 文档指出该reopen事件的 ID 为rapp代码 ( kAEReopenApplication) 并且是kCoreEventClass该类的一部分。

从那里开始,在代码中构建 Apple 事件并将其发送到应用程序相对简单。请注意,您确实应该从AESendMessage调用中进行一些错误检查,因为如果未启动应用程序等,事件可能会失败——我还没有真正测试过这些情况。

这是我的解决方案。请注意,您需要保留原始代码才能将应用程序置于最前面——Apple Event 不会更改应用程序顺序。

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);
于 2013-06-09T03:49:04.577 回答