【问题标题】:How can I use the Mac file selection menu in my own program?如何在自己的程序中使用 Mac 文件选择菜单?
【发布时间】:2015-10-15 22:47:56
【问题描述】:

在我使用过的几乎所有桌面应用程序中,都可以使用看起来像 Finder 的标准文件导航器来选择打开或保存文件的路径。例如,如果我去保存这个网页, this menu comes up,我可以选择保存位置,创建新文件夹等。是否有系统调用从我自己的程序中启动此菜单,并大概返回所选文件或文件夹的路径?

【问题讨论】:

    标签: macos path


    【解决方案1】:

    看看NSOpenPanel

    它完全符合您的要求:

    NSOpenPanel 类为 Cocoa 用户界面提供打开面板。应用程序使用“打开”面板作为一种方便的方式来向用户查询要打开的文件的名称。

    【讨论】:

    • 这看起来很有希望。例如,是否可以从 Python 进行系统调用?
    • 没关系,明白了!我在下面发布了代码。 Here 是其使用示例。
    【解决方案2】:

    如果其他人看到这一点,Johannes Weiß 的回答是正确的:NSOpenPanel 正是我想要的。在 Python 中,它的代码如下所示:

    from AppKit import NSOpenPanel
    # the following import is only used to prevent multiselect and directory select
    from objc import NO
    panel = NSOpenPanel.openPanel()
    
    # set the title (not required)
    panel.setTitle("open file")
    
    # prevent multiselect and directory select (not required)
    panel.setAllowsMultipleSelection_(NO)
    panel.setCanChooseDirectories_(NO)
    
    # open the panel
    panel.runModal()
    
    # get the path
    path = panel.filenames()[0]
    

    可以找到更长的示例here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-19
      • 1970-01-01
      相关资源
      最近更新 更多