Q:

Why this bug - applescript , automator & applescript editor

My applescript gave correct output when ran inside automator as service with no input but it gives error message when ran outside automator (safari, preview etc.) 
It also gives  correct output when ran through applescript editor, no problem running it with safari, preview or any other application. 
The reason I was so keen to get it done with automator was that I don't want to install a 3rd party app just for assigning one shortcut to one script.
I debugged this portion which after removing from code allowed automator to run the script. Ran it with try, error in automator & viola, no issues at all. 
But I want to know why this code earlier & without try-error function was giving the error messages when ran outside automator application. 
Here's the portion of the script with try-error function: 
try

set writ to do shell script "/usr/bin/python Users/[username]/Documents/tech_toolkit/windtitle"

tell application writ
    if the (count of windows) is not 0 then
        set window_title to name of front window
    end if
end tell

on error

     delay 2

end try

For shell script /windtitle , windtitle is an executable file
#!/usr/bin/env python

from AppKit import NSWorkspace
active_app_name = NSWorkspace.sharedWorkspace().frontmostApplication().localizedName()
print active_app_name

A:

The error is:

Your python script return the "Automator Runner" application when it run as service , it's
the frontmost in the background but not in the foreground.

To get the frontmost application, use this script:
try
    tell application (path to frontmost application as text)
        set window_title to name of front window
        set writ to its name -- get the name of this application
    end tell
on error
    return -- quit this script, because this application is not scriptable, or no window
end try

