Different behavior of script when exported as an application


I have been hitting brick walls for the last couple of days with the following:

I have developed an applescript to automate some actions with the UI of an OCR application. The script works perfectly when executed as script and within the AppleScript Editor but behaves differently when I export it as an application (using the AppleScript editor export) or when I create an Automator application with it.

I have done some testing and it seems that any script command related to UI Elements fails when executed after having been exported as an app. And this is not due to the OCR application: I replicated the issue with the following script using Textedit:

tell application "TextEdit" to activate

tell application "System Events"
	tell process "TextEdit"
			UI elements
		on error
			display dialog "Error" buttons {"OK"}
		end try
	end tell
end tell

The “UI elements” command in the above script can be substituted with any command referring to a window, a menu, … and the result is always the same : it works perfectly when executed within the AppleScript Editor (no Error dialog box is shown). When exported as an application or embedded within an Automator application, the “UI elements” command fails and it always displays the Error dialog box).

I am running Mavericks 10.9.3 (AppleScript 2.3.2)

Any help will be welcome.

Maybe this url will help… http://www.macosautomation.com/mavericks/guiscripting/index.html