2016-04-09 20:58:22 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var Launcher = require('../../launcher');
|
|
|
|
|
|
|
|
var spawn = Launcher.req('child_process').spawn;
|
|
|
|
|
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-04-09 22:19:56 +02:00
|
|
|
AutoTypeHelper.exec(ForeMostAppScript, function(err, out) {
|
|
|
|
if (err) { return callback(err); }
|
|
|
|
var appName = out.trim();
|
|
|
|
if (['Google Chrome', 'Chromium', 'Google Chrome Canary'].indexOf(appName) >= 0) {
|
|
|
|
AutoTypeHelper.exec(ChromeScript.replace(/\{}/g, appName), function(err, out) {
|
|
|
|
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) {
|
|
|
|
AutoTypeHelper.exec(SafariScript.replace(/\{}/g, appName), function(err, out) {
|
|
|
|
if (err) { return callback(err); }
|
|
|
|
var parts = out.split('\n');
|
|
|
|
return callback(null, parts[1].trim(), parts[0].trim());
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
AutoTypeHelper.exec(OtherAppsScript.replace(/\{}/g, appName), function(err, out) {
|
|
|
|
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-09 20:58:22 +02:00
|
|
|
var ps = spawn('osascript', ['-e', script]);
|
|
|
|
var stderr = '';
|
|
|
|
var stdout = '';
|
|
|
|
ps.stdout.on('data', function(data) {
|
|
|
|
stdout += data.toString();
|
|
|
|
});
|
|
|
|
ps.stderr.on('data', function(data) {
|
|
|
|
stderr += data.toString();
|
|
|
|
});
|
|
|
|
ps.on('close', function(code) {
|
|
|
|
if (code) {
|
|
|
|
callback('Exit code ' + code + ': ' + stderr + (stdout ? '\nout: ' + stdout : ''));
|
|
|
|
} else {
|
2016-04-09 22:19:56 +02:00
|
|
|
callback(null, stdout);
|
2016-04-09 20:58:22 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = AutoTypeHelper;
|