Skip to content

Instantly share code, notes, and snippets.

@jnozzi
Last active July 11, 2016 04:09
Show Gist options
  • Save jnozzi/817f3276c55dbb7025be to your computer and use it in GitHub Desktop.
Save jnozzi/817f3276c55dbb7025be to your computer and use it in GitHub Desktop.
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
Copy link

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

@supernoveau
Copy link

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