From 7b0ba81f2ed86fb108edc2ad0ab2ab1bed2de9f7 Mon Sep 17 00:00:00 2001 From: Maigo Erit Date: Wed, 22 Feb 2017 22:16:37 +0200 Subject: [PATCH] 2.4.3 fix TCCD cpu usage by running osascipt once. --- app/background.service.js | 38 +++++++++++++++++------------ app/get-foreground-window-title.osa | 12 ++++++--- app/package.json | 2 +- app/user-messages.js | 2 +- package.json | 2 +- 5 files changed, 34 insertions(+), 22 deletions(-) diff --git a/app/background.service.js b/app/background.service.js index 4d0b662c..29963560 100755 --- a/app/background.service.js +++ b/app/background.service.js @@ -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'; @@ -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.'; @@ -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 () { diff --git a/app/get-foreground-window-title.osa b/app/get-foreground-window-title.osa index 2b0bb4ab..ac485bef 100755 --- a/app/get-foreground-window-title.osa +++ b/app/get-foreground-window-title.osa @@ -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 @@ -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} \ No newline at end of file +return {frontAppName, windowTitle} \ No newline at end of file diff --git a/app/package.json b/app/package.json index e8243507..59064280 100644 --- a/app/package.json +++ b/app/package.json @@ -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 ", diff --git a/app/user-messages.js b/app/user-messages.js index 2d596eed..9fb7151a 100644 --- a/app/user-messages.js +++ b/app/user-messages.js @@ -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 = ''; }); } } diff --git a/package.json b/package.json index a4c82f7c..b59d9c2a 100644 --- a/package.json +++ b/package.json @@ -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": {