Skip to content

Commit

Permalink
2.4.3
Browse files Browse the repository at this point in the history
fix System Events got an error: Can't get window 1 of process whose value of attribute \"AXMain\" = true
  • Loading branch information
Maigo Erit committed Feb 21, 2017
1 parent f9190e1 commit 4446bf8
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 50 deletions.
94 changes: 56 additions & 38 deletions app/background.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var moment = require('moment');
var path = require('path');
var exec = require("child_process").exec;
var execSync = require("child_process").execSync;
var execFile = require("child_process").execFile;

var emptyItem = {title: 'EMPTY'};

Expand Down Expand Up @@ -284,23 +285,24 @@ BackgroundService.onResume = function () {
BackgroundService.saveForegroundWindowTitle = function () {

//'darwin', 'freebsd', 'linux', 'sunos' or 'win32'
var script = "";
let runExec = "";
let args = [];
if (process.platform === 'darwin') {
//Run applescript from shell.
script = "osascript " + path.join(__dirname, "get-foreground-window-title.osa");
runExec = "osascript";
args.push(path.join(__dirname, "get-foreground-window-title.osa"));
} else if (process.platform === 'win32') {
script = 'powershell.exe "& ""' + path.join(__dirname, "get-foreground-window-title.ps1") + '"""';
runExec = 'powershell.exe';
args.push('"& ""' + path.join(__dirname, "get-foreground-window-title.ps1") + '"""');

} else if (process.platform === 'linux') {
script = "sh " + path.join(__dirname, "get-foreground-window-title.sh");
runExec = "sh";
args.push(path.join(__dirname, "get-foreground-window-title.sh"));
}

//logger.debug('Script saveForegroundWindowTitle file: ' + script);

var child = exec(script, {timeout: 1000});

child.stdout.on("data", (data)=> {
let stdout = data.toString();
var handleSuccess = (stdout)=> {
logger.debug('Foreground window: ' + stdout);

var active = {};
Expand All @@ -326,39 +328,40 @@ BackgroundService.saveForegroundWindowTitle = function () {
logger.debug("Foreground window (parsed):", active);

saveActiveWindow(active);
});

let firstErrorPart = true;
child.stderr.setEncoding('utf8');
child.stderr.on("data", (data) => {
};

let error = data.toString();
var handleError = (error)=> {

if (!firstErrorPart) {
logger.error('Multipart error, ignoring rest');
return;
}
firstErrorPart = false;
logger.error('saveForegroundWindowTitle error: ' + error);

if (error.includes('UnauthorizedAccess') || error.includes('AuthorizationManager check failed')) {
error = 'Choose [A] Always run in opened command prompt.';
execSync('start cmd.exe /K' + script);
}

if (error.includes('assistive')
|| error.includes('get-foreground-window-title')
|| error.includes('390')) {
if (error.includes('assistive') || error.includes('osascript')) {
error = 'Tockler is not allowed assistive access. Security & Privacy -> Accessibility -> enable tockler.app';
}

UserMessages.showError('Error getting window title', error);
return;
};

});
var callcack = (err, stdout, stderr) => {

if (stderr) {
handleError(stderr);
return;
}

child.stdin.end();
if (err) {
handleError(stderr);
logger.error("saveForegroundWindowTitle err", err);
return;
}
handleSuccess(stdout);
};

execFile(runExec, args, {timeout: 2000}, callcack);
};

BackgroundService.saveRunningLogItem = function () {
Expand Down Expand Up @@ -443,29 +446,30 @@ BackgroundService.saveRunningLogItem = function () {
BackgroundService.saveUserIdleTime = function () {

//'darwin', 'freebsd', 'linux', 'sunos' or 'win32'
var script = "";
let runExec = "";
let args = [];
if (process.platform === 'darwin') {
script = "sh " + path.join(__dirname, "get-user-idle-time.mac.sh");
runExec = "sh";
args.push(path.join(__dirname, "get-user-idle-time.mac.sh"));
} else if (process.platform === 'win32') {
script = 'powershell.exe "& ""' + path.join(__dirname, "get-user-idle-time.ps1") + '"""';
runExec = 'powershell.exe';
args.push('"& ""' + path.join(__dirname, "get-user-idle-time.ps1") + '"""');
} else if (process.platform === 'linux') {
script = "sh " + path.join(__dirname, "get-user-idle-time.linux.sh");
runExec = "sh";
args.push(path.join(__dirname, "get-user-idle-time.linux.sh"));
}

//logger.debug('Script saveUserIdleTime file: ' + script)

var child = exec(script, {timeout: 1000});
child.stdout.on("data", (stdout)=> {
var handleSuccess = (stdout)=> {
logger.debug('Idle time: ' + stdout);

var seconds = stdout;

saveIdleTrackItem(seconds);
});

child.stderr.on("data", (data) => {
};

let error = data.toString();
var handleError = (error)=> {
logger.error('saveUserIdleTime error: ' + error);

if (error.includes('UnauthorizedAccess') || error.includes('AuthorizationManager check failed')) {
Expand All @@ -474,12 +478,26 @@ BackgroundService.saveUserIdleTime = function () {
}

UserMessages.showError('Error getting user idle time', error);
return;

});
};

var callcack = (err, stdout, stderr) => {

child.stdin.end();
if (stderr) {
handleError(stderr);
return;
}

if (err) {
handleError(err);
logger.error("saveUserIdleTime err", err);
return;
}

handleSuccess(stdout);
};

execFile(runExec, args, {timeout: 2000}, callcack);
};

BackgroundService.init = function () {
Expand Down
26 changes: 16 additions & 10 deletions app/get-foreground-window-title.osa
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
global frontApp, frontAppName, windowTitle

set windowTitle to ""
tell application "System Events"
set frontApp to first application process whose frontmost is true
set frontAppName to name of frontApp
tell process frontAppName
tell (1st window whose value of attribute "AXMain" is true)
set windowTitle to value of attribute "AXTitle"
end tell
end tell
end tell
set windowTitle to "NO_TITLE_FOUND"
try
tell application "System Events"
set frontApp to first application process whose frontmost is true
set frontAppName to name of frontApp
tell process frontAppName
if exists (1st window whose value of attribute "AXMain" is true) then
tell (1st window whose value of attribute "AXMain" is true)
set windowTitle to value of attribute "AXTitle"
end tell
end if
end tell
end tell
on error errorMsg
set windowTitle to errorMsg
end try

return {frontAppName, windowTitle}
2 changes: 1 addition & 1 deletion app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Tockler",
"version": "2.4.0",
"version": "2.4.3",
"description": "Automatically track applications usage and working time",
"main": "app.js",
"author": "Maigo Erit <[email protected]>",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Tockler",
"version": "2.4.0",
"version": "2.4.3",
"description": "Automatically track applications usage and working time",
"main": "app/app.js",
"scripts": {
Expand Down

0 comments on commit 4446bf8

Please sign in to comment.