Rename a node by name property

import info.magnolia.jcr.util.NodeUtil;
import info.magnolia.jcr.util.PropertyUtil

hm = MgnlContext.getWebContext().getJCRSession('website')
nodes = hm.getNode('/path/to/nodes')

pages = NodeUtil.getNodes(nodes)

pages.each{ def page ->
    
    NodeUtil.renameNode(page, PropertyUtil.getString(page, "name"))
	hm.save()
	
}
return "pages changed"