Skip to content

Instantly share code, notes, and snippets.

@mminer
Last active October 18, 2017 04:49
Show Gist options
  • Save mminer/13d9307749174b5862e1d9a025358dfe to your computer and use it in GitHub Desktop.
Save mminer/13d9307749174b5862e1d9a025358dfe to your computer and use it in GitHub Desktop.
Adds a folder to the Finder sidebar.
import Foundation
func addToSidebar(path: String) {
guard
let listType = kLSSharedFileListFavoriteItems?.takeUnretainedValue(),
let itemList = LSSharedFileListCreate(nil, listType, nil)?.takeUnretainedValue(),
let items = LSSharedFileListCopySnapshot(itemList, nil)?.takeUnretainedValue()
else {
return
}
let lastIndex = CFArrayGetCount(items) - 1
let lastItemPointer = CFArrayGetValueAtIndex(items, lastIndex)
let lastItem = unsafeBitCast(lastItemPointer, to: LSSharedFileListItem.self)
let url = NSURL(fileURLWithPath: path, isDirectory: true)
LSSharedFileListInsertItemURL(itemList, lastItem, nil, nil, url, nil, nil)
}
@mminer
Copy link
Author

mminer commented Oct 18, 2017

Most of these functions are deprecated and display this warning:

'LSSharedFileListCopySnapshot' was deprecated in OS X 10.11: This functionality is no longer supported on OS X.

So this is no longer a good idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment