It's amazing that it could detect the Spotlight search window.
How about "Alfred"?
I changed Spotlight into Alfred and it does not work.
Edit: Turns out I made a mistake. There were two places to change and I changed only one place. It's working great now!
From this post, an answer provided by Chris as well.
Code for Alfred:
tell application "System Events"
tell application process "Alfred"
if (get name of windows) contains "Alfred" then
return true
else
return false
end if
end tell
end tell