That's just telling you that the $modx object doesn't exist. It would if the code were in a snippet, but if you're running the script outside of MODX, you have to instantiate $modx.
See this:
http://bobsguides.com/blog.html/2013/07/13/using-modx-outside-of-modx/