lacona-api
Version:
Cross-platform API for use with lacona commands
1,089 lines (881 loc) • 33.4 kB
JavaScript
import _slicedToArray from 'babel-runtime/helpers/slicedToArray';
import _regeneratorRuntime from 'babel-runtime/regenerator';
import _asyncToGenerator from 'babel-runtime/helpers/asyncToGenerator';
import _typeof from 'babel-runtime/helpers/typeof';
/** @jsx createElement */
import _ from 'lodash';
import { map } from 'rxjs/operator/map';
import { Observable } from 'rxjs/Observable';
import { v4 as guid } from 'node-uuid';
import { exec } from 'child_process';
var demoConfig = {
webSearch: {
searchEngines: [{ name: 'Google', url: 'https://www.google.com/search?q=${query}' }, { name: 'Google Images', url: 'https://www.google.com/search?q=${query}&tbm=isch' }, { name: 'Google Maps', url: 'https://www.google.com/maps?q=${query}' }, { name: 'Gmail', url: 'https://www.google.com/search?q=${query}' }, { name: 'Google Mail', url: 'http://mail.google.com/mail/u/0/#search/${query}' }, { name: 'Google Inbox', url: 'https://inbox.google.com/search/${query}' }, { name: 'Google Drive', url: 'https://drive.google.com/drive/u/0/#search?q=${query}' }, { name: 'Maps', url: 'http://maps.apple.com/?q=${query}' }, { name: 'Twitter', url: 'https://twitter.com/search?q=${query}' }, { name: 'Facebook', url: 'https://www.facebook.com/search/results.php?q=${query}' }, { name: 'LinkedIn', url: 'https://www.linkedin.com/vsearch/p?keywords=${query}' }, { name: 'Youtube', url: 'https://www.youtube.com/results?search_query=${query}' }, { name: 'Wikipedia', url: 'https://wikipedia.org/wiki/Special:Search/${query}' }, { name: 'Amazon', url: 'http://www.amazon.com/s?url=search-alias=aps&field-keywords=${query}&tag=lacona-20' }, { name: 'eBay', url: 'http://shop.ebay.com/?_nkw=${query}' }, { name: 'IMDb', url: 'http://www.imdb.com/find?s=all&q=${query}' }, { name: 'Rotten Tomatoes', url: 'http://www.rottentomatoes.com/search/?search=${query}' }, { name: 'Bing', url: 'http://www.bing.com/search?q=${query}' }, { name: 'Yahoo', url: 'https://search.yahoo.com/search?p=${query}' }, { name: 'Ask', url: 'http://www.ask.com/web?q=${query}' }, { name: 'Flickr', url: 'https://www.flickr.com/search/?q=${query}&w=all' }, { name: 'Wolfram|Alpha', url: 'http://www.wolframalpha.com/input/?i=${query}' }, { name: 'Yubnub', url: 'http://www.yubnub.org/parser/parse?command=${query}' }, { name: 'DuckDuckGo', url: 'https://duckduckgo.com/?q=${query}' }]
},
applications: {
searchDirectories: [],
applications: []
}
};
var SEND_KEY = Symbol.for("io.lacona.lacona.api.send");
export function __set_send(send) {
global[SEND_KEY] = send;
}
function send(type) {
var data = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
return new Promise(function (resolve, reject) {
global[SEND_KEY](type, data, function (err, res) {
if (err) {
reject(err);
} else {
resolve(res);
}
});
});
}
export function isOSX() {
return process.platform === 'darwin' || !isDemo();
}
export function isDemo() {
return process.env.LACONA_ENV === 'demo';
}
/* Open */
function open(_ref) {
var url = _ref.url;
var path = _ref.path;
var applicationPath = _ref.applicationPath;
if (isOSX()) {
return send('OPEN', { url: url, path: path, applicationPath: applicationPath });
}
}
export function openURL(_ref2) {
var url = _ref2.url;
return open({ url: url });
}
export function openFile(_ref3) {
var path = _ref3.path;
return open({ path: path });
}
/* Events */
export function canAccessEvents() {
if (isOSX()) {
return send('CAN_ACCESS_EVENTS');
}
}
export function canAccessReminders() {
if (isOSX()) {
return send('CAN_ACCESS_REMINDERS');
}
}
export function createEvent(_ref4) {
var title = _ref4.title;
var start = _ref4.start;
var end = _ref4.end;
var allDay = _ref4.allDay;
var calendar = _ref4.calendar;
var location = _ref4.location;
if (isOSX()) {
return send('CREATE_EVENT', { title: title, start: start, end: end, allDay: allDay, calendar: calendar, location: location });
}
}
export function createReminder(_ref5) {
var title = _ref5.title;
var date = _ref5.date;
var reminderList = _ref5.reminderList;
if (isOSX()) {
return send('CREATE_REMINDER', { title: title, date: date, reminderList: reminderList });
}
}
export function fetchReminderLists() {
if (isOSX()) {
return send('FETCH_REMINDER_LISTS');
}
}
export function fetchCalendars() {
if (isOSX()) {
return send('FETCH_CALENDARS');
}
}
/* Notifications */
export function showNotification(_ref6) {
var title = _ref6.title;
var subtitle = _ref6.subtitle;
var content = _ref6.content;
if (isOSX()) {
return send('SHOW_NOTIFICATION', { title: title, subtitle: subtitle, content: content });
}
}
/* User Defaults */
// FIRST AVAILABLE IN 1.0.3
export function fetchUserDefaults(_ref7) {
var domain = _ref7.domain;
var key = _ref7.key;
if (isOSX()) {
return send('FETCH_USER_DEFAULTS', { domain: domain, key: key });
}
}
/* Clipboard */
export function fetchClipboard() {
if (isOSX()) {
return send('FETCH_CLIPBOARD');
}
}
export function setClipboard(_ref8) {
var text = _ref8.text;
if (isOSX()) {
return send('SET_CLIPBOARD', { text: text });
}
}
/* Applications */
export function watchApplications(_ref9) {
var directories = _ref9.directories;
var appPaths = _ref9.appPaths;
if (isDemo()) {
return new Observable(function (observer) {
observer.next(demoData.applications);
});
}
if (isOSX()) {
var _ret = function () {
var _context;
var tilde = userHome();
var trueDirectories = _.map(directories, function (dir) {
return dir.replace(/^~/, tilde);
});
var truePaths = _.map(appPaths, function (path) {
return path.replace(/^~/, tilde);
});
return {
v: (_context = watchSpotlight({
directories: trueDirectories,
query: "kMDItemContentTypeTree == 'com.apple.application'",
attributes: ['kMDItemDisplayName', 'kMDItemCFBundleIdentifier', 'kMDItemAlternateNames', 'kMDItemPath']
}), map).call(_context, function (data) {
return _.map(data, function (item) {
var alternativeNames = item.kMDItemAlternateNames || [];
if (item.kMDItemCFBundleIdentifier === 'com.google.Chrome') {
alternativeNames = ['Chrome'];
}
if (item.kMDItemCFBundleIdentifier === 'com.apple.iChat') {
alternativeNames = ['iMessage', 'iChat'];
}
return {
name: item.kMDItemDisplayName,
bundleId: item.kMDItemCFBundleIdentifier,
path: item.kMDItemPath,
alternativeNames: alternativeNames
};
});
})
};
}();
if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v;
}
}
export function openURLInApplication(_ref10) {
var url = _ref10.url;
var applicationPath = _ref10.applicationPath;
return open({ url: url, applicationPath: applicationPath });
}
export function openFileInApplication(_ref11) {
var path = _ref11.path;
var applicationPath = _ref11.applicationPath;
return open({ path: path, applicationPath: applicationPath });
}
export function fetchApplication(_ref12) {
var name = _ref12.name;
return send('FETCH_APPLICATION_INFO', { name: name });
}
/* Bookmarks */
export function watchBookmarks() {
if (isDemo()) {
return new Observable(function (observer) {
observer.next(demoData.bookmarks);
});
}
if (isOSX()) {
var _context2;
return (_context2 = watchSpotlight({
query: "kMDItemContentTypeTree = 'com.apple.safari.bookmark'",
attributes: ['kMDItemDisplayName', 'kMDItemURL', 'kMDItemPath']
}), map).call(_context2, function (data) {
return _.map(data, function (_ref13) {
var kMDItemDisplayName = _ref13.kMDItemDisplayName;
var kMDItemURL = _ref13.kMDItemURL;
var kMDItemPath = _ref13.kMDItemPath;
return {
name: kMDItemDisplayName,
url: kMDItemURL,
path: kMDItemPath
};
});
});
}
}
/* Contacts */
export function canAccessContacts() {
if (isOSX()) {
return send('CAN_ACCESS_CONTACTS');
}
}
export function fetchUserContact() {
if (isDemo()) {
return Promise.resolve(global.demoData.userContact);
}
if (isOSX()) {
return send('FETCH_USER_CONTACT');
}
}
export function fetchContacts() {
if (isDemo()) {
return Promise.resolve(global.demoData.contacts);
}
if (isOSX()) {
return send('FETCH_ALL_CONTACTS');
}
}
/* Files */
export var fetchFiles = function () {
var ref = _asyncToGenerator(_regeneratorRuntime.mark(function _callee(_ref14) {
var query = _ref14.query;
var escapedQuery, data;
return _regeneratorRuntime.wrap(function _callee$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
if (!isDemo()) {
_context3.next = 2;
break;
}
return _context3.abrupt('return', Promise.resolve(demoData.spotlightFiles));
case 2:
if (!isOSX()) {
_context3.next = 8;
break;
}
escapedQuery = _.chain(query.split('')).map(function (char) {
return char.replace('*', '\\*').replace('\\', '\\\\').replace('"', '\\"');
// .replace("'", "\\'")
}).join('*').value();
_context3.next = 6;
return fetchSpotlight({
query: 'kMDItemFSName LIKE[cd] "*' + escapedQuery + '*" AND ' + 'kMDItemSupportFileType != "MDSystemFile" AND ' + 'kMDItemContentTypeTree != "com.apple.application" AND ' + 'kMDItemContentTypeTree != "com.apple.application-bundle" AND ' + 'kMDItemContentTypeTree != "com.apple.safari.bookmark" AND ' + 'kMDItemContentTypeTree != "public.contact" AND ' + 'kMDItemContentTypeTree != "com.apple.addressbook.group" AND ' + 'kMDItemContentTypeTree != "com.apple.safari.history" AND ' + 'kMDItemContentTypeTree != "public.calendar-event" AND ' + 'kMDItemContentTypeTree != "com.apple.ichat.transcript"',
attributes: ['kMDItemPath', 'kMDItemContentType'],
limit: 10
});
case 6:
data = _context3.sent;
return _context3.abrupt('return', _.map(data, function (_ref15) {
var kMDItemPath = _ref15.kMDItemPath;
var kMDItemContentType = _ref15.kMDItemContentType;
return {
path: kMDItemPath,
contentType: kMDItemContentType
};
}));
case 8:
case 'end':
return _context3.stop();
}
}
}, _callee, this);
}));
return function fetchFiles(_x2) {
return ref.apply(this, arguments);
};
}();
export function userHome() {
if (isDemo()) {
return '/Users/LaconaUser';
}
return process.env[process.platform == 'win32' ? 'USERPROFILE' : 'HOME'];
}
/* Running Apps */
export function fetchRunningApplications() {
if (isDemo()) {
return Promise.resolve(global.demoData.applications);
}
if (isOSX()) {
return send('FETCH_RUNNING_APPLICATIONS');
}
}
export function activateApplication(_ref16) {
var path = _ref16.path;
if (isOSX()) {
return open({ path: path });
}
}
export function hideApplication(_ref17) {
var path = _ref17.path;
if (isOSX()) {
return send('HIDE', { path: path });
}
}
export function closeApplicationWindows(_ref18) {
var bundleId = _ref18.bundleId;
if (isOSX()) {
var _script = '\n tell application "System Events"\n set proc to first process whose background only is false and bundle identifier is "' + bundleId + '"\n repeat with win in proc\'s windows\n set butt to (win\'s first button whose subrole is "AXCloseButton")\n click butt\n end repeat\n end tell\n ';
return runApplescript({ script: _script });
}
}
export function quitApplication(_ref19) {
var path = _ref19.path;
if (isOSX()) {
return send('QUIT', { path: path });
}
}
/* Open Windows */
var openWindowFetchScript = '\n on run\n \ttell application "System Events"\n \t\tset allWindows to {}\n \t\trepeat with proc in (processes where background only is false)\n \t\t\trepeat with win in proc\'s windows\n \t\t\t\tif win\'s subrole is "AXStandardWindow" then\n \t\t\t\t\tset end of allWindows to {{proc\'s id, win\'s name}, win\'s title, my hasCloseButton(win)}\n \t\t\t\tend if\n \t\t\tend repeat\n \t\tend repeat\n \tend tell\n \treturn allWindows\n end run\n\n on hasCloseButton(win)\n \ttell application "System Events"\n \t\tif win\'s subrole is "AXStandardWindow" then\n \t\t\trepeat with butt in win\'s buttons\n \t\t\t\tif butt\'s subrole is "AXCloseButton" then\n \t\t\t\t\treturn true\n \t\t\t\tend if\n \t\t\tend repeat\n \t\tend if\n \t\treturn false\n \tend tell\n end hasCloseButton\n';
export var fetchOpenWindows = function () {
var ref = _asyncToGenerator(_regeneratorRuntime.mark(function _callee2() {
var data;
return _regeneratorRuntime.wrap(function _callee2$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
if (!isDemo()) {
_context4.next = 2;
break;
}
return _context4.abrupt('return', []);
case 2:
if (!isOSX()) {
_context4.next = 7;
break;
}
_context4.next = 5;
return runApplescript({ script: openWindowFetchScript });
case 5:
data = _context4.sent;
return _context4.abrupt('return', _.map(data, _.partial(_.zipObject, ['id', 'name', 'closeable'], _)));
case 7:
case 'end':
return _context4.stop();
}
}
}, _callee2, this);
}));
return function fetchOpenWindows() {
return ref.apply(this, arguments);
};
}();
export function closeOpenWindow(_ref20) {
var id = _ref20.id;
if (isOSX()) {
//NOT IMPLEMENTED
}
}
export function activateOpenWindow(_ref21) {
var id = _ref21.id;
if (isOSX()) {
var _id = _slicedToArray(id, 2);
var procId = _id[0];
var name = _id[1];
var _script2 = '\n tell application "System Events"\n set proc to first process whose background only is false and id is ' + procId + '\n set win to proc\'s first window whose name is "' + name + '"\n perform action "AXRaise" of win\n set proc\'s frontmost to true\n end tell\n ';
return runApplescript({ script: _script2 });
}
}
/* Browser Tabs */
var browserFetchScript = '\n -- set chromeTabs to {}\n set safariTabs to {}\n\n -- if is_running("Google Chrome") then\n -- set chromeTabs to run script "\n -- set allTabs to {}\n -- tell application \\"Google Chrome\\"\n -- repeat with win in every window\n -- repeat with t in win\'s tabs\n -- set end of allTabs to {\\"Google Chrome\\", {\\"Google Chrome\\", t\'s id}, title of t}\n -- end repeat\n -- end repeat\n -- end tell\n -- return allTabs\n -- "\n -- end if\n\n if is_running("Safari") then\n set safariTabs to run script "\n set allTabs to {}\n tell application \\"Safari\\"\n repeat with win in (windows where visible is true)\n repeat with t in win\'s tabs\n set end of allTabs to {\\"Safari\\", {\\"Safari\\", {win\'s index, t\'s index}}, t\'s name}\n end repeat\n end repeat\n end tell\n return allTabs\n "\n end if\n\n on is_running(appName)\n tell application "System Events" to (name of processes) contains appName\n end is_running\n\n -- return chromeTabs & safariTabs\n return safariTabs\n';
export var fetchBrowserTabs = function () {
var ref = _asyncToGenerator(_regeneratorRuntime.mark(function _callee3() {
var data;
return _regeneratorRuntime.wrap(function _callee3$(_context5) {
while (1) {
switch (_context5.prev = _context5.next) {
case 0:
if (!isDemo()) {
_context5.next = 2;
break;
}
return _context5.abrupt('return', []);
case 2:
if (!isOSX()) {
_context5.next = 7;
break;
}
_context5.next = 5;
return runApplescript({ script: browserFetchScript });
case 5:
data = _context5.sent;
return _context5.abrupt('return', _.map(data, _.partial(_.zipObject, ['appName', 'id', 'name'], _)));
case 7:
case 'end':
return _context5.stop();
}
}
}, _callee3, this);
}));
return function fetchBrowserTabs() {
return ref.apply(this, arguments);
};
}();
export function activateBrowserTab(id) {
if (isOSX()) {
var _id3 = _slicedToArray(_id2, 2);
var appName = _id3[0];
var _id2 = _id3[1];
if (appName === 'Google Chrome') {
script = '\n tell application "Google Chrome"\n repeat with wi from 1 to count windows\n repeat with ti from 1 to count (window wi\'s tabs)\n if id of window wi\'s tab ti is ' + _id2 + ' then\n set theTab to ti\n set theWin to wi\n end if\n end repeat\n end repeat\n\n set window theWin\'s active tab index to theTab\n set window theWin\'s index to 1\n activate\n end tell\n ';
return runApplescript({ script: script });
} else if (appName === 'Safari') {
// TODO THIS DOES NOT WORK
var _id4 = _slicedToArray(_id2, 2);
var winId = _id4[0];
var tabId = _id4[1];
script = '\n tell application "Safari"\n activate\n set win to window ' + winId + '\n set win\'s current tab to win\'s tab ' + tabId + '\n set win\'s index to 1\n end tell\n ';
return runApplescript({ script: script });
}
}
}
export function closeBrowserTab(_ref22) {
var id = _ref22.id;
if (isOSX()) {
//not yet implemented
}
}
/* Preference Panes */
export function watchPreferencePanes() {
if (isDemo()) {
return new Observable(function (observer) {
observer.next(demoData.preferencePanes);
});
}
if (isOSX()) {
var _context6;
return (_context6 = watchSpotlight({
query: "kMDItemContentType == 'com.apple.systempreference.prefpane'",
attributes: ['kMDItemDisplayName', 'kMDItemPath']
}), map).call(_context6, function (data) {
return _.map(data, function (_ref23) {
var kMDItemDisplayName = _ref23.kMDItemDisplayName;
var kMDItemPath = _ref23.kMDItemPath;
return { name: kMDItemDisplayName, path: kMDItemPath };
});
});
}
}
/* Dictionary */
export function fetchDictionaryDefinitions(_ref24) {
var word = _ref24.word;
if (isOSX()) {
return send('FETCH_DICTIONARY_DEFINITIONS', { word: word });
}
}
/* Settings */
export function setBluetooth(_ref25) {
var enabled = _ref25.enabled;
if (isOSX()) {
return send('SET_BLUETOOTH', { enabled: enabled });
}
}
export function checkBluetooth() {
if (isOSX()) {
return send('CHECK_BLUETOOTH');
}
}
export function setDarkMode(_ref26) {
var enabled = _ref26.enabled;
if (isOSX()) {
var _script3 = '\n tell application "System Events"\n tell appearance preferences\n set dark mode to ' + (enabled ? 'true' : 'false') + '\n end tell\n end tell\n ';
return runApplescript({ script: _script3 });
}
}
var CHECK_DARK_MODE_SCRIPT = '\n tell application "System Events"\n tell appearance preferences\n return dark mode\n end tell\n end tell\n';
export var checkDarkMode = function () {
var ref = _asyncToGenerator(_regeneratorRuntime.mark(function _callee4() {
var enabled;
return _regeneratorRuntime.wrap(function _callee4$(_context7) {
while (1) {
switch (_context7.prev = _context7.next) {
case 0:
if (!isOSX()) {
_context7.next = 5;
break;
}
_context7.next = 3;
return runApplescript({ script: CHECK_DARK_MODE_SCRIPT });
case 3:
enabled = _context7.sent;
return _context7.abrupt('return', { enabled: enabled });
case 5:
case 'end':
return _context7.stop();
}
}
}, _callee4, this);
}));
return function checkDarkMode() {
return ref.apply(this, arguments);
};
}();
function callSystem(command) {
return new Promise(function (resolve, reject) {
exec(command, function (err, stdout, stderr) {
if (err) {
reject(err);
} else {
resolve(stdout);
}
});
});
}
export var setWifi = function () {
var ref = _asyncToGenerator(_regeneratorRuntime.mark(function _callee5(_ref27) {
var enabled = _ref27.enabled;
return _regeneratorRuntime.wrap(function _callee5$(_context8) {
while (1) {
switch (_context8.prev = _context8.next) {
case 0:
if (!isOSX()) {
_context8.next = 4;
break;
}
_context8.next = 3;
return callSystem('/usr/sbin/networksetup -setairportpower en0 ' + (enabled ? 'on' : 'off'));
case 3:
return _context8.abrupt('return');
case 4:
case 'end':
return _context8.stop();
}
}
}, _callee5, this);
}));
return function setWifi(_x3) {
return ref.apply(this, arguments);
};
}();
export var checkWifi = function () {
var ref = _asyncToGenerator(_regeneratorRuntime.mark(function _callee6() {
var stdout;
return _regeneratorRuntime.wrap(function _callee6$(_context9) {
while (1) {
switch (_context9.prev = _context9.next) {
case 0:
if (!isOSX()) {
_context9.next = 5;
break;
}
_context9.next = 3;
return callSystem('/usr/sbin/networksetup -getairportpower en0');
case 3:
stdout = _context9.sent;
return _context9.abrupt('return', { enabled: _.includes(stdout, 'On') });
case 5:
case 'end':
return _context9.stop();
}
}
}, _callee6, this);
}));
return function checkWifi() {
return ref.apply(this, arguments);
};
}();
export var setDoNotDisturb = function () {
var ref = _asyncToGenerator(_regeneratorRuntime.mark(function _callee7(_ref28) {
var enabled = _ref28.enabled;
return _regeneratorRuntime.wrap(function _callee7$(_context10) {
while (1) {
switch (_context10.prev = _context10.next) {
case 0:
if (!isOSX()) {
_context10.next = 9;
break;
}
if (!enabled) {
_context10.next = 6;
break;
}
_context10.next = 4;
return callSystem('defaults -currentHost write ~/Library/Preferences/ByHost/com.apple.notificationcenterui doNotDisturb -boolean true; defaults -currentHost write ~/Library/Preferences/ByHost/com.apple.notificationcenterui doNotDisturbDate -date "`date -u +\\"%Y-%m-%d %H:%M:%S +000\\"`"; killall NotificationCenter');
case 4:
_context10.next = 8;
break;
case 6:
_context10.next = 8;
return callSystem('defaults -currentHost write ~/Library/Preferences/ByHost/com.apple.notificationcenterui doNotDisturb -boolean false; killall NotificationCenter');
case 8:
return _context10.abrupt('return');
case 9:
case 'end':
return _context10.stop();
}
}
}, _callee7, this);
}));
return function setDoNotDisturb(_x4) {
return ref.apply(this, arguments);
};
}();
export var checkDoNotDisturb = function () {
var ref = _asyncToGenerator(_regeneratorRuntime.mark(function _callee8() {
var stdout;
return _regeneratorRuntime.wrap(function _callee8$(_context11) {
while (1) {
switch (_context11.prev = _context11.next) {
case 0:
if (!isOSX()) {
_context11.next = 5;
break;
}
_context11.next = 3;
return callSystem('/usr/bin/defaults -currentHost read ' + userHome() + '/Library/Preferences/ByHost/com.apple.notificationcenterui doNotDisturb');
case 3:
stdout = _context11.sent;
return _context11.abrupt('return', { enabled: _.includes(stdout, '1') });
case 5:
case 'end':
return _context11.stop();
}
}
}, _callee8, this);
}));
return function checkDoNotDisturb() {
return ref.apply(this, arguments);
};
}();
export function setVolume(_ref29) {
var mute = _ref29.mute;
if (isOSX()) {
var _script4 = 'set volume ' + (mute ? 'with' : 'without') + ' output muted';
return runApplescript({ script: _script4 });
}
}
export var checkVolume = function () {
var ref = _asyncToGenerator(_regeneratorRuntime.mark(function _callee9() {
var _script5, output;
return _regeneratorRuntime.wrap(function _callee9$(_context12) {
while (1) {
switch (_context12.prev = _context12.next) {
case 0:
if (!isOSX()) {
_context12.next = 6;
break;
}
_script5 = 'output muted of (get volume settings)';
_context12.next = 4;
return runApplescript({ script: _script5 });
case 4:
output = _context12.sent;
return _context12.abrupt('return', { mute: output });
case 6:
case 'end':
return _context12.stop();
}
}
}, _callee9, this);
}));
return function checkVolume() {
return ref.apply(this, arguments);
};
}();
export function shutdown() {
if (isOSX()) {
return runApplescript({ script: 'tell application "System Events" to shut down' });
}
}
export function restart() {
if (isOSX()) {
return runApplescript({ script: 'tell application "System Events" to restart' });
}
}
export function logOut() {
if (isOSX()) {
return runApplescript({ script: 'tell application "System Events" to log out' });
}
}
export function sleep() {
if (isOSX()) {
return runApplescript({ script: 'tell application "System Events" to sleep' });
}
}
export function lock() {
if (isOSX()) {
return callSystem('/System/Library/CoreServices/Menu\\ Extras/User.menu/Contents/Resources/CGSession -suspend');
}
}
export function hibernate() {}
export function turnOffDisplay() {
if (isOSX()) {
return callSystem('/usr/bin/pmset displaysleepnow');
}
}
export function turnOnScreensaver() {
if (isOSX()) {
return runApplescript({ script: 'tell application "System Events" to start current screen saver' });
}
}
export function emptyTrash() {
if (isOSX()) {
return runApplescript({ script: 'tell application "Finder" to empty the trash' });
}
}
/* Mounted Volumes */
export function fetchMountedVolumes() {
if (isDemo()) {
return Promise.resolve(global.demoData.volumes);
}
if (isOSX()) {
return send('FETCH_MOUNTED_VOLUMES');
}
}
export function unmountVolume(_ref30) {
var id = _ref30.id;
if (isOSX()) {
var _script6 = 'tell application "Finder" to eject "' + id + '"';
return runApplescript({ script: _script6 });
}
}
export function unmountAllVolumes() {
if (isOSX()) {
var _script7 = 'tell application "Finder" to eject the disks';
return runApplescript({ script: _script7 });
}
}
/* Music */
function simplifyArtist(item) {
var albumArtist = item.albumArtist;
var artist = item.artist;
item.artist = albumArtist || artist;
delete item.albumArtist;
}
function sortStringAsInteger(key) {
return function (item) {
return parseInt(item[key], 10);
};
}
function arrangeOSXMusic(arrays) {
return _.chain(arrays).thru(_.spread(_.zip)).map(_.partial(_.zipObject, ['name', 'album', 'artist', 'albumArtist', 'composer', 'genre', 'year', 'discNumber', 'trackNumber', 'id'])).tap(function (items) {
return _.forEach(items, simplifyArtist);
}).sortBy([simplifyArtist, sortStringAsInteger('year'), 'album', sortStringAsInteger('discNumber'), sortStringAsInteger('trackNumber'), sortStringAsInteger('id')]).value();
}
export var fetchMusic = function () {
var ref = _asyncToGenerator(_regeneratorRuntime.mark(function _callee10() {
var _script8, arrays;
return _regeneratorRuntime.wrap(function _callee10$(_context13) {
while (1) {
switch (_context13.prev = _context13.next) {
case 0:
if (!isDemo()) {
_context13.next = 2;
break;
}
return _context13.abrupt('return', global.demoData.music);
case 2:
if (!isOSX()) {
_context13.next = 8;
break;
}
_script8 = 'tell application "iTunes" to get {name, album, artist, album artist, composer, genre, year, disc number, track number, database ID} of every track of first library playlist';
_context13.next = 6;
return runApplescript({ script: _script8 });
case 6:
arrays = _context13.sent;
return _context13.abrupt('return', arrangeOSXMusic(arrays));
case 8:
case 'end':
return _context13.stop();
}
}
}, _callee10, this);
}));
return function fetchMusic() {
return ref.apply(this, arguments);
};
}();
export function playSongIds(_ref31) {
var ids = _ref31.ids;
var script = '\n tell application "iTunes"\n if user playlist "Lacona Playlist" exists then\n try\n delete user playlist "Lacona Playlist"\n end try\n end if\n\n make new user playlist with properties {name:"Lacona Playlist", shuffle: false}\n\n repeat with tid in {' + ids.join(',') + '}\n set trk to (some track of first user playlist whose database ID is tid)\n duplicate trk to end of user playlist "Lacona Playlist"\n end repeat\n\n play user playlist "Lacona Playlist"\n end tell\n ';
return runApplescript({ script: script });
}
export function musicPlay() {
if (isOSX()) {
return runApplescript({ script: 'tell application "iTunes" to play' });
}
}
export function musicPause() {
if (isOSX()) {
return runApplescript({ script: 'tell application "iTunes" to pause' });
}
}
export function musicNext() {
if (isOSX()) {
return runApplescript({ script: 'tell application "iTunes" to next track' });
}
}
export function musicPrevious() {
if (isOSX()) {
return runApplescript({ script: 'tell application "iTunes" to previous track' });
}
}
export function musicStop() {
if (isOSX()) {
return runApplescript({ script: 'tell application "iTunes" to stop' });
}
}
/* OSX specific */
export function runApplescript(_ref32) {
var script = _ref32.script;
if (isOSX()) {
return send('RUN_APPLESCRIPT', { script: script });
}
}
export function fetchSpotlight(_ref33) {
var _ref33$query = _ref33.query;
var query = _ref33$query === undefined ? '' : _ref33$query;
var _ref33$attributes = _ref33.attributes;
var attributes = _ref33$attributes === undefined ? [] : _ref33$attributes;
var _ref33$directories = _ref33.directories;
var directories = _ref33$directories === undefined ? [] : _ref33$directories;
var _ref33$limit = _ref33.limit;
var limit = _ref33$limit === undefined ? 0 : _ref33$limit;
var queryId = guid();
return send('QUERY_SPOTLIGHT', {
query: query,
queryId: queryId,
attributes: attributes,
directories: directories,
limit: limit,
liveUpdate: false
});
}
export function watchSpotlight(_ref34) {
var _ref34$query = _ref34.query;
var query = _ref34$query === undefined ? '' : _ref34$query;
var _ref34$attributes = _ref34.attributes;
var attributes = _ref34$attributes === undefined ? [] : _ref34$attributes;
var _ref34$directories = _ref34.directories;
var directories = _ref34$directories === undefined ? [] : _ref34$directories;
var _ref34$limit = _ref34.limit;
var limit = _ref34$limit === undefined ? 0 : _ref34$limit;
return new Observable(function (observer) {
observer.next([]);
var queryId = guid();
global[SEND_KEY]('QUERY_SPOTLIGHT', {
query: query,
queryId: queryId,
attributes: attributes,
directories: directories,
limit: limit,
liveUpdate: true
}, function (err, data) {
if (err) {
console.error(err);
} else {
observer.next(data);
}
});
return function () {
return global[SEND_KEY]('CANCEL_SPOTLIGHT_QUERY', { queryId: queryId });
};
});
}
export function reloadAddons() {
return send('RELOAD_ADDONS');
}
/* Config and Context */
export function openLaconaPreferences() {
return send('OPEN_LACONA_PREFERENCES');
}