keeweb/app/scripts/auto-type/helper/auto-type-helper-darwin.js

62 lines
2.5 KiB
JavaScript
Raw Normal View History

2016-04-09 20:58:22 +02:00
'use strict';
2016-04-23 16:50:40 +02:00
var Launcher = require('../../comp/launcher');
2016-04-09 20:58:22 +02:00
2016-04-09 22:19:56 +02:00
var ForeMostAppScript = 'tell application "System Events" to set frontApp to name of first process whose frontmost is true';
var ChromeScript = 'tell application "{}" to set appUrl to URL of active tab of front window\n' +
'tell application "{}" to set appTitle to title of active tab of front window\n' +
'return appUrl & "\n" & appTitle';
var SafariScript = 'tell application "{}" to set appUrl to URL of front document\n' +
'tell application "{}" to set appTitle to name of front document\n' +
'return appUrl & "\n" & appTitle';
var OtherAppsScript = 'tell application "System Events"\n' +
' tell process "{}"\n' +
' tell (1st window whose value of attribute "AXMain" is true)\n' +
' set windowTitle to value of attribute "AXTitle"\n' +
' end tell\n' +
' end tell\n' +
'end tell';
2016-04-09 20:58:22 +02:00
var AutoTypeHelper = function() {
};
AutoTypeHelper.prototype.getActiveWindowTitle = function(callback) {
2016-07-17 13:30:38 +02:00
AutoTypeHelper.exec(ForeMostAppScript, (err, out) => {
2016-04-09 22:19:56 +02:00
if (err) { return callback(err); }
var appName = out.trim();
2016-04-10 08:36:09 +02:00
// getting urls and titles from Chrome or Safari:
// - will suit in 90% cases
// - does not require assistive access
// - allows to get url
2016-04-09 22:19:56 +02:00
if (['Google Chrome', 'Chromium', 'Google Chrome Canary'].indexOf(appName) >= 0) {
2016-07-17 13:30:38 +02:00
AutoTypeHelper.exec(ChromeScript.replace(/\{}/g, appName), (err, out) => {
2016-04-09 22:19:56 +02:00
if (err) { return callback(err); }
var parts = out.split('\n');
return callback(null, parts[1].trim(), parts[0].trim());
});
} else if (['Safari', 'Webkit'].indexOf(appName) >= 0) {
2016-07-17 13:30:38 +02:00
AutoTypeHelper.exec(SafariScript.replace(/\{}/g, appName), (err, out) => {
2016-04-09 22:19:56 +02:00
if (err) { return callback(err); }
var parts = out.split('\n');
return callback(null, parts[1].trim(), parts[0].trim());
});
} else {
2016-04-10 08:36:09 +02:00
// special cases are not available. this method may ask the user about assistive access
2016-07-17 13:30:38 +02:00
AutoTypeHelper.exec(OtherAppsScript.replace(/\{}/g, appName), (err, out) => {
2016-04-09 22:19:56 +02:00
if (err) { return callback(err); }
return callback(null, out.trim());
});
}
});
};
2016-04-09 20:58:22 +02:00
2016-04-09 22:19:56 +02:00
AutoTypeHelper.exec = function(script, callback) {
2016-04-19 19:39:17 +02:00
Launcher.spawn({
cmd: 'osascript',
args: ['-e', script],
complete: callback
2016-04-09 20:58:22 +02:00
});
};
module.exports = AutoTypeHelper;