How to open a Safari App Extension popover using a keyboard shortcut?

Hi,


I created an extension that has a toolbar button which opens a popover window.

Is it possible to assign a keyboard shortcut to open the popover window?


I tried to add a keyboard shortcut for Safari using the toolbar button text (text you see when you hover over the button) as an identifier but that didn't work.


Thank you,

Kristof

Accepted Reply

On the latest version of Safari, there is an API to open the popover:


https://developer.apple.com/documentation/safariservices/sfsafaritoolbaritem/3075413-showpopover?language=objc


However, setting up a keyboard shortcut is tricky, as there currently isn't a supported way to do it. You could inject a content script that listens for your shortcut and sends a message to your Safari App Extension.


Please file a radar about the keyboard shortcuts?


Thanks!

Replies

On the latest version of Safari, there is an API to open the popover:


https://developer.apple.com/documentation/safariservices/sfsafaritoolbaritem/3075413-showpopover?language=objc


However, setting up a keyboard shortcut is tricky, as there currently isn't a supported way to do it. You could inject a content script that listens for your shortcut and sends a message to your Safari App Extension.


Please file a radar about the keyboard shortcuts?


Thanks!

Thank you for the quick response! I filed a bug report (id 49877009).

I might try to inject a script but having improved support for keyboard shortcuts would make sense I think.

thanks for you best response...its really helpful informations...


Best Regards

ministoreonline

I tried to implement the suggestion by injecting a script to detect keyboard shortcuts but I ran into a new problem.


The script part works but it looks like inside the 'messageReceived' method I have no access to ToolbarItem which would allow me to show the popover. The 'messageReceived' method has access to 'SFSafariPage' but not to 'SFSafariWindow' which gives access to the ToolbarItem.


Support for keyboard shortcuts would be highly appreciated. Thanks!

In theory...


override func messageReceived(withName messageName: String, from page: SFSafariPage, userInfo: [String : Any]?) {

// This method will be called when a content script provided by your extension calls safari.extension.dispatchMessage("message").

page.getContainingTab { (tab) in

tab.getContainingWindow(completionHandler: { (window) in

window?.getToolbarItem(completionHandler: { (toolbaritem) in

toolbaritem?.showPopover()

})

})

}

}


But I did never have any success with this method showPopover.

Either using Command or Popover setting in Info.plist for the Action.

The showPopover message does nothing...


If you have any success with this... I'd be pleased to know.

Thank you very much for the suggestion!

I missed the fact that I could get access to the toolbaritem through page -> tab -> window, and it actually worked for me!

See following commit for details.