Checking for existance

March 31, 2007

There’s no built-in for checking if a file exists when developing Dashboard Widgets in JavaScript. But with a little help from the BSD subsystem we can do it easily. For this to work, you obviously need to have

<key>AllowSystem</key>

<true/>

set in your Info.plist. That opens up a whole world of possibilities. I find that the most effective way is to use file. This way we can even check if something is of the correct type. For example, for a recent UnitInfo update, I had to check if InCrease was installed:

foldingBase = "~/Library/InCrease/cpu1/";

var fp = widget.system ('/usr/bin/file -bi ' + foldingBase + 'unitinfo.txt', null).outputString.replace ("\n", "");



if (fp.indexOf ("text/plain") != -1)

{

  clientVersion = 'InCrease';

} else {

  foldingBase = "~/Library/Folding@Home/";

  clientVersion = 'Folding@Home';

}



unitInfoFile = foldingBase + 'unitinfo.txt';

clientConfigFile = foldingBase + 'client.cfg';