2017-01-31 07:50:28 +01:00
|
|
|
const Launcher = require('../../comp/launcher');
|
2016-04-09 20:58:22 +02:00
|
|
|
|
2019-08-16 23:05:39 +02:00
|
|
|
const ForeMostAppScript =
|
|
|
|
'tell application "System Events" to set frontApp to name of first process whose frontmost is true';
|
|
|
|
const ChromeScript =
|
|
|
|
'tell application "{}" to set appUrl to URL of active tab of front window\n' +
|
2016-04-09 22:19:56 +02:00
|
|
|
'tell application "{}" to set appTitle to title of active tab of front window\n' +
|
|
|
|
'return appUrl & "\n" & appTitle';
|
2019-08-16 23:05:39 +02:00
|
|
|
const SafariScript =
|
|
|
|
'tell application "{}" to set appUrl to URL of front document\n' +
|
2016-04-09 22:19:56 +02:00
|
|
|
'tell application "{}" to set appTitle to name of front document\n' +
|
|
|
|
'return appUrl & "\n" & appTitle';
|
2019-08-16 23:05:39 +02:00
|
|
|
const OtherAppsScript =
|
|
|
|
'tell application "System Events"\n' +
|
2016-04-09 22:19:56 +02:00
|
|
|
' 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';
|
|
|
|
|
2019-08-16 23:05:39 +02:00
|
|
|
const AutoTypeHelper = function() {};
|
2016-04-09 20:58:22 +02:00
|
|
|
|
|
|
|
AutoTypeHelper.prototype.getActiveWindowTitle = function(callback) {
|
2016-07-17 13:30:38 +02:00
|
|
|
AutoTypeHelper.exec(ForeMostAppScript, (err, out) => {
|
2019-08-16 23:05:39 +02:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2017-01-31 07:50:28 +01:00
|
|
|
const 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) => {
|
2019-08-16 23:05:39 +02:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2017-01-31 07:50:28 +01:00
|
|
|
const parts = out.split('\n');
|
2017-02-05 08:54:01 +01:00
|
|
|
return callback(null, (parts[1] || '').trim(), parts[0].trim());
|
2016-04-09 22:19:56 +02:00
|
|
|
});
|
|
|
|
} else if (['Safari', 'Webkit'].indexOf(appName) >= 0) {
|
2016-07-17 13:30:38 +02:00
|
|
|
AutoTypeHelper.exec(SafariScript.replace(/\{}/g, appName), (err, out) => {
|
2019-08-16 23:05:39 +02:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2017-01-31 07:50:28 +01:00
|
|
|
const parts = out.split('\n');
|
2017-02-05 08:54:01 +01:00
|
|
|
return callback(null, (parts[1] || '').trim(), parts[0].trim());
|
2016-04-09 22:19:56 +02:00
|
|
|
});
|
|
|
|
} 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) => {
|
2019-08-16 23:05:39 +02:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2016-04-09 22:19:56 +02:00
|
|
|
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;
|