Skip to content

Commit

Permalink
2.4.3 fix TCCD cpu usage by running osascipt once.
Browse files Browse the repository at this point in the history
  • Loading branch information
Maigo Erit committed Feb 22, 2017
1 parent 4446bf8 commit 7b0ba81
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 22 deletions.
38 changes: 22 additions & 16 deletions app/background.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,14 +282,17 @@ BackgroundService.onResume = function () {
isSleeping = false;
};

let oneThreadRunning = false;
BackgroundService.saveForegroundWindowTitle = function () {

//'darwin', 'freebsd', 'linux', 'sunos' or 'win32'
let runExec = "";
let args = [];
let oneThread = false;
if (process.platform === 'darwin') {
//Run applescript from shell.
runExec = "osascript";
oneThread = true;
args.push(path.join(__dirname, "get-foreground-window-title.osa"));
} else if (process.platform === 'win32') {
runExec = 'powershell.exe';
Expand Down Expand Up @@ -332,7 +335,7 @@ BackgroundService.saveForegroundWindowTitle = function () {

var handleError = (error)=> {

logger.error('saveForegroundWindowTitle error: ' + error);
//logger.error('saveForegroundWindowTitle error: ' + error);

if (error.includes('UnauthorizedAccess') || error.includes('AuthorizationManager check failed')) {
error = 'Choose [A] Always run in opened command prompt.';
Expand All @@ -341,27 +344,30 @@ BackgroundService.saveForegroundWindowTitle = function () {

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.toString());
}

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

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

if (stderr) {
handleError(stderr);
return;
}
if (oneThread == false || oneThread == true && oneThreadRunning == false) {
console.log('...')
oneThreadRunning = true;
let ls = execFile(runExec, args);
ls.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});

ls.stderr.on('data', (data) => {
handleError(data);
handleSuccess(data);
});

ls.on('close', (code) => {
console.log(`child process exited with code ${code}`);
});
}

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

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

BackgroundService.saveRunningLogItem = function () {
Expand Down
12 changes: 9 additions & 3 deletions app/get-foreground-window-title.osa
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
global frontApp, frontAppName, windowTitle
global frontApp, frontAppName, windowTitle

set windowTitle to "NO_TITLE_FOUND"
repeat
set frontAppName to "NO_APP_FOUND"
set windowTitle to "NO_TITLE_FOUND"
try
tell application "System Events"
set frontApp to first application process whose frontmost is true
Expand All @@ -16,5 +18,9 @@
on error errorMsg
set windowTitle to errorMsg
end try
set output to (frontAppName & "," & windowTitle)
log output
delay 3
end repeat

return {frontAppName, windowTitle}
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.3",
"version": "2.4.4",
"description": "Automatically track applications usage and working time",
"main": "app.js",
"author": "Maigo Erit <[email protected]>",
Expand Down
2 changes: 1 addition & 1 deletion app/user-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class UserMessages {
wait: false // Wait with callback, until user action is taken against notification
}, function (err, response) {
logger.error('Notifier error:', err, response);
UserMessages.lastError = '';
//UserMessages.lastError = '';
});
}
}
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.3",
"version": "2.4.4",
"description": "Automatically track applications usage and working time",
"main": "app/app.js",
"scripts": {
Expand Down

0 comments on commit 7b0ba81

Please sign in to comment.