2015-10-15 25 views
0

在我用過的幾乎任何桌面應用程序中,可以使用看起來像Finder一樣的標準文件導航器來選擇打開或保存文件的路徑。例如,如果我去保存這個網頁, this menu comes up,我可以選擇保存它的位置,創建一個新文件夾等等。是否有系統調用來從我自己的程序啓動這個菜單,並且可能返回一個路徑到選定的文件或文件夾?如何在我自己的程序中使用Mac文件選擇菜單?

回答

0

看一看NSOpenPanel

這不正是你彷彿在尋找:

的NSOpenPanel類爲可可的用戶界面開啓面板。應用程序使用「打開」面板作爲一種便捷的方式來查詢用戶要打開的文件的名稱。

+0

這看起來很有希望。例如,是否可以從Python進行系統調用? – Jangell

+0

沒關係,明白了!我發佈了下面的代碼。 [Here](http://nullege.com/codes/show/[email protected]@[email protected]@[email protected]@native_widgets.py/233/AppKit.NSOpenPanel)就是一個例子。 – Jangell

0

如果其他人看到這個,JohannesWeiß的回答是正確的: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

相關問題