better window title detection

This commit is contained in:
antelle 2016-04-09 23:19:56 +03:00
parent c21ecbc785
commit 8236146ed2
2 changed files with 42 additions and 15 deletions

View File

@ -4,23 +4,50 @@ var Launcher = require('../../launcher');
var spawn = Launcher.req('child_process').spawn;
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';
var AutoTypeHelper = function() {
};
AutoTypeHelper.prototype.getActiveWindowTitle = function(callback) {
var script = 'global frontApp, frontAppName, windowTitle\n' +
'set windowTitle to ""\n' +
'tell application "System Events"\n' +
' set frontApp to first application process whose frontmost is true\n' +
' set frontAppName to name of frontApp\n' +
' tell process frontAppName\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\n' +
'return windowTitle';
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());
});
}
});
};
AutoTypeHelper.exec = function(script, callback) {
var ps = spawn('osascript', ['-e', script]);
var stderr = '';
var stdout = '';
@ -34,7 +61,7 @@ AutoTypeHelper.prototype.getActiveWindowTitle = function(callback) {
if (code) {
callback('Exit code ' + code + ': ' + stderr + (stdout ? '\nout: ' + stdout : ''));
} else {
callback(stdout);
callback(null, stdout);
}
});
};

View File

@ -79,11 +79,11 @@ var AutoType = {
getActiveWindowTitle: function(callback) {
logger.debug('Get window title');
return this.helper.getActiveWindowTitle(function(err, title) {
return this.helper.getActiveWindowTitle(function(err, title, url) {
if (err) {
logger.error('Error get window title', err);
} else {
logger.debug('Window title: ', title);
logger.debug('Window title', title, url);
}
return callback(err, title);
});