Cập nhật cho 2015
Tiêu đề LSSharedFileList
cho biết điều này đã chuyển sang khuôn khổ CoreServices. Trong thực tế, nếu bạn Cmd-Shift-O (trong Xcode) và nhập LSSharedFileList, sau đó điều hướng đến kết quả duy nhất, bạn sẽ thấy trong thanh nhảy mà tiêu đề thực sự bây giờ chứa trong CoreServices.framework
. Trong mọi trường hợp, khóa vẫn là kLSSharedFileListFavoriteItems
.
Ví dụ:
+ (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;
}
Cách sử dụng:
// Create the path to the favorite item to add
NSString * itemPath = [@"~/Music" stringByExpandingTildeInPath];
NSURL * itemURL = [NSURL fileURLWithPath:itemPath];
// Insert the item
[WhateverClassTheAboveFunctionIsIn appendFavoriteItemWithURL:itemURL];
Nguồn
2015-06-23 15:37:30
Cách AppleScript làm việc 4 tôi. thx – amitp
@amitp Tuyệt vời! Tôi đoán có thể chọn câu trả lời của tôi như là giải pháp sau đó! :-) – Asmus