252 lines
7.8 KiB
JavaScript
252 lines
7.8 KiB
JavaScript
/**
|
|
* Utilities for working with `osascript` which runs AppleScript on Macs
|
|
*/
|
|
'use strict';
|
|
|
|
var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');
|
|
|
|
var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
|
|
|
|
let osascriptExecAsync = (() => {
|
|
var _ref = (0, _asyncToGenerator3.default)(function* (script, opts) {
|
|
return yield (0, _execAsync2.default)('osascript', osascriptArgs(script), Object.assign({ stdio: 'inherit' }, opts));
|
|
});
|
|
|
|
return function osascriptExecAsync(_x, _x2) {
|
|
return _ref.apply(this, arguments);
|
|
};
|
|
})();
|
|
|
|
let osascriptSpawnAsync = (() => {
|
|
var _ref2 = (0, _asyncToGenerator3.default)(function* (script, opts) {
|
|
return yield (0, _spawnAsync2.default)('osascript', osascriptArgs(script), opts);
|
|
});
|
|
|
|
return function osascriptSpawnAsync(_x3, _x4) {
|
|
return _ref2.apply(this, arguments);
|
|
};
|
|
})();
|
|
|
|
let isAppRunningAsync = (() => {
|
|
var _ref3 = (0, _asyncToGenerator3.default)(function* (appName) {
|
|
let zeroMeansNo = (yield osascriptExecAsync('tell app "System Events" to count processes whose name is ' + JSON.stringify(appName))).trim();
|
|
return zeroMeansNo !== '0';
|
|
});
|
|
|
|
return function isAppRunningAsync(_x5) {
|
|
return _ref3.apply(this, arguments);
|
|
};
|
|
})();
|
|
|
|
let safeIdOfAppAsync = (() => {
|
|
var _ref4 = (0, _asyncToGenerator3.default)(function* (appName) {
|
|
try {
|
|
return (yield osascriptExecAsync('id of app ' + JSON.stringify(appName))).trim();
|
|
} catch (e) {
|
|
return null;
|
|
}
|
|
});
|
|
|
|
return function safeIdOfAppAsync(_x6) {
|
|
return _ref4.apply(this, arguments);
|
|
};
|
|
})();
|
|
|
|
let openFinderToFolderAsync = (() => {
|
|
var _ref5 = (0, _asyncToGenerator3.default)(function* (dir, activate = true) {
|
|
yield osascriptSpawnAsync(['tell application "Finder"', 'open POSIX file ' + JSON.stringify(dir), activate && 'activate' || '', 'end tell']);
|
|
});
|
|
|
|
return function openFinderToFolderAsync(_x7) {
|
|
return _ref5.apply(this, arguments);
|
|
};
|
|
})();
|
|
|
|
let openInAppAsync = (() => {
|
|
var _ref6 = (0, _asyncToGenerator3.default)(function* (appName, pth) {
|
|
let cmd = 'tell app ' + JSON.stringify(appName) + ' to open ' + JSON.stringify(_path2.default.resolve(pth));
|
|
// console.log("cmd=", cmd);
|
|
return yield osascriptSpawnAsync(cmd);
|
|
});
|
|
|
|
return function openInAppAsync(_x8, _x9) {
|
|
return _ref6.apply(this, arguments);
|
|
};
|
|
})();
|
|
|
|
let chooseAppAsync = (() => {
|
|
var _ref7 = (0, _asyncToGenerator3.default)(function* (listOfAppNames) {
|
|
let runningAwaitables = [];
|
|
let appIdAwaitables = [];
|
|
for (let appName of listOfAppNames) {
|
|
runningAwaitables.push(isAppRunningAsync(appName));
|
|
appIdAwaitables.push(safeIdOfAppAsync(appName));
|
|
}
|
|
let running = yield Promise.all(runningAwaitables);
|
|
let appIds = yield Promise.all(appIdAwaitables);
|
|
|
|
let i;
|
|
for (i = 0; i < listOfAppNames.length; i++) {
|
|
if (running[i]) {
|
|
return listOfAppNames[i];
|
|
}
|
|
}
|
|
|
|
for (i = 0; i < listOfAppNames.length; i++) {
|
|
if (!!appIds[i]) {
|
|
return listOfAppNames[i];
|
|
}
|
|
}
|
|
|
|
return null;
|
|
});
|
|
|
|
return function chooseAppAsync(_x10) {
|
|
return _ref7.apply(this, arguments);
|
|
};
|
|
})();
|
|
|
|
let chooseEditorAppAsync = (() => {
|
|
var _ref8 = (0, _asyncToGenerator3.default)(function* (preferredEditor) {
|
|
if (preferredEditor) {
|
|
// Make sure this editor exists
|
|
let appId = yield safeIdOfAppAsync(preferredEditor);
|
|
if (appId) {
|
|
return preferredEditor;
|
|
} else {
|
|
console.warn(`Your preferred editor (${preferredEditor}) isn't installed on this computer.`);
|
|
}
|
|
}
|
|
|
|
let editorsToTry = ['Visual Studio Code', 'Atom', 'Sublime Text', 'TextMate', 'TextWrangler', 'Visual Studio Code', 'Brackets', 'SubEthaEdit', 'BBEdit', 'Textastic', 'UltraEdit', 'MacVim', 'CodeRunner 2', 'CodeRunner', 'TextEdit'];
|
|
|
|
return yield chooseAppAsync(editorsToTry);
|
|
});
|
|
|
|
return function chooseEditorAppAsync(_x11) {
|
|
return _ref8.apply(this, arguments);
|
|
};
|
|
})();
|
|
|
|
let chooseTerminalAppAsync = (() => {
|
|
var _ref9 = (0, _asyncToGenerator3.default)(function* () {
|
|
return yield chooseAppAsync(['iTerm 3', 'iTerm 2', 'iTerm', 'HyperTerm',
|
|
// 'Cathode',
|
|
// 'Terminator',
|
|
// 'MacTerm',
|
|
'Terminal']);
|
|
});
|
|
|
|
return function chooseTerminalAppAsync() {
|
|
return _ref9.apply(this, arguments);
|
|
};
|
|
})();
|
|
|
|
let openInEditorAsync = (() => {
|
|
var _ref10 = (0, _asyncToGenerator3.default)(function* (pth, preferredEditor) {
|
|
let appName = yield chooseEditorAppAsync(preferredEditor);
|
|
console.log('Will open in ' + appName + ' -- ' + pth);
|
|
return yield openInAppAsync(appName, pth);
|
|
});
|
|
|
|
return function openInEditorAsync(_x12, _x13) {
|
|
return _ref10.apply(this, arguments);
|
|
};
|
|
})();
|
|
|
|
let openItermToSpecificFolderAsync = (() => {
|
|
var _ref11 = (0, _asyncToGenerator3.default)(function* (dir) {
|
|
return yield osascriptSpawnAsync(['tell application "iTerm"', 'make new terminal', 'tell the first terminal', 'activate current session', 'launch session "Default Session"', 'tell the last session', 'write text "cd ' + _util2.default.inspect(dir) + ' && clear"',
|
|
// 'write text "clear"',
|
|
'end tell', 'end tell', 'end tell']);
|
|
// exec("osascript -e 'tell application \"iTerm\"' -e 'make new terminal' -e 'tell the first terminal' -e 'activate current session' -e 'launch session \"Default Session\"' -e 'tell the last session' -e 'write text \"cd #{value}\"' -e 'write text \"clear\"' -e 'end tell' -e 'end tell' -e 'end tell' > /dev/null 2>&1")
|
|
});
|
|
|
|
return function openItermToSpecificFolderAsync(_x14) {
|
|
return _ref11.apply(this, arguments);
|
|
};
|
|
})();
|
|
|
|
let openTerminalToSpecificFolderAsync = (() => {
|
|
var _ref12 = (0, _asyncToGenerator3.default)(function* (dir, inTab = false) {
|
|
if (inTab) {
|
|
return yield osascriptSpawnAsync(['tell application "terminal"', 'tell application "System Events" to tell process "terminal" to keystroke "t" using command down', 'do script with command "cd ' + _util2.default.inspect(dir) + ' && clear" in selected tab of the front window', 'end tell']);
|
|
} else {
|
|
return yield osascriptSpawnAsync(['tell application "terminal"', 'do script "cd ' + _util2.default.inspect(dir) + ' && clear"', 'end tell', 'tell application "terminal" to activate']);
|
|
}
|
|
});
|
|
|
|
return function openTerminalToSpecificFolderAsync(_x15) {
|
|
return _ref12.apply(this, arguments);
|
|
};
|
|
})();
|
|
|
|
let openFolderInTerminalAppAsync = (() => {
|
|
var _ref13 = (0, _asyncToGenerator3.default)(function* (dir, inTab = false) {
|
|
let program = yield chooseTerminalAppAsync();
|
|
|
|
switch (program) {
|
|
case 'iTerm':
|
|
return yield openItermToSpecificFolderAsync(dir, inTab);
|
|
break;
|
|
|
|
case 'Terminal':
|
|
default:
|
|
return yield openTerminalToSpecificFolderAsync(dir, inTab);
|
|
break;
|
|
}
|
|
});
|
|
|
|
return function openFolderInTerminalAppAsync(_x16) {
|
|
return _ref13.apply(this, arguments);
|
|
};
|
|
})();
|
|
|
|
var _execAsync = require('exec-async');
|
|
|
|
var _execAsync2 = _interopRequireDefault(_execAsync);
|
|
|
|
var _path = require('path');
|
|
|
|
var _path2 = _interopRequireDefault(_path);
|
|
|
|
var _spawnAsync = require('@expo/spawn-async');
|
|
|
|
var _spawnAsync2 = _interopRequireDefault(_spawnAsync);
|
|
|
|
var _util = require('util');
|
|
|
|
var _util2 = _interopRequireDefault(_util);
|
|
|
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
|
|
function osascriptArgs(script) {
|
|
if (!_util2.default.isArray(script)) {
|
|
script = [script];
|
|
}
|
|
|
|
let args = [];
|
|
for (let line of script) {
|
|
args.push('-e');
|
|
args.push(line);
|
|
}
|
|
|
|
return args;
|
|
}
|
|
|
|
module.exports = {
|
|
chooseAppAsync,
|
|
chooseEditorAppAsync,
|
|
chooseTerminalAppAsync,
|
|
execAsync: osascriptExecAsync,
|
|
isAppRunningAsync,
|
|
openFinderToFolderAsync,
|
|
openFolderInTerminalAppAsync,
|
|
openInAppAsync,
|
|
openInEditorAsync,
|
|
openItermToSpecificFolderAsync,
|
|
openTerminalToSpecificFolderAsync,
|
|
safeIdOfAppAsync,
|
|
spawnAsync: osascriptSpawnAsync
|
|
};
|
|
//# sourceMappingURL=index.js.map
|