UNPKG

lacona-osx

Version:

Lacona Extensions for OSX, tied into the global-context hosted API

142 lines (113 loc) 4.57 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.ContextURL = exports.ContextDirectory = undefined; let fetchCurrentDirectory = (() => { var _ref = _asyncToGenerator(function* () { const dir = yield (0, _laconaApi.runApplescript)({ script: FETCH_CURRENT_FINDER_DIRECTORY_SCRIPT }); if (dir) { return { path: dir, argument: 'finder directory', annotation: { type: 'icon', value: '/System/Library/CoreServices/Finder.app' } }; } else { return {}; } }); return function fetchCurrentDirectory() { return _ref.apply(this, arguments); }; })(); let fetchCurrentURL = (() => { var _ref2 = _asyncToGenerator(function* () { const url = yield (0, _laconaApi.runApplescript)({ script: FETCH_CURRENT_SAFARI_URL_SCRIPT }); if (url) { return { url: url, argument: 'safari URL', annotation: { type: 'icon', value: '/Applications/Safari.app' } }; } else { return {}; } }); return function fetchCurrentURL() { return _ref2.apply(this, arguments); }; })(); var _elliptical = require('elliptical'); var _laconaPhrases = require('lacona-phrases'); var _laconaApi = require('lacona-api'); var _laconaSourceHelpers = require('lacona-source-helpers'); function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } /** @jsx createElement */ const FETCH_CURRENT_FINDER_DIRECTORY_SCRIPT = ` set dir to "" set frontApp to "" tell application "System Events" set frontApp to name of first application process whose frontmost is true end tell if (frontApp is "Finder") then tell application "Finder" if (count of Finder windows) is not 0 then set currentDir to (target of front Finder window) as text set dir to (POSIX path of currentDir) end if end tell end if return dir `; const FETCH_CURRENT_SAFARI_URL_SCRIPT = ` set theURL to "" if is_running("Safari") then set theURL to run script " set theURL to \\"\\" set frontApp to \\"\\" tell application \\"System Events\\" set frontApp to name of first application process whose frontmost is true end tell if (frontApp is \\"Safari\\") then tell application \\"Safari\\" set theURL to URL of document 1 end tell end if return theURL " end if on is_running(appName) tell application "System Events" to (name of processes) contains appName end is_running return theURL `; const CurrentDirectorySource = (0, _laconaSourceHelpers.onActivate)(fetchCurrentDirectory, {}); const CurrentURLSource = (0, _laconaSourceHelpers.onActivate)(fetchCurrentURL, {}); const ContextDirectory = exports.ContextDirectory = { extends: [_laconaPhrases.Directory], describe({ observe, config }) { if (!config.enableContextDirectory) return; const context = observe((0, _elliptical.createElement)(CurrentDirectorySource, null)); if (context.path) { const expandedPath = context.path.replace(/^~/, (0, _laconaApi.userHome)()); return (0, _elliptical.createElement)( 'placeholder', { argument: context.argument || 'directory', suppressEmpty: false }, (0, _elliptical.createElement)('list', { items: [{ text: context.path }, { text: context.argument, category: 'symbol' }, { text: 'context directory', category: 'symbol' }], value: expandedPath, annotation: context.annotation, limit: 1 }) ); } } }; const ContextURL = exports.ContextURL = { extends: [_laconaPhrases.URL], describe({ observe, config }) { if (!config.enableContextURL) return; const context = observe((0, _elliptical.createElement)(CurrentURLSource, null)); if (context.url) { return (0, _elliptical.createElement)( 'placeholder', { argument: context.argument || 'url', suppressEmpty: false }, (0, _elliptical.createElement)('list', { items: [{ text: context.url }, { text: context.argument, category: 'symbol' }, { text: 'context url', category: 'symbol' }], value: context.url, annotation: context.annotation, limit: 1 }) ); } } };