Replace all occurrences of by $file, then add the following lines just after failat 9999:
set file=`requestfile`
if $file eq ""
skip Exit
endif
Btw, the last line should read Lab Exit, not just Lab.
And the first line .key file can be removed.
Or, if you optionally want to be able to call the script with a file name as parameter, keep the first line and change my lines to
set file=""
if $file eq ""
set file=`requestfile`
if $file eq ""
skip Exit
endif
endif