我能够找到一些对我有帮助的东西。默认情况下,工具栏项目不会添加到查找器窗口,除非用户将其拖入。我无法找到以编程方式将项目添加到查找器窗口工具栏的方法。
将项目添加到查找器侧栏
// Create a reference to the shared file list.
LSSharedFileListRef favoriteItems = LSSharedFileListCreate(NULL, kLSSharedFileListFavoriteItems, NULL);
// Check Items
if (favoriteItems)
{
// Get CFURL for Application
CFURLRef url = (__bridge CFURLRef)[NSURL fileURLWithPath:path];
// Add Item
LSSharedFileListItemRef item = LSSharedFileListInsertItemURL(favoriteItems, kLSSharedFileListItemBeforeFirst, NULL, NULL, url, NULL, NULL);
// Release
if (item)
CFRelease(item);
}
// Release
if (favoriteItems != NULL)
CFRelease(favoriteItems);
从侧边栏中删除项目的代码
// Create a reference to the shared file list.
LSSharedFileListRef favoriteItems = LSSharedFileListCreate(NULL, kLSSharedFileListFavoriteItems, NULL);
// Check Items
if (favoriteItems)
{
// Get Login Items
CFArrayRef favoriteItemsArray = LSSharedFileListCopySnapshot(favoriteItems, NULL);
// Loop Through Items
for (id item in (__bridge NSArray *)favoriteItemsArray)
{
// Get Item Ref
LSSharedFileListItemRef itemRef = (__bridge LSSharedFileListItemRef)item;
// Get Item URL
CFURLRef itemURL = LSSharedFileListItemCopyResolvedURL(itemRef, kLSSharedFileListNoUserInteraction | kLSSharedFileListDoNotMountVolumes, NULL);
if (itemURL != NULL)
{
// If Item Matches Remove It
if ([[(__bridge NSURL *)itemURL path] hasPrefix:path])
LSSharedFileListItemRemove(favoriteItems, itemRef);
// Release
if (itemURL != NULL)
CFRelease(itemURL);
}
}
// Release
if (favoriteItemsArray != NULL)
CFRelease(favoriteItemsArray);
}
// Release
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];
启用扩展的代码(捆绑 ID)
system("pluginkit -e use -i com.mycompany.finderExt")
禁用扩展的代码(捆绑 ID)
system("pluginkit -e ignore -i com.mycompany.finderExt")