2012-04-06 51 views
4

有沒有辦法通過編程的方式在我的應用程序中打開Finder的「獲取信息」窗口?如何打開文件/目錄獲取信息窗口?

Get Info Window

+2

只是要清楚:你要問切換到搜索和顯示實際Info面板,或者是你想在你的應用中顯示面板? – 2012-04-06 19:23:42

+0

我想在我的應用程序中顯示面板 – Kira 2012-04-06 19:26:47

回答

1

使用一些AppleScript的,它是很容易的:

set macpath to POSIX file "/Users/rross/test.applescript" as alias 
tell application "Finder" to open information window of macpath 
+0

有沒有一種方法可以讓它不帶applescript?)簡單地說,我從來沒有使用過applescript – Kira 2012-04-06 19:34:26

+0

這是否會顯示面板內應用程序,因爲OP想要?我不是AppleScript的人,但是這不只是調用Finder中的面板? – 2012-04-06 19:34:30

+0

@ConradShultz嗯我開始我的回答之前,原來的帖子上添加評論。而且,Kira,applescript很容易處理,做一些谷歌搜索,你應該能夠找到如何在自己的應用程序中執行applescript。 – 2012-04-06 19:35:49

1

AFAIK沒有API來獲取信息面板中的應用內展示。 (我歡迎這方面的更正。)想到的最接近的是通過快速瀏覽API提供的preview panel

我認爲您需要構建自己的所有信息都可以通過NSWorkspaceNSFileManager類獲得。

+1

當然,我可以讓我的窗戶,但我不想「重新發明輪子」,如果這已經對我做了 – Kira 2012-04-06 19:39:30

+0

在這種情況下,我認爲你可能不得不。我敢打賭,你並不孤單 - 也許可以考慮將你的結果發佈到GitHub上? – 2012-04-06 19:42:05

6

還有一個簡單的解決方案,你可以在蘋果的「照片搜索」項目中看到。

以下是可用於根據示例顯示單個文件的「獲取信息」窗口的代碼。

- (void)infoButtonAction:(NSOutlineView *)sender { 
    // Access the row that was clicked on and open that image 
    NSInteger row = [sender clickedRow]; 
    SearchItem *item = [resultsOutlineView itemAtRow:row]; 
    // Do a "reveal" in finder 
    if ([item filePathURL]) { 
     NSPasteboard *pboard = [NSPasteboard pasteboardWithUniqueName]; 
     [pboard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil]; 
     [pboard setString:[[item filePathURL] path] forType:NSStringPboardType]; 
     NSPerformService(@"Finder/Show Info", pboard); 
    } 
} 

我進一步修改了代碼按我的需要,以示對多個文件的對話框如下:

NSPasteboard *pboard = [NSPasteboard pasteboardWithUniqueName]; 
[pboard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil]; 

NSMutableArray *fileList = [NSMutableArray new]; 

//Add as many as file's path in the fileList array 
for(FileItem *item in fileItems) { 
    [fileList addObject:[[item.filePath filePathURL] path]]; 
} 

[pboard setPropertyList:fileList forType:NSFilenamesPboardType]; 
NSPerformService(@"Finder/Show Info", pboard); 

希望,這將幫助,並通知你,這將與沙箱應用程序在工作獅子和後來。

+0

您使用剪貼板來存儲文件?但是當用戶點擊CMD V時,也會粘貼我們請求的「Get Info」文件? 如果是這樣,在獲取信息之後從文件夾中刪除文件(也出於內存原因?) – 2012-11-19 19:07:03

+0

當然,在NSPerformService調用某些延遲之後,您可以根據需要從剪貼板中刪除文件。 – AmitSri 2012-11-22 11:59:43

1

我用這個代碼開口一個文件 「獲取信息」 窗口

NSPasteboard *pboard = [NSPasteboard pasteboardWithUniqueName]; 
 
    [pboard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil]; 
 
    NSString *path = [selectedDuplicateItem getFileItemPath]; 
 
    [pboard setString:path forType:NSStringPboardType]; 
 
    NSPerformService(@"Finder/Show Info", pboard);

但是,有一些bug。如果我的文件路徑包含一個空格,例如path = @「/ Users/alexanderyolkin/Downloads/DETest/Folder/LICENSE 2」,NSPerformService會打開兩個窗口「Get Info」 - 用於路徑和相同文件,不包含空格或文件夾。

因此,解決方案使用的是 [pboard setPropertyList:fileList forType:NSFilenamesPboardType];

而且代碼

NSPasteboard *pboard = [NSPasteboard pasteboardWithUniqueName]; 
 
    [pboard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil]; 
 
    
 
    NSString *path = [selectedDuplicateItem getFileItemPath]; 
 
    NSMutableArray *fileList = [NSMutableArray new]; 
 
    [fileList insertObject:path atIndex:0]; 
 
    
 
    [pboard setPropertyList:fileList forType:NSFilenamesPboardType]; 
 
    NSPerformService(@"Finder/Show Info", pboard);

多數民衆贊成在工作完美

相關問題