Skip to content

Instantly share code, notes, and snippets.

@ofstudio
Created December 28, 2014 12:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ofstudio/583de48e902917c1eeec to your computer and use it in GitHub Desktop.
Save ofstudio/583de48e902917c1eeec to your computer and use it in GitHub Desktop.
Example of iTunes scripting in OS X with Javascript
/**
* Example of iTunes scripting in OS X with Javascript
* Open /Applications/Script Editor.app, paste the code and click Run button
*
* @author Oleg Fomin <ofstudio@gmail.com>
*
*/
var my_app = Application.currentApplication(),
itunes = new Application('iTunes'),
selection;
my_app.includeStandardAdditions = true;
itunes.includeStandardAdditions = true;
selection = itunes.selection();
if (selection.length > 0) {
for (var i in selection) {
itunes.play(selection[i]);
my_app.displayNotification (
selection[i].artist() + ' is my favorite artist',
{
withTitle: 'Cool jukebox',
subtitle: 'Now playing: ' + selection[i].name()
}
);
delay(7);
}
} else {
itunes.activate();
itunes.displayDialog('Please select a few tracks in iTunes and try again!');
}
@northamerican
Copy link

hi oleg. i’m wondering if you’ve tried running any scripts from inside itunes rather than from script editor.
i've made a script that parses discogs.com genres and applies them to selected tracks in itunes. it works fine from inside script editor but when saved and run from itunes, it hangs.
first i save my script to ~/Library/iTunes/Scripts, then in itunes, click on the script icon that appears in the menu bar next to 'help' and select my script. you should see that the js will not work.

the strange thing is that all scripts done in javascript make itunes hang.

this applescript runs fine:

tell application "iTunes"
    display dialog "test"   
end tell

but the equivalent javascript doesn't:

var iTunes = new Application('iTunes');
iTunes.includeStandardAdditions = true;
iTunes.activate(); 
iTunes.displayDialog('test');
//or
var iTunes = Application('iTunes');
iTunes.includeStandardAdditions = true;
iTunes.displayDialog('test');

the javascript versions work if run from script editor.

here is my script if you're interested:
https://gist.github.com/north-is-northwest/16fadb1d945cd5c01d9a

it seems very few people have yet used javascript for itunes and there is little documentation. i'm wondering if you've tried this or have any ideas? thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment