diff --git a/app/background.service.js b/app/background.service.js index fe70f5f3..4d0b662c 100755 --- a/app/background.service.js +++ b/app/background.service.js @@ -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'}; @@ -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 = {}; @@ -326,19 +328,10 @@ 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')) { @@ -346,19 +339,29 @@ BackgroundService.saveForegroundWindowTitle = function () { 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 () { @@ -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')) { @@ -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 () { diff --git a/app/get-foreground-window-title.osa b/app/get-foreground-window-title.osa index 574ae45b..2b0bb4ab 100755 --- a/app/get-foreground-window-title.osa +++ b/app/get-foreground-window-title.osa @@ -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} \ No newline at end of file diff --git a/app/package.json b/app/package.json index e0750780..e8243507 100644 --- a/app/package.json +++ b/app/package.json @@ -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 ", diff --git a/package.json b/package.json index f7f9e910..a4c82f7c 100644 --- a/package.json +++ b/package.json @@ -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": {