简体   繁体   English

如何从Safari扩展程序启动Mac OS应用程序?

[英]How to Launch a Mac OS App From Safari Extension?

Does anyone know what the replacement for the following legacy extension code block to launch a MacOS app is in the new Safari Extensions? 有谁知道新的Safari扩展程序中用于启动MacOS应用程序的以下旧扩展代码块的替代品?

safari.application.activeBrowserWindow.activeTab.url = "{app_url_scheme}://"

We are currently working on transitioning from a legacy extension to support the newer Safari Extensions framework. 我们目前正在努力从旧版扩展过渡到支持较新的Safari扩展框架。 In the legacy extension we used the following piece code in the global.html file to launch our Mac OS app: 在旧版扩展程序中,我们在global.html文件中使用以下片段代码启动了Mac OS应用程序:

safari.application.activeBrowserWindow.activeTab.url = "{app_url_scheme}://......."

It seems that the comparable code in the new Safari Extension framework would be the following: 新的Safari扩展框架中的可比代码似乎如下:

guard let url = URL(string: "{app_url_scheme}://") else {
    return
}

SFSafariApplication.getActiveWindow {(activeWindow: SFSafariWindow?)in
    activeWindow?.openTab(with: url, makeActiveIfPossible: false, 
    completionHandler: { (activeTab: SFSafariTab?) in
        print("openTab completed")
    })
} 

However, while the tab will open just fine if it's something like " https://www.cnn.com ", it won't do anything if it's just "{app_url_scheme}://". 但是,虽然像“ https://www.cnn.com ”这样的标签会很好地打开,但如果只是“ {app_url_scheme}://”,它将不会执行任何操作。 Furthermore, I can type in "{app_url_scheme}://" to the Safari browser and it launches the app just fine, so I know it's not a registration problem. 此外,我可以在Safari浏览器中输入“ {app_url_scheme}://”,这样就可以正常启动该应用程序,因此我知道这不是注册问题。

For anyone stumbling across this in the future, it seems the code to launch a MacOS app would be the following: 对于将来遇到任何麻烦的人来说,启动MacOS应用程序的代码似乎如下:

guard let url = URL(string: "{app_url_scheme}") else {
   return
}

NSWorkspace.shared.open(url)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将参数从 App Delegate 传递到系统扩展,Mac OS - Pass Argument From App Delegate to System Extension, Mac OS 我的基于swift的Mac OS应用程序在其他机器上启动时崩溃 - My swift based Mac OS app crashes on launch on other machines 启动时打开菜单栏应用程序(Mac OS 应用程序)[Swift,ViewController 在启动时打开] - Open Menu Bar app on launch (Mac OS App) [Swift, ViewController opening on launch] 从 WidgetKit 小部件扩展检测应用程序启动 - Detect app launch from WidgetKit widget extension 从 FinderSync 扩展启动 macOS 应用程序 - Launch macOS app from FinderSync extension 如何在Safari App Extension中将OAuth与Facebook结合使用 - How to use OAuth with Facebook in a Safari App Extension 如何在 Safari App Extension 中获取活动标签? - How to get active tab in a Safari App Extension? Safari app扩展如何捕获之前的Navigate事件? - Safari app extension how to capture beforeNavigate event? 如何在 Swift 中不弹出后仅从共享扩展启动应用程序? - How to Just launch app from share extension without post popup in Swift? 在原生 safari 应用程序扩展中,我们如何从 swift 调用带有参数的 javascript 函数? - In Native safari app extension How we can call a javascript function with argumemts from swift?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM