简体   繁体   中英

How to open file/directory Get Info window?

Is there a way to open the Finder's "Get Info" window within my app for some path, programmatically?

获取信息窗口

There is another simple solution, you can see in the apple's "Photo Search" project.

Following is the code which you can use to show "Get Info" Window for single file as per the sample.

- (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);
    }
}

I have further modified the code as per my need to show the dialog for multiple files as follows:

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);

Hope, this will help, and FYI, this will work with sandboxed App in Lion and later.

I used this code for openings one file "Get Info" window

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);

but, there is some bug. If my file path contains a space, for example path = @"/Users/alexanderyolkin/Downloads/DETest/Folder/LICENSE 2" , NSPerformService opens two windows "Get Info" - for path and for the same file without space or for folder.

So, the solution was in using [pboard setPropertyList:fileList forType:NSFilenamesPboardType];

and the code is

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);

thats work perfect

Use some applescript and it's quite easy:

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

AFAIK there is no API to obtain an info panel for in-app display. (I welcome correction on this point.) The closest thing that comes to mind is the preview panel available through the Quick Look APIs.

I think that all the information you would need to construct your own can be obtained via the NSWorkspace and NSFileManager classes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM