我找到了一些有用的东西。默认情况下,除非用户拖动它,否则工具栏项目不会添加到finder窗口中。我无法找到一种以编程方式将该项目添加到finder窗口工具栏的方法。
将项目添加到finder侧边栏
LSSharedFileListRef favoriteItems = LSSharedFileListCreate(NULL, kLSSharedFileListFavoriteItems, NULL);
if (favoriteItems)
{
CFURLRef url = (__bridge CFURLRef)[NSURL fileURLWithPath:path];
LSSharedFileListItemRef item = LSSharedFileListInsertItemURL(favoriteItems, kLSSharedFileListItemBeforeFirst, NULL, NULL, url, NULL, NULL);
if (item)
CFRelease(item);
}
if (favoriteItems != NULL)
CFRelease(favoriteItems);
从侧边栏中移除项目的代码
LSSharedFileListRef favoriteItems = LSSharedFileListCreate(NULL, kLSSharedFileListFavoriteItems, NULL);
if (favoriteItems)
{
CFArrayRef favoriteItemsArray = LSSharedFileListCopySnapshot(favoriteItems, NULL);
for (id item in (__bridge NSArray *)favoriteItemsArray)
{
LSSharedFileListItemRef itemRef = (__bridge LSSharedFileListItemRef)item;
CFURLRef itemURL = LSSharedFileListItemCopyResolvedURL(itemRef, kLSSharedFileListNoUserInteraction | kLSSharedFileListDoNotMountVolumes, NULL);
if (itemURL != NULL)
{
if ([[(__bridge NSURL *)itemURL path] hasPrefix:path])
LSSharedFileListItemRemove(favoriteItems, itemRef);
if (itemURL != NULL)
CFRelease(itemURL);
}
}
if (favoriteItemsArray != NULL)
CFRelease(favoriteItemsArray);
}
if (favoriteItems != NULL)
CFRelease(favoriteItems);
刷新Finder中的目录
// Reload Finder (change the word directory to file if updating file)
NSAppleScript * update = [[NSAppleScript alloc] initWithSource:[NSString stringWithFormat:@"tell application \"Finder\" to update POSIX directory \"%@\"",path]];
[update executeAndReturnError:nil];
启用扩展(bundle ID)的代码
system("pluginkit -e use -i com.mycompany.finderExt")
禁用扩展程序的代码(bundle ID)
system("pluginkit -e ignore -i com.mycompany.finderExt")