it's easy to make a window become frontmost by click it or tell it to active,
except Desktop, it won't be the frontmost window of finder when we click on the desktop,
so,
is there anyway to make the desktop "active"?
Even though AppleScript and Automator are good at detecting changes to folders and then making changes, I think you'll be disappointed at the response at acting on changes you make to your environment, like switching to the Finder.
It'll mean an "on idle" script, which has to be smart enough to know that you've just made the Finder the frontmost app so you want your Desktop folder visible. It'll also have to be smart enough to know that Finder was previously the frontmost app and you either already have the Desktop folder open or you closed it because you want it closed. That's not terribly complex, but the script will do it for every idle message, which will likely mean your machine becomes sluggish. (But maybe you've got a Mac Pro :
You've got a few of alternatives ...
Use an Expose hot corner
Keep the desktop window open at all times
Add the Desktop window as a stack in your Dock
Use the script in this thread to activate the Desktop window (You could use something like Butler that would let you activate it with a keystroke.)
I think I just discovered the command you wanted... I know you're probably not interested by now, but the "insertion location" command does what you want.
For example, the script
Code:
tell application "Finder"
set selectedLocation to insertion location as string
display dialog selectedLocation
end tell
would tell you your active Finder window, even if it was the desktop!