Skip to content

Commit 00bd36d

Browse files
committed
getPlayerState is now returning « not running » if iTunes is closed.
1 parent 3726eff commit 00bd36d

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

example.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ if(currentPlayerState == 'playing') {
1212

1313
var exampleMsg = 'iTunes is currently paused';
1414
console.log(exampleMsg)
15-
}else{ // If iTunes is not playing or paused, we assume that he is stopped
15+
}else if (currentPlayerState == "not running"){
1616

17-
var exampleMsg = "iTunes is not playing at the moment.";
17+
var exampleMsg = "iTunes is not running at the moment.";
1818
console.log(exampleMsg);
19-
}
19+
}

itunes-bridge.js

+18-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
var applescript = require('run-applescript');
66
var secToMin = require('sec-to-min'); // This is required for the duration of the track as iTunes returns the duration in seconds.
7+
var { execSync } = require('child_process');
78
var exports = module.exports = {};
89

910

@@ -86,11 +87,10 @@ exports.getCurrentTrackElapsedSeconds = function() {
8687
}
8788
};
8889
exports.getPlayerState = function() {
89-
// TODO: Check if iTunes is launched as another check before the AppleScript one
90-
try {
90+
if(isAppRunning("iTunes")) {
9191
return applescript.sync('tell application "iTunes" to get the player state');
92-
}catch(err){
93-
return null;
92+
}else{
93+
return "not running";
9494
}
9595
};
9696
exports.getPlaylistCount = function() {
@@ -108,3 +108,17 @@ exports.getCurrentTrackElapsedSeconds = function() {
108108
return null;
109109
}
110110
};
111+
112+
113+
114+
115+
116+
function isAppRunning(app){
117+
try {
118+
execSync('pgrep -x "'+app+'"');
119+
return true;
120+
}
121+
catch (err) {
122+
return false;
123+
}
124+
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "itunes-bridge",
3-
"version": "0.1.8",
3+
"version": "0.1.9",
44
"description": "A NodeJS package to control and get informations from iTunes through AppleScript",
55
"main": "itunes-bridge.js",
66
"repository": {

0 commit comments

Comments
 (0)