6

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

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

4

3 回答 3

6

看看这里

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

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

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

于 2011-01-31T16:43:29.527 回答
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];
于 2015-06-23T15:37:30.923 回答
0

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

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

于 2012-05-24T11:13:06.337 回答