NSOpenPanel 无法获得焦点
NSOpenPanel cannot get focus
首先,我是 Swift 的新手,如果我的问题看起来微不足道,我很抱歉。
我想要一个非常简单的命令行程序,它可以打开一个对话框来选择文件或文件夹。此工具不能 运行 带有图标在 Dock 中跳动的实际完整应用程序,而是一些微妙的东西。而已。我所做的正是产生了这个,除了面板无法获得焦点。当我点击面板时,它保持灰色。有趣的是,可以单击按钮或拖放文件,但无法浏览文件系统。也没有捕获键盘事件。
import AppKit
let dialog = NSOpenPanel()
dialog.title = "Choose a .tif file or a folder";
dialog.showsResizeIndicator = true;
dialog.showsHiddenFiles = false;
dialog.canChooseDirectories = true;
dialog.canCreateDirectories = true;
dialog.allowsMultipleSelection = false;
dialog.allowedFileTypes = ["tif", "tiff"];
dialog.isFloatingPanel = true;
if (dialog.runModal() == NSApplication.ModalResponse.OK)
{
let result = dialog.url // Pathname of the file
if (result != nil)
{
let path = result!.path
print(path)
exit(0)
}
}
exit(1)
我怎样才能显示一个行为正常的 NSOpenPanel?即:可以获得焦点,可以与鼠标和键盘进行交互,...
在此上下文中(没有 window 的应用程序)您需要将 NSApplication 激活策略设置为 .accessory
以激活面板(还有 .regular
但它会显示 Dock图标和菜单栏)。
import AppKit
NSApplication.shared.setActivationPolicy(.accessory)
let dialog = NSOpenPanel()
dialog.title = "Choose a .tif file or a folder"
dialog.showsResizeIndicator = true
dialog.showsHiddenFiles = false
dialog.canChooseDirectories = true
dialog.canCreateDirectories = true
dialog.allowsMultipleSelection = false
dialog.allowedFileTypes = ["tif", "tiff"]
dialog.isFloatingPanel = true
if (dialog.runModal() == NSApplication.ModalResponse.OK)
{
let result = dialog.url // Pathname of the file
if (result != nil)
{
let path = result!.path
print(path)
exit(0)
}
}
exit(1)
首先,我是 Swift 的新手,如果我的问题看起来微不足道,我很抱歉。
我想要一个非常简单的命令行程序,它可以打开一个对话框来选择文件或文件夹。此工具不能 运行 带有图标在 Dock 中跳动的实际完整应用程序,而是一些微妙的东西。而已。我所做的正是产生了这个,除了面板无法获得焦点。当我点击面板时,它保持灰色。有趣的是,可以单击按钮或拖放文件,但无法浏览文件系统。也没有捕获键盘事件。
import AppKit
let dialog = NSOpenPanel()
dialog.title = "Choose a .tif file or a folder";
dialog.showsResizeIndicator = true;
dialog.showsHiddenFiles = false;
dialog.canChooseDirectories = true;
dialog.canCreateDirectories = true;
dialog.allowsMultipleSelection = false;
dialog.allowedFileTypes = ["tif", "tiff"];
dialog.isFloatingPanel = true;
if (dialog.runModal() == NSApplication.ModalResponse.OK)
{
let result = dialog.url // Pathname of the file
if (result != nil)
{
let path = result!.path
print(path)
exit(0)
}
}
exit(1)
我怎样才能显示一个行为正常的 NSOpenPanel?即:可以获得焦点,可以与鼠标和键盘进行交互,...
在此上下文中(没有 window 的应用程序)您需要将 NSApplication 激活策略设置为 .accessory
以激活面板(还有 .regular
但它会显示 Dock图标和菜单栏)。
import AppKit
NSApplication.shared.setActivationPolicy(.accessory)
let dialog = NSOpenPanel()
dialog.title = "Choose a .tif file or a folder"
dialog.showsResizeIndicator = true
dialog.showsHiddenFiles = false
dialog.canChooseDirectories = true
dialog.canCreateDirectories = true
dialog.allowsMultipleSelection = false
dialog.allowedFileTypes = ["tif", "tiff"]
dialog.isFloatingPanel = true
if (dialog.runModal() == NSApplication.ModalResponse.OK)
{
let result = dialog.url // Pathname of the file
if (result != nil)
{
let path = result!.path
print(path)
exit(0)
}
}
exit(1)