如何使用MacOS应用程序停靠菜单重新打开已关闭的应用程序(通过使用位于左上角的红色按钮)? [英] How to use the MacOS app dock menu to re-open the app that has been closed (by using the red button located on the top left corner)?

查看:165
本文介绍了如何使用MacOS应用程序停靠菜单重新打开已关闭的应用程序(通过使用位于左上角的红色按钮)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早安, 当我使用红色按钮(位于左上角)关闭MacOS应用程序时,MacOS应用程序消失了,但停靠图标仍在底部. 如果我在Dock图标上单击鼠标右键,我想添加重新打开"菜单项以重新打开该应用程序. 下面是产生到一定程度的代码...当我单击重新打开"时,它将在控制台中显示"XXX" ...,因为我找不到重新打开该应用程序的代码! 填写下面的函数调用reOpen(sender:NSMenuItem),对您的帮助将不胜感激. 谢谢

Good Day, When I close my MacOS app by using the red button (located at the top left corner), the MacOS application disapears but the dock icon is still there at the bottom. If I click right on the dock icon I want to add a "Re-Open" menu item to re-open the app. Below is the code produced to a certain point... When I click on "Re-Open" it prints "XXX" in the console... because I have not found the code to re-open the app! Any help would be much appreciated to fill up the below function call reOpen(sender : NSMenuItem) Thanks

下面是我的AppDelegate.swift文件内容:

Below is my AppDelegate.swift file content:

import Cocoa
import SwiftUI

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

  var window: NSWindow!

  func applicationDidFinishLaunching(_ aNotification: Notification) {

    let contentView = ContentView()

    window = NSWindow(
      contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
      styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
      backing: .buffered, defer: false)
    window.center()
    window.setFrameAutosaveName("Main Window")
    window.contentView = NSHostingView(rootView: contentView
      window.makeKeyAndOrderFront(nil)
  }

  func applicationWillTerminate(_ aNotification: Notification) {
    // Insert code here to tear down your application
  }

  func applicationDockMenu(_ sender: NSApplication) -> NSMenu? {
    let menu = NSMenu()
    let reOpenMenuItem = NSMenuItem(title:"Re-Open", action:#selector(AppDelegate.reOpen), keyEquivalent:"")
    menu.addItem(reOpenMenuItem)
    return menu
  }

  @objc func reOpen(sender : NSMenuItem) {
    print("XXX")
  }
}

推荐答案

最后为reOpen函数添加的最终工作代码为:

Finally the final working code to add for the reOpen function is:

@objc func reOpen(sender : NSMenuItem) {
  print("XXX")
  let url = URL(fileURLWithPath: Bundle.main.resourcePath!)
  let path = url.deletingLastPathComponent().deletingLastPathComponent().absoluteString
  let task = Process()
  task.launchPath = "/usr/bin/open"
  task.arguments = [path]
  task.launch()
  exit(0)
}

我在此链接上找到了代码:

I found the code here at this link:

在此处输入链接描述

这篇关于如何使用MacOS应用程序停靠菜单重新打开已关闭的应用程序(通过使用位于左上角的红色按钮)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆