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

93 lines
3.3 KiB
JavaScript
Raw Normal View History

2019-09-15 14:16:32 +02:00
import { Launcher } from 'comp/launcher';
2016-04-09 20:58:22 +02:00
2019-08-16 23:05:39 +02:00
const ForeMostAppScript =
'tell application "System Events"\n' +
' set frontAppName to name of first process whose frontmost is true\n' +
' set frontAppId to id of first process whose frontmost is true\n' +
'end tell\n' +
'"" & frontAppId & " " & frontAppName';
2019-08-16 23:05:39 +02:00
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';
2020-06-01 16:53:51 +02:00
const AutoTypeHelper = function () {};
2016-04-09 20:58:22 +02:00
2020-06-01 16:53:51 +02:00
AutoTypeHelper.prototype.getActiveWindowInfo = 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);
}
const output = out.trim();
const spaceIx = output.indexOf(' ');
let id = '',
appName = '';
if (spaceIx >= 0) {
id = output.substr(0, spaceIx);
appName = output.substr(spaceIx + 1).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, { id });
2019-08-16 23:05:39 +02:00
}
2017-01-31 07:50:28 +01:00
const parts = out.split('\n');
return callback(null, {
id,
url: parts[0].trim(),
title: (parts[1] || '').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, { id });
2019-08-16 23:05:39 +02:00
}
2017-01-31 07:50:28 +01:00
const parts = out.split('\n');
return callback(null, {
id,
url: parts[0].trim(),
title: (parts[1] || '').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, { id });
2019-08-16 23:05:39 +02:00
}
return callback(null, {
id,
title: out.trim()
});
2016-04-09 22:19:56 +02:00
});
}
});
};
2016-04-09 20:58:22 +02:00
2020-06-01 16:53:51 +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
});
};
2019-09-15 14:16:32 +02:00
export { AutoTypeHelper };