Monday, November 19, 2007

Solution to Wscript and HTML Integration

Much to my disgrace, I could get WSCript to work with HTML for hours.
The error "WScript is undefined" has puzzled me throughout the night, but finally the problem is solved.


What's the Problem?


According to MSDN, "the WScript object is the root object of the Windows Script Host object model hierarchy. It never needs to be instantiated before invoking its properties and methods, and it is always available from any script file. "

Simply put WScript provides access to root object for the Windows Script Host object model.


WScript is the default Windows Script Host Object, HTML can only communicate with it via ActiveX. So that's why we need ActiveXObject() instead of WScript.CreateNewOjbect(). And instead of WScript.Echo(); we have to use window.alert which is a built-in function for Javascript.



The initial js Code:

var iTunesApp = WScript.CreateObject("iTunes.Application");
WScript.Echo(allPlaylistNames());

function allPlaylistNames() {

var librarySource = iTunesApp.LibrarySource;
var listOfPlaylists = librarySource.Playlists;
var playlistReturn = new Array();

for(i = 1; i <= listOfPlaylists.Count; i++) { playlistReturn[i] = listOfPlaylists.Item(i).Name + '[[sep]]p_' + i; } return playlistReturn.sort(); }


Change it to :

var iTunesApp = new ActiveXObject("iTunes.Application");
window.alert(allPlaylistNames());

function allPlaylistNames(){
var librarySource = iTunesApp.LibrarySource;
var listOfPlaylists = librarySource.Playlists;
var playlistReturn = new Array();

for(i = 1; i <= listOfPlaylists.Count; i++) {
playlistReturn[i] = listOfPlaylists.Item(i).Name + '[[sep]]p_' + i;
}
return playlistReturn.sort();

}

References:

http://msdn2.microsoft.com/en-us/library/at5ydy31.aspx
http://thegreenbutton.com/forums/thread/84583.aspx
http://microsoft.apress.com/asptodayarchive/73503/accessing-the-windows-system-using-activex-and-html

3 comments:

PPPFFF said...

Hi !
have just googled your "Solution" because I have started to experiment with the iTunes Interface to HTML and Javascript. In Windows Scripting (WSH) I am quite successful, but I have difficulties when I reach the HTML thing.
Have tried your solution but here I am stuck.
Do you have a complete example including the HTML part ?
My idea is to have a little slide show with pictures from the artist.
best regards
Peter

[T] said...

Peter, at the moment I don't have the HTML version of the code. I think you should be able to use the code in javascript, put them in the "head" and get it running. I only took advantage of ActiveX in order to utilize iTunes from a webpage.

®¿® said...

I like it.

Thanks man. Saved me some time.