Can you give the error that you get for the script?

First try with just a simple statement of 2-3 lines, instead of going for the whole code.
See if it works.