Play mp3 music/audio file asynchronously (for CS2 and CS5)

Photoshop Script Snippets - Note: Full Scripts go in the Photoshop Scripts Forum

Moderators: xbytor, DavideBarranca, Paul MR


Play mp3 music/audio file asynchronously (for CS2 and CS5)

Postby autiokari » Sun Jul 31, 2011 1:12 pm

Hehe. Wanted to have some rock'n roll with Photoshop so spent a couple of hours on this one. Works at least with CS2 and CS5.under Windows OS and because there is not a built-in mp3 player I use a very good freeware command-line tool from the nice people at

The mp3 player ... Play.shtml is easy to use, it is only one exe file, I have it in the same folder that holds the mp3 audio files. A shortcut must be created for it (by wrong-click in WIndows Explorer) and the preferences of that shortcut must be changed to "Run: Minimized" (on the preferences/shortcut -tab). The Javascript code below first creates a vbs script file and then executes it. The vbs script starts the madplay.exe with the target mp3 file.

Note1: I have renamed the shortcut file that I created for the "madplay.exe" as "madplay on background.exe" (In the vbs the filename appear as "madplay on background.exe.LNK" which is correct).

Note2: the below function is raw from my include-file, the variables parentFolder and grandParentFolder are declared elsewhere and contain the paths respectively (as strings). The variable pcID is also declared elsewhere and contain the ID of the computer (can be removed if not needed).

Note3: on the first run Windows may throw a security alert dialog, do uncheck the checkbox: "ask this everytime this program is run" then the dialog will not be presented anymore.

Code: Select allplayMP3=function(MP3file){
var Q="\"\"\"";
var S = " ";
var A = " & ";

var tempvbs=new File(parentFolder+"/temp-" + pcID + ".vbs" );
var soundFile = new File(grandParentFolder + "/sounds/" + MP3file).fsName;
var playerFile= new File(grandParentFolder + "/sounds/" + "madplay on background.exe.LNK").fsName;
var tempvbs=new File(parentFolder+"/temp-" + pcID + ".vbs" );

var t = "Set WshShell = WScript.CreateObject(\"WScript.Shell\")" +"\n";
t = t + "WshShell.Run " + Q + playerFile + Q + A + Q + soundFile +  Q + "\n";"w:");

It is then used like below, and will run asynchronously:

Code: Select allplayMP3("Don Omar - Danza Kuduro - oi oi oi.mp3");

The below code can be used to declare & define the variables in the main script. This way the tempvbs file goes to one folder upwards from the folder where the main script resides and the "/sounds" folder will be a sub-folder of the folder one step upwards from that folder.

Code: Select allvar thisScript=undefined;
try {some_undefined_variable;}
catch (e){thisScript=new File(e.fileName);}

var thisFolder=thisScript.path;
var parentFolder= thisScript.parent.path;
var grandParentFolder= thisScript.parent.parent.path;
var pcID = ($.getenv("COMPUTERNAME")).toLowerCase();

Could even be used to provide verbal instructions!

Have fun!

Return to “Photoshop Scripting: Code Snippets”

Who is online

Users browsing this forum: No registered users and 3 guests