Running a script on opening a view

Running a script on opening a view

So I have a very simple script which i am currently running from within a form (on input). This script basically does an aggregate sum based on some criteria.

I would like to run this script when I open up a view. 

How do I do this ? 

I've tried pasting the script in the view definition window but it doesn't compile.

Any help ?