【问题标题】:Add Item to Finder Sidebar将项目添加到 Finder 侧边栏
【发布时间】:2011-06-18 15:50:39
【问题描述】:

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

使用 PlistEdit Pro 等第 3 方应用程序,我能够更新别名。我的问题是如何使用 Carbon API 更新别名。无法找到创建将在 Finder 中打开的别名的方法。看来 Dropbox 和 PlistEditor Pro 都能找到方法。

【问题讨论】:

    标签: macos cocoa plist macos-carbon finder


    【解决方案1】:

    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];
    

    【讨论】:

    • 我创建了这个 Gist,将这个功能包装到一个简单的 NSURL 类别中:gist.github.com/jnozzi/817f3276c55dbb7025be
    • 赞成,我喜欢这个解决方案,但遗憾的是其中一些功能现在在 10.11 上已弃用。知道改用什么吗?
    • 暂时没有,本。不推荐使用的方法是否在标头中注释?他们可能会提出替代方案。
    【解决方案2】:

    @Asmus : 默认情况下,'command + T' 是在 finder 中将文件夹添加到侧边栏的快捷方式。当手动将键盘快捷键“command + T”分配给其他任务时,您指向的Applescript工作正常。

    如果在 osx lion(10.7) 中将“command + T”设置为显示我的另一个桌面的快捷键后执行,applescript 将失败

    【讨论】:

      【解决方案3】:

      看看here:

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

      您要查找键 kLSSharedFileListFavoriteItems,它处理侧边栏中“Places”下的项目。 我想你可以尝试做类似this 的事情,使用 LSSharedFileListCreate 来创建 kLSSharedFileListFavoriteItems。

      或者你可以使用here 发布的applescript,这会更容易,但不是“正确的方式”©

      【讨论】:

      • @amitp 太好了!我想可以选择我的答案作为解决方案! :-)
      猜你喜欢
      • 2010-12-05
      • 2021-09-25
      • 2011-04-14
      • 2018-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-27
      • 2020-11-26
      相关资源
      最近更新 更多