Instead of window 1
I would recommend to use think window 1
which is either a document or viewer window as window 1
might be any window. This simplified version should return the same results:
tell application id "DNtp"
if exists (content record of current tab of think window 1) then
return reference URL of (current tab of think window 1)
else if (count of selected records) is 1 then
return reference URL of selected record 1
else
return reference URL of root of viewer window 1
end if
end tell