Skip to content

Commit

Permalink
Merge pull request #1082 from cozy-labs/macos-favorite-python
Browse files Browse the repository at this point in the history
Bring back synced dir in Finder's favorite items on macOS
  • Loading branch information
sebn committed Jun 28, 2018
2 parents 3a8ef46 + b194334 commit 752ab11
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
3 changes: 3 additions & 0 deletions electron-builder.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ files:
- gui/main.js
- gui/node_modules
- gui/ports.js
- gui/scripts/**
- package.json
asarUnpack:
- gui/scripts/**
directories:
buildResources: gui/assets
win:
Expand Down
19 changes: 12 additions & 7 deletions gui/js/shortcut.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const lnk = require('lnk')
const os = require('os')
const path = require('path')
const url = require('url')
const childProcess = require('child_process')

const log = require('../../core/app').logger({
Expand All @@ -28,9 +27,16 @@ const execSync = (cmd) => {
log.debug(output)
}

const sfltoolAddFavorite = (path) => {
const item = url.resolve('file://', path)
execSync(`sfltool add-item com.apple.LSSharedFileList.FavoriteItems ${item}`)
function quotePath (path) {
return `"${path.replace(/"/g, '\\"')}"`
}

const macosAddFavoriteScript = path
.resolve(__dirname, '../scripts/macos-add-favorite.py')
.replace('/app.asar/', '/app.asar.unpacked/')

const macosAddFavorite = (path) => {
execSync(`/usr/bin/python ${quotePath(macosAddFavoriteScript)} ${quotePath(path)}`)
}

// For Darwin <=> macOS version mapping, see:
Expand All @@ -44,9 +50,8 @@ module.exports.addFileManagerShortcut = (config) => {
win10PinToHome(config.syncPath)
} else if (platform === 'win32' && major >= 6) {
winAddLink(config.syncPath)
} else if (platform === 'darwin' && major >= 15) {
// sfltool is available since 10.11 (El Capitan)
sfltoolAddFavorite(config.syncPath)
} else if (platform === 'darwin') {
macosAddFavorite(config.syncPath)
} else {
throw new Error(`Not registering shortcut on ${platform} ${major}`)
}
Expand Down
33 changes: 33 additions & 0 deletions gui/scripts/macos-add-favorite.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/python

from Cocoa import NSURL
from CoreFoundation import CFArrayGetCount, CFArrayGetValueAtIndex, kCFAllocatorDefault
from Foundation import NSBundle
from LaunchServices import kLSSharedFileListFavoriteItems
from objc import loadBundleFunctions
from sys import argv

# For some reason, these functions cannot be imported directly and must be
# manually loaded from the SharedFileList bundle
SFL_bundle = NSBundle.bundleWithIdentifier_('com.apple.coreservices.SharedFileList')
functions = [
('LSSharedFileListCreate', '^{OpaqueLSSharedFileListRef=}^{__CFAllocator=}^{__CFString=}@'),
('LSSharedFileListCopySnapshot', '^{__CFArray=}^{OpaqueLSSharedFileListRef=}o^I'),
('LSSharedFileListInsertItemURL', '^{OpaqueLSSharedFileListItemRef=}^{OpaqueLSSharedFileListRef=}^{OpaqueLSSharedFileListItemRef=}^{__CFString=}^{OpaqueIconRef=}^{__CFURL=}^{__CFDictionary=}^{__CFArray=}'),
('kLSSharedFileListItemBeforeFirst', '^{OpaqueLSSharedFileListItemRef=}'),
]
loadBundleFunctions(SFL_bundle, globals(), functions)

# The path to added to the Finder's favorites
path = argv[1]

# Make it an URL object (which is a valid favorite item)
item = NSURL.alloc().initFileURLWithPath_(path)

# Retrieve the favorite items list
favorite_items = LSSharedFileListCreate(kCFAllocatorDefault,
kLSSharedFileListFavoriteItems, None)

# Add the item to the top of the list
LSSharedFileListInsertItemURL(favorite_items, kLSSharedFileListItemBeforeFirst,
None, None, item, None, None)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "CozyDrive",
"productName": "Cozy Drive",
"private": true,
"version": "3.8.0-beta.1",
"version": "3.8.1-alpha.5",
"description": "Cozy Drive is a synchronization tool for your files and folders with Cozy Cloud.",
"homepage": "https://github.com/cozy-labs/cozy-desktop",
"author": "Cozy Cloud <contact@cozycloud.cc> (https://cozycloud.cc/)",
Expand Down

0 comments on commit 752ab11

Please sign in to comment.