首先,我是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?即:可以获取焦点,可以与鼠标和键盘进行交互,...
答案 0 :(得分:3)
在这种情况下(无窗口的应用程序),您需要将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)