Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
NSURL category that provides a simple way to append a fileURL to the end of favorite items or favorite volumes list used in the side bars seen in Finder and the open and save dialog boxes.
//
// NSURL+FavoritesList.h
// FavoritesListTester
//
// Appends the URL to the end of the Favorite Items or Favorite Volumes list
// (found in the side bar of Finder windows and open/save dialogs, etc.)
//
// Notes:
//
// 1. Must be a file URL.
// 2. Appends only (no insert at top for simplicity).
// 3. Compatible back to Mac OS X 10.5.
// 4. Due to Finder bugs over the years, this category doesn't bother
// allowing for setting custom icons/names; it just uses the default Finder
// icon and file name for simplicity.
//
// Created by Joshua Nozzi on 6/23/15.
// http://joshua.nozzi.name/2015/06/23/nsurl-category/
// https://gist.github.com/jnozzi/817f3276c55dbb7025be
//
// Copyright (c) 2015 Joshua Nozzi. All rights reserved.
// Released under BSD license (this notice must be retained if redistributed).
// No guarantees made. Use at your own risk.
//
#import <Foundation/Foundation.h>
@interface NSURL (FavoritesList)
- (BOOL)appendToFavoriteItemsList;
- (BOOL)appendToFavoriteVolumesList;
@end
//
// NSURL+FavoritesList.m
//
// Appends the URL to the end of the Favorite Items or Favorite Volumes list
// (found in the side bar of Finder windows and open/save dialogs, etc.)
//
// Notes:
//
// 1. Must be a file URL.
// 2. Appends only (no insert at top for simplicity).
// 3. Compatible back to Mac OS X 10.5.
// 4. Due to Finder bugs over the years, this category doesn't bother
// allowing for setting custom icons/names; it just uses the default Finder
// icon and file name for simplicity.
//
// Created by Joshua Nozzi on 6/23/15.
// http://joshua.nozzi.name/2015/06/23/nsurl-category/
// https://gist.github.com/jnozzi/817f3276c55dbb7025be
//
// Copyright (c) 2015 Joshua Nozzi. All rights reserved.
// Released under BSD license (this notice must be retained if redistributed).
// No guarantees made. Use at your own risk.
//
#import "NSURL+FavoritesList.h"
@implementation NSURL (FavoritesList)
- (BOOL)appendToFavoriteItemsList {
return [self appendToList:kLSSharedFileListFavoriteItems];
}
- (BOOL)appendToFavoriteVolumesList {
return [self appendToList:kLSSharedFileListFavoriteVolumes];
}
- (BOOL)appendToList:(CFStringRef)list {
// Pessimism ...
BOOL result = NO;
// Do we have a file URL?
if (self.isFileURL) {
// Ask CoreServices for the favorite items list
LSSharedFileListRef listRef = LSSharedFileListCreate(NULL, list, NULL);
if (listRef) {
// We've got the list, so try to append our item
LSSharedFileListItemRef itemRef = LSSharedFileListInsertItemURL(listRef,
kLSSharedFileListItemLast,
NULL,
NULL,
(__bridge CFURLRef)self,
NULL,
NULL);
// Did it work?
if (itemRef) {
// Release the item and flag success
CFRelease(itemRef);
result = YES;
}
// Release the list
CFRelease(listRef);
}
}
return result;
}
@end
@pfvernon2

This comment has been minimized.

Copy link

commented Nov 17, 2015

LSSharedFileListCreate, etc. appear to be deprecated starting with OS X 10.11. Any idea what the replacement is?

@almccann

This comment has been minimized.

Copy link

commented Apr 19, 2016

It looks to me like the idea is to replace this type of access with "Finder Sync".
https://developer.apple.com/library/mac/documentation/General/Conceptual/ExtensibilityPG/Finder.html

Although I haven't found in there yet how to append to the favourites sidebar.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.