Objc scriptingbridge如何在特定文件夹中打开新的Finder窗口

时间:2014-07-03 04:26:27

标签: objective-c cocoa finder scripting-bridge

我想在特定位置打开特定尺寸的新Finder窗口并显示特定文件夹。

在ScriptingBridge(XCode 6,Mavericks 9.4)中,我可以打开一个新的Finder窗口并找出如何读取它打开的文件夹的URL。但我真的很挣扎如何将文件夹设置为不同的东西。

我试图为窗口分配一个初始化为'URL'属性的'目标'SBObject。 但URL属性是readonly虽然'target'似乎不是。我使用'get'来确保有一个FinderFolder对象和一个FinderFinderWindow对象。

我尝试使用带有URL密钥的属性字典并创建“文件夹”。

FinderFolder *folder = [[[_finder classForScriptingClass:@"folder"] alloc]
    initWithProperties:dict];
[[_finder folders] addObject:folder]; 

我已阅读Apple文档并查找了全部示例,但找不到某人分配文件夹路径的实例。

2 个答案:

答案 0 :(得分:0)

// open folder "Documents" of home folder
FinderFolder *theHomeFolder = [theFinder home];
NSLog(@"theHomeFolder: %@",theHomeFolder);

SBElementArray * theHomeFolderFolders = [theHomeFolder folders];
NSLog(@"theHomeFolderFolders: %@",theHomeFolderFolders);

FinderFolder *theDocsFolder = [theHomeFolderFolders objectWithName:@"Documents"];
NSLog(@"theDocsFolder: %@",theDocsFolder);

[theDocsFolder openUsing:nil withProperties:nil];
FinderFinderWindow * docsFolderWindow = (FinderFinderWindow*) [theDocsFolder containerWindow];
NSLog(@"docsFolderWindow: %@",docsFolderWindow);

docsFolderWindow = [docsFolderWindow get];
NSLog(@"docsFolderWindow: %@",docsFolderWindow);

// change its bounds
docsFolderWindow.bounds = NSMakeRect(64., 64., 800, 600.);

// other fun stuff…
[docsFolderWindow setCurrentView:FinderEcvwListView];
[[docsFolderWindow iconViewOptions] setArrangement:FinderEarrArrangedByName];
[docsFolderWindow cleanUpBy:@selector(name)];

docsFolderWindow.toolbarVisible = NO;
docsFolderWindow.statusbarVisible = NO;
docsFolderWindow.sidebarWidth = 0;

答案 1 :(得分:0)

有几种不同的方式。另一个答案有效,或者您可以显式创建一个新的Finder窗口并设置其target属性:

NSURL *u = [NSURL fileURLWithPath:@"/tmp"];
FinderFinderWindow *w = [[[theFinder classForScriptingClass:@"Finder window] alloc] init];
[[theFinder finderWindows] addObject:w];
[w setTarget:u];

您也可以将其设置为引用Finder项目的SBObject,例如来自其他答案的theDocsFolder