UI login apple script not running on 10.8.3 but working fine on 10.6.x

I had coded an applescript that login the user on back end when MAC starts.
NSAppleScript *runscript;

	//NSLog(@"comes to applescript");
	//////////NEW CODE FROM HERE//////////

NSString *scriptString = @"set username_ to \"Administrator\"\n"
						  "set password_ to \"vsi9217\"\n"
						  "tell application \"System Events\"\n"
						  "key code 125\n"
						  "key code 125\n"
						  "delay 1\n"
						  "key code 125\n"
						  "key code 125\n"
						  "key code 125\n"
						  "key code 125\n"
						  "key code 125\n"
						  "key code 125\n"
						  "delay 0.5\n"
						  "key code 36\n"
						  "delay 1\n"
						  "tell process \"SecurityAgent\" to set value of text field 1 of group 1 of window 1 to username_\n"
						  "tell process \"SecurityAgent\" to set value of text field 2 of group 1 of window 1 to password_\n"
						  "click button \"Log In\" of window 1 of application process \"SecurityAgent\"\n"
						  "end tell\n";

NSLog(@"%@",scriptString);





runscript = [[NSAppleScript alloc] initWithSource:scriptString];

[runscript executeAndReturnError:nil];
[runscript release];

its working fine and perfect on 10.6.x but when i updated my OS to 10.8.3 script is not working . Kindly help.

Model: iMac
Browser: Firefox 19.0
Operating System: Mac OS X (10.8)

Hi,

in Mountain Lion there is no group 1 in the Security Agent window,
anyway I would write for robustness


tell application "System Events"
.

.
	repeat until exists process "SecurityAgent"
		delay 0.5
	end repeat
	tell window 1 of process "SecurityAgent"
		set value of text field 1 to username_
		set value of text field 2 to password_
		keystroke return
	end tell
end tell


Thank you very much Stefan.
Can you plz refer me some links or tutorials where i can find more detail about systemevents,groups and securitywindows.
Thanks

i am facing one problem.
that is Keystroke return is not working
i am also applying keycode 36 , but that’s also not working.

Model: iMac
Browser: Firefox 19.0
Operating System: Mac OS X (10.8)

GUI scripting is a pain and a lot of trial and error

If the window stays visible when you switch to another application you can see the UI hierarchy with tools like UIElementInspector or UIBrowser.

If not, you can only get information by logging relevant parameters like number of elements name/title of an UI element etc.

Instead of

keystroke return 

you can try

click button "OK"

or

click button 1

Yes…its really painful.Thanks for your effort. After many try i found the problem…
There is no “Log In” button in 10.8.x or 10.7.x
Now i want to fill the password field and enter the return key while password field selected
when i am trying to enter the key through scripting its not working because password is not selected.
any idea how to do these two steps in one go?