プログラムで、アプリ内でFinderの[情報を見る]ウィンドウを開いてパスを確認する方法はありますか?
4 に答える
もう1つの簡単な解決策があります。これは、アップルの「写真検索」プロジェクトで確認できます。
以下は、サンプルに従って単一ファイルの「情報を見る」ウィンドウを表示するために使用できるコードです。
- (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);
これがお役に立てば幸いです。参考までに、これはLion以降のサンドボックス化されたアプリで機能します。
このコードを使用して、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);
しかし、いくつかのバグがあります。たとえば、ファイルパスにスペースが含まれている場合path = @"/Users/alexanderyolkin/Downloads/DETest/Folder/LICENSE 2"
、NSPerformServiceは、パス用とスペースなしの同じファイル用またはフォルダ用の2つのウィンドウ「GetInfo」を開きます。
だから、解決策は使用することでした
[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);
それは完璧に動作します
いくつかのapplescriptを使用すると、非常に簡単です。
set macpath to POSIX file "/Users/rross/test.applescript" as alias
tell application "Finder" to open information window of macpath
AFAIKには、アプリ内表示用の情報パネルを取得するためのAPIはありません。(この点についての訂正を歓迎します。)頭に浮かぶ最も近いものは、 QuickLookAPIを介して利用できるプレビューパネルです。
独自に構築するために必要なすべての情報は、NSWorkspaceクラスとNSFileManagerクラスを介して取得できると思います。