Tuesday, 9 March 2010

Java Script

I agree with Douglas Crockford that Javascript is the worlds most misunderstood language.

Snippets of code and unfinished applications

See AutoMenu for a script idea that might even be useful

Windows Scripting Host

Javascript can be used as a kind of shell script.

I have one working example of a useful script and some ideas for some more; see the next section.

Playlists, Javascript and offline storage

The working script simply scours a directory tree looking for playlists (m3u and pls) and music files (ogg and mp3) and generates new playlists in a user specified directory. The reason for its existence is that my hard disk is too small to hold all my music so I have to burn cds; the problem then is that the playlists are on the CD instead of the hard disk. By creating a different root directory on each CD (mymusic1, mymusic2, etc.) this script solves the problem by generating new playlists that contain absolute paths so that I can have the playlists present even if the files to which they refer are not.

Unfortunately this creates a new problem: double clicking a playlist launches the default player (Zinf in my case) which might then find that the files referred to by the playlist are not available because the cd is not present or is the wrong one.

So now to the idea for solving it: instead of launching the default player launch a script instead. The script would inspect the playlist and check for the availability of the files to which it refers; if they are available it would just launch the player, if not it would prompt the user to insert the appropriate disk.

Unfortunately there are several obvious extensions to this that would make it so much more useful; I understand exactly how Babbage felt. By the way, does anyone know if Ken Mcleod's babbages are even theoretically feasible?

The first idea is that not only should the presence or absence of the files be determined but also that when the files are found to be on a removable medium that they should be copied to a cache on the local disk. The playlist must then be rewritten to refer to the cached copy and the player launched. Sounds good doesn't it; unfortunately the cache is, by definition, limited in size so there will come a time when something must be discarded. This means that the playlist must be restored so that it once again refers to the offline copy and the local copies of the files discarded. To make this work we must do several things:

  • keep track of the date and time that the playlists are launched,
  • keep a copy of the original playlist,
  • decide when the cache is full, either by catching a disk full exception or by monitoring the actual size of the cache,

Other thoughts:

  • should we also trap launches of individual files from the cache and regard them in the same light as launches of the playlist of which they are a part,
  • what happens if a file is part of more than one playlist?

Resolution:

  • assume that files are in only one playlist (reasonable for me, perhaps not for everyone),
  • assume that only playlists are launched; that is, only trap launches of playlists,
  • assume that all playlists are in the same directory.

Implementation:

  • set aside a folder for the cache,
  • use a file to record the access time of the playlist because we cannot rely on the operating system (Windoze) to do this for us. We will write the date and time into the file rather than rely on the creation or modification date,
  • use Folder.Size to determine the current size of the cache as well as trapping disk full exceptions during copying,
  • defer the Folder.Size check until after copying so that it happens in the background while the player is playing,
  • store a copy of the original playlist in the cache so that it can be restored if the cached files are deleted,

Cache layout

The root of the cache should contain an options file telling the maximum size of the cache and any other global settings we might need. In the first version we can hard code these things.

A copy of the original playlist will be stored in the cache root directory.

The files will be stored in a directory tree just as they were found except that it is below the cache root. This means that the rewrite of the playlist is a simple substitution of the root of the path of each file.

No comments:

Post a Comment

Blog Archive

Followers