gpt4 book ai didi

objective-c - LSSharedFileListInsertItemURL 不更改名称

转载 作者:搜寻专家 更新时间:2023-10-30 20:25:22 26 4
gpt4 key购买 nike

我遇到了 LSSharedFileListInsertItemURL 的问题。我正在尝试将一个项目添加到 Finder 侧边栏,效果很好。它唯一不会做的是更改侧边栏中项目的名称。我将“FolderName”作为参数推送,但在运行此函数后,该项目未重命名。它确实会以名称闪烁一秒钟,但很快就会变回其实际名称。我已经尽我所能寻找解决方案,但一无所获。如果有人发现我的代码有问题或有“hack”来使它正常工作,请告诉我。

-(void) addPathToSharedItem:(NSString *)path
{

CFURLRef url = (__bridge CFURLRef)[NSURL fileURLWithPath:path];

// Create a reference to the shared file list.
LSSharedFileListRef favoriteItems = LSSharedFileListCreate(NULL, kLSSharedFileListFavoriteItems, NULL);

if (favoriteItems) {

//Insert an item to the list.
CFStringRef mdcName = CFSTR("FolderName");

LSSharedFileListItemRef item = LSSharedFileListInsertItemURL(favoriteItems, kLSSharedFileListItemLast, mdcName, NULL, url, NULL, NULL);

if (item){

CFRelease(item);
}
}

CFRelease(favoriteItems);
}

最佳答案

[我知道很久以前有人问过这个问题,但似乎在其他地方找不到合适的答案。]

Finder 在 LSSharedFileListInsertItemURL 之后不刷新收藏夹名称实际上是一个已知错误 reported to Apple in 2013 .

我们发现将任何其他文件夹手动添加到收藏夹会刷新收藏夹部分并显示之前通过 LSSharedFileListInsertItemURL 设置的正确名称。

一个非常肮脏的解决方法是通过插入任何其他项目然后立即删除它来自动执行此操作。

下面的代码(抱歉,在 C++ 中,但它可以很容易地移植到 Objective C 中)实现了这一点:

// Create a reference to the shared file list.
LSSharedFileListRef favoriteItems = LSSharedFileListCreate(NULL, kLSSharedFileListFavoriteItems, NULL);
if (!favoriteItems)
return false;

//Insert an item to the list.
LSSharedFileListItemRef item = LSSharedFileListInsertItemURL( favoriteItems, //Insert in this list
kLSSharedFileListItemBeforeFirst, //Here
(CFStringRef) shortCurtNameNS, //Shortcut name
NULL, //Icon
url, //URL / path
NULL,
NULL);
if (item)
CFRelease(item);


// Here it goes dark. Really dark.
// Finder does not refresh Favorites until another one is inserted "manually".
// The following lines just emulates this : insert another item then immediately remove it. This will refresh favs.
// KarmaPoints--;
CFURLRef dummy = (__bridge CFURLRef)[NSURL fileURLWithPath:@"/"];
NSString * dummyName = [NSString stringWithCString:"Root" encoding:[NSString defaultCStringEncoding]];
LSSharedFileListItemRef dummyItem = LSSharedFileListInsertItemURL( favoriteItems, //Insert in this list
kLSSharedFileListItemLast, //Here
(CFStringRef) dummyName, //Shortcut name
NULL, //Icon
dummy, //URL / path
NULL,
NULL);
// Remove it
LSSharedFileListItemRemove(favoriteItems, dummyItem);
if (dummyItem)
CFRelease(dummyItem);

关于objective-c - LSSharedFileListInsertItemURL 不更改名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16044895/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com