How to get the Desktop path when "click on it"

OK, “what” I want applescript to do is very simple.

I want applescript to show the path of the finder window when the finder window which was clicked,
for example:

Click “Documents” window, it’s a finder window of a folder, then it will show “HD:User:XXX:Documents”

by

=========
tell application “Finder”
set target_window to window 1
set target_path to target of target_window as text
display dialog target_path
end tell

Everything works well except when Desktop was clicked,
every Finder window could become “window 1” except Desktop itself,
if I opened the desktop folder as a finder window, of course it could work fine,
but it was unable to make the “Desktop” surface which lies below all windows to become “window 1”.

That means even if I clicked on the Desktop, the “frontmost window” (or window 1, whatever) still would be the last window which I’ve clicked on but not the Desktop, so it was unable to get the path of Desktop when the Desktop surface was clicked on.

Does it make any sense?

What I want is just to get the path of Desktop by “moving the mouse cursor over the Desktop surface and clicking on it”.

The possible way I could imagine is to call Cocoa NSevent, when the “mouse click” event happened, calculate it’s cursor position,
if this curson position without any window lying on, then the surface which was clicked on must be the Desktop.

Could anyone share the detail way? Thanks sincerely.

Hi,

the problem is, the desktop surface is not a window.
But you could select any item on desktop and run this script


tell application "Finder"
	set sel to item 1 of (get selection)
	set target_path to container of sel as text
	display dialog target_path
end tell

I know this way, but what if there’s no any item on the Desktop?

I’ve also tried to make it possible to select the item on desktop by sending keytrokes in applescript,
but it’s unable to do that,because hence you didn’t make the desktop active, you couldn’t select the items on it.