将项添加到Finder侧栏

时间:2011-01-31 15:16:35

标签: macos cocoa plist macos-carbon finder

我想在Finder侧边栏中添加一个新项目。我发现Finder将“地点”列表保存在`〜/ Library / Preferences / com.apple.sidebarlists.plist中。我能够使用Carbon API读取文件,并看到每个项目都有Name,icon和别名。

使用第三方应用程序(如PlistEdit Pro)我可以更新别名。我的问题是如何使用Carbon API更新别名。无法找到创建将在Finder中打开的别名的方法。似乎Dropbox和PlistEditor Pro都能找到方法。

3 个答案:

答案 0 :(得分:6)

看看here

  

共享文件列表API是新手   在Mac OS X Leopard中启动服务。   此API提供对多个的访问   各种系统 - 全球和每用户   持久的文件系统列表   对象,例如最近的文档和   应用程序,收藏夹和登录   项目。有关详细信息,请参阅新的   接口文件LSSharedFileList.h。

您想要查找键kLSSharedFileListFavoriteItems,它处理补充工具栏中“位置”下的项目。 我猜你可以尝试做类似于this的事情,使用LSSharedFileListCreate来创建kLSSharedFileListFavoriteItems。

或者您可以使用贴出here的AppleScript,这会更容易,但不是“正确的方式”©

答案 1 :(得分:3)

2015年更新

LSSharedFileList标题表示已移至CoreServices框架。实际上,如果您使用Cmd-Shift-O(在Xcode中)并键入LSSharedFileList,然后导航到唯一的结果,您将在跳转栏中看到标题确实现在包含在CoreServices.framework中。在任何情况下,密钥仍为kLSSharedFileListFavoriteItems

示例:

+ (BOOL)appendFavoriteItemWithURL:(NSURL *)url {

  // Pessimism ...
  BOOL result = NO;

  // Do we have a file URL?
  if (url.isFileURL) {

    // Ask CoreServices for the favorite items list 
    // (kLSSharedFileListFavoriteItems)
    LSSharedFileListRef list = LSSharedFileListCreate(NULL, kLSSharedFileListFavoriteItems, NULL);
    if (list) {

      // We've got the list, so try to append our item
      // (use kLSSharedFileListItemBeforeFirst vs. 
      // kLSSharedFileListItemLast if desired)
      LSSharedFileListItemRef item = LSSharedFileListInsertItemURL(list,
                                     kLSSharedFileListItemLast,
                                     NULL,
                                     NULL,
                                     (__bridge CFURLRef)url,
                                     NULL,
                                     NULL);

      // Did it work?
      if (item) {

        // Release the item and flag success
        CFRelease(item);
        result = YES;

      }

      // Release the list
      CFRelease(list);

    }

  }

  return result;
}

用法:

// Create the path to the favorite item to add
NSString * itemPath = [@"~/Music" stringByExpandingTildeInPath];
NSURL * itemURL = [NSURL fileURLWithPath:itemPath];

// Insert the item
[WhateverClassTheAboveFunctionIsIn appendFavoriteItemWithURL:itemURL];

答案 2 :(得分:0)

@Asmus:默认情况下'命令+ T'是在finder中向侧栏添加文件夹的快捷方式。当你用键盘快捷键'命令+ T'手动分配给其他任务。

  

如果在设置'命令+ T'之后执行,则AppleScript会失败。作为在osx lion(10.7)中显示我的其他桌面的快捷键

相关问题