If you really need this as JavaScript, you can (probably) convert it to a JS function with this script:
http://ps-scripts.cvs.sourceforge.net/viewvc/ps-scripts/xtools/apps/ActionToJavascript.jsx
I say "probably" because the translation script works about 98% of the time. Some file paths and other more obscure things will trip it up on occasion.