将项目添加到 Finder 侧边栏

发布于 2024-10-14 21:09:47 字数 278 浏览 5 评论 0原文

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

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

I would like to add a new item to the Finder sidebar. I found out that the Finder keeps the list of "places" in `~/Library/Preferences/com.apple.sidebarlists.plist. I was able to read the file using Carbon API and saw that each item had Name, icon and alias.

Using a 3rd party application such as PlistEdit Pro I was able to update the alias. My question is how to update the alias using Carbon API. Was not able to find a way to create alias that will open in Finder. It seem that both Dropbox and PlistEditor Pro was able to find the way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

老子叫无熙 2024-10-21 21:09:47

看看此处

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

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

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

Take a look here:

The Shared File List API is new to
Launch Services in Mac OS X Leopard.
This API provides access to several
kinds of system-global and per-user
persistent lists of file system
objects, such as recent documents and
applications, favorites, and login
items. For details, see the new
interface file LSSharedFileList.h.

You want to look for the key kLSSharedFileListFavoriteItems, which handles the items under "Places" in the Sidebar.
I guess you could try to do something similar to this, using LSSharedFileListCreate to create kLSSharedFileListFavoriteItems.

Or you could use the applescript posted here, which would be way easier, but not the "Right Way"©

没︽人懂的悲伤 2024-10-21 21:09:47

2015 年更新

LSSharedFileList 标头表示该内容已移至 CoreServices 框架。事实上,如果您按 Cmd-Shift-O(在 Xcode 中)并输入 LSSharedFileList,然后导航到唯一的结果,您将在跳转栏中看到标头现在确实包含在 CoreServices.framework。无论如何,关键仍然是kLSSharedFileListFavouriteItems

示例:

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

Update for 2015

The LSSharedFileList header says this has moved to the CoreServices framework. In fact, if you Cmd-Shift-O (in Xcode) and type LSSharedFileList, then navigate to the only result, you'll see in the jump bar that the header is indeed now contained within CoreServices.framework. In any case, the key is still kLSSharedFileListFavoriteItems.

Example:

+ (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;
}

Usage:

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

// Insert the item
[WhateverClassTheAboveFunctionIsIn appendFavoriteItemWithURL:itemURL];
执妄 2024-10-21 21:09:47

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

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

更新 2023: 由于 Finder 和其他应用程序中选项卡的可用性,< code>command + T 创建一个新选项卡。新的默认快捷键是command + ctrl + T

@Asmus : By default 'command + T' is the shortcut to add a folder to sidebar in finder. Applescript pointed by u is working fine when keyboard shortcut key 'command + T' is assigned manually to other tasks.

The applescript fails if executed after setting 'command + T' as the shortcut key to show my other desktop in osx lion(10.7)

Update 2023: Due to the availability of Tabs in Finder and other applications, command + T creates a new Tab. The new default shortcut is command + ctrl + T!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文