Skip to content

Commit

Permalink
Factor out the Spotify OSAScript stuff to its own file
Browse files Browse the repository at this point in the history
Now that we have ESM support in Electron, we can factor out stuff into helper files.
  • Loading branch information
TomasHubelbauer committed Jun 3, 2024
1 parent a733dfa commit 9548f0f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 41 deletions.
16 changes: 16 additions & 0 deletions askSpotify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import child_process from 'child_process';
import util from 'util';

const exec = util.promisify(child_process.exec);

export default async function askSpotify(
/** @type {string} */
query
) {
const { stdout, stderr } = await exec(`osascript -e 'tell application "Spotify" to ${query}'`);
if (stderr) {
throw new Error(stderr);
}

return stdout.trimEnd();
}
48 changes: 7 additions & 41 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import electron from 'electron';
import child_process from 'child_process';
import util from 'util';
import timers from 'timers/promises';
import fs from 'fs';
import askSpotify from './askSpotify.js';

electron.app.on('ready', async () => {
const exec = util.promisify(child_process.exec);

// Hide the Dock icon for the application
electron.app.dock.hide();

Expand Down Expand Up @@ -109,12 +106,7 @@ electron.app.on('ready', async () => {
/** @type {string} */
let artist;
try {
const { stdout: artistStdout, stderr: artistStderr } = await exec(`osascript -e 'tell application "Spotify" to artist of current track'`);
if (artistStderr) {
throw new Error(artistStderr);
}

artist = artistStdout.trimEnd();
artist = await askSpotify('artist of current track');
}
catch (error) {
console.log('Failed to get Spotify artist: ' + error);
Expand All @@ -124,12 +116,7 @@ electron.app.on('ready', async () => {
/** @type {string} */
let song;
try {
const { stdout: songStdout, stderr: songStderr } = await exec(`osascript -e 'tell application "Spotify" to name of current track'`);
if (songStderr) {
throw new Error(songStderr);
}

song = songStdout.trimEnd();
song = await askSpotify('name of current track');
}
catch (error) {
console.log('Failed to get Spotify song: ' + error);
Expand All @@ -154,13 +141,8 @@ electron.app.on('ready', async () => {
/** @type {string} */
let id;
try {
const { stdout: idStdout, stderr: idStderr } = await exec(`osascript -e 'tell application "Spotify" to id of the current track'`);
if (idStderr) {
throw new Error(idStderr);
}

// E.g.: "ID spotify:track:…"
id = idStdout.split(':').at(-1).trimEnd();
id = (await askSpotify('id of the current track')).split(':').at(-1).trimEnd();
}
catch (error) {
console.log('Failed to get Spotify ID: ' + error);
Expand All @@ -186,13 +168,7 @@ electron.app.on('ready', async () => {
}

try {
const { stdout: stateStdout, stderr: stateStderr } = await exec(`osascript -e 'tell application "Spotify" to player state'`);
if (stateStderr) {
throw new Error(stateStderr);
}

const _state = stateStdout.trimEnd();

const _state = await askSpotify('player state');
if (_state !== 'playing' && _state !== 'paused') {
throw new Error(`Unexpected player state '${_state}'!`);
}
Expand All @@ -214,25 +190,15 @@ electron.app.on('ready', async () => {
}

try {
const { stdout: positionStdout, stderr: positionStderr } = await exec(`osascript -e 'tell application "Spotify" to player position'`);
if (positionStderr) {
throw new Error(positionStderr);
}

position = Number(positionStdout);
position = Number(await askSpotify('player position'));
}
catch (error) {
console.log('Failed to get Spotify position: ' + error);
continue;
}

try {
const { stdout: durationStdout, stderr: durationStderr } = await exec(`osascript -e 'tell application "Spotify" to duration of current track'`);
if (durationStderr) {
throw new Error(durationStderr);
}

duration = Number(durationStdout) / 1000;
duration = Number(await askSpotify('duration of current track')) / 1000;
}
catch (error) {
console.log('Failed to get Spotify duration: ' + error);
Expand Down

0 comments on commit 9548f0f

Please sign in to comment.