I've tried this instead. Basically I'm trying to take all the artwork on a particular layer and move it to another one. Once I've cleared out the layer I'll delete it so I'll effectively end up flattening the document. However I can't get this to work. Is this because I have to form a GroupItem and move everything into that then move the group item to a different layer first?
var doc = activeDocument;
var destination=app.activeDocument.layers[1]
$.writeln(destination.name);
for (var i = 0; i < doc.layers.length; i++) {
var currentLayer = app.activeDocument.layers[i];
$.writeln(currentLayer.name)
if(currentLayer.name!="Contents") {
var contents=currentLayer.hasSelectedArtwork = true;
contents.moveTo(destination);
}
}