how to tell if a selected item is on desktop, or in folder of desktop?

G’day

I’ve tried everything I can think of, but the Finder does not distinguish between an item selected on the desktop, or the same item selected in a folder of the desktop.

I’ve tried paths, id’s, and anything that’s visible in the window itself. :frowning:

The script below is typical. Everything referred to selected on the desktop returns as though it’s selected in a desktop window of the desktop itself. Even the path aliases are the same.


tell application "Finder"
	try
		set thelist to (selection as list)
		set distribute to number of items in thelist
		repeat with x from 1 to distribute
			try
				set thisItem to item x of thelist
				set temp to id of window of ((folder of thisItem))
				display dialog temp as string
			on error
				display dialog "Desktop"
				error
			end try
		end repeat
	end try
end tell


Any advice appreciated.

Santa

Santa:

Is this close to what you are looking for?


tell application "Finder"
	try
		set thelist to (selection)
		repeat with x in thelist
			try
				set temp to x's container
				display dialog temp as string
			on error
				display dialog "Desktop"
				error
			end try
		end repeat
	end try
end tell


Thanks Craig

I wish it were that easy.

Selecting icons either in a desktop window or on the desktop itself returns the same string using your script.

Regards

Santa :confused:

Model: G5 1.8 GHz
AppleScript: 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Here’s a small variation of your script.


tell application "Finder"
	try
		set thelist to (selection)
		repeat with x in thelist
			try
				set temp to id of window of x's container
				display dialog temp as string
			on error
				display dialog "Desktop"
				error
			end try
		end repeat
	end try
end tell

If a Desktop Finder Window is open, the script always points to the id value of the window, no matter if the icons selected are in the window, or on the desktop. Without the desktop window open, the script errors out.

I need to distinguish if the icons selected are in the desktop window, or on the desktop.

It’s no big deal, just frustrating!!!

Santa

Model: G5 1.8 GHz
AppleScript: 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Thats not going to work. what if the window open is the home folder and the in list view. with items selected from the desktop folder and other items from else where.

try this

tell application "Finder"
	set thelist to (selection)
	repeat with x in thelist
		
		set temp to name of x's container as string
		if temp is not equal to "Desktop" then
			display dialog temp as string
		else
			
			display dialog "Desktop"
		end if
	end repeat
	
end tell

Hello

I don’t understand Santa’s responce.

If I select an icon sitting in an open window (whose folder is on the desktop) the log report is something like:

tell application “Finder”
get selection
{document file “IMG_1503.jpg” of folder “keywordtest files” of folder “Desktop” of folder “yvankoenig” of folder “Users” of startup disk}
get container of document file “IMG_1503.jpg” of folder “keywordtest files” of folder “Desktop” of folder “yvankoenig” of folder “Users” of startup disk
folder “keywordtest files” of folder “Desktop” of folder “yvankoenig” of folder “Users” of startup disk
get folder “keywordtest files” of folder “Desktop” of folder “yvankoenig” of folder “Users” of startup disk
“Macintosh HD:Users:yvankoenig:Desktop:keywordtest files:”
display dialog “Macintosh HD:Users:yvankoenig:Desktop:keywordtest files:”
{button returned:“OK”}
end tell

If I select an icon which is directly on the desktop, the log report looks like:

tell application “Finder”
get selection
{document file “DSC_0312.JPG” of folder “Desktop” of folder “yvankoenig” of folder “Users” of startup disk}
get container of document file “DSC_0312.JPG” of folder “Desktop” of folder “yvankoenig” of folder “Users” of startup disk
folder “Desktop” of folder “yvankoenig” of folder “Users” of startup disk
get folder “Desktop” of folder “yvankoenig” of folder “Users” of startup disk
“Macintosh HD:Users:yvankoenig:Desktop:”
display dialog “Macintosh HD:Users:yvankoenig:Desktop:”
{button returned:“OK”}
end tell

This clearly shows the status difference.

Yvan KOENIG

G’day from Oz.

Thanks for the advice, but you’re all missing the point.

I’ll try to explain.

  1. Open a window

  2. In the sidebar (turn sidebar on if off), click on the ‘Desktop’ icon. This opens up a window which holds all the icons that reside on the desktop. Effectively they’re showing in two places at once, on the desktop, and in the window of the desktop. They have different arrangements.

  3. Select some icons in the window in icon view (which is a window OF the Desktop), and run my script, or Marks, and the resultant path will show ‘Desktop’.

  4. Select some icons on the desktop, and run the scripts. They have the same result.

  5. How can I distinguish between the ACTUAL desktop icons being selected, and the DESKTOP WINDOW icons being selected.

It’s trivial to identify icons in any other window, but how to in a desktop window versus the actual desktop is my conundrum.

Have I opened up a can of worms? :stuck_out_tongue:

Regards

Santa

Model: G5 1.8 GHz
AppleScript: 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

OK

English is not my native language and so I didn’t understand that you wher writing about a window of the Desktop itself.

I tried the script grabbing the window’s ID.

It seems that it is not good because, when a desktop window exists, even if I select an icon from the plain dektop, I get the window ID so the identification is not made.

From plain desktop

tell application “Finder”
get selection
{document file “testDB2.cwk” of folder “Desktop” of folder “yvankoenig” of folder “Users” of startup disk}
get id of window of container of document file “testDB2.cwk” of folder “Desktop” of folder “yvankoenig” of folder “Users” of startup disk
16
display dialog “16”
{button returned:“OK”}
end tell

From “Desktop as a window”

tell application “Finder”
get selection
{document file “applescript_examples_cd.dmg” of folder “Desktop” of folder “yvankoenig” of folder “Users” of startup disk}
get id of window of container of document file “applescript_examples_cd.dmg” of folder “Desktop” of folder “yvankoenig” of folder “Users” of startup disk
16
display dialog “16”
{button returned:“OK”}
end tell

Yvan KOENIG

Hello Santa,

I need to go to sleep, so here’s a crude script using System Events. You probably can fix it up.


tell application "Finder"
	activate
	set s to (selection)
	set i to item 1 of s
	set c to (container of i) as alias
	set n to name of c
	if n is "Desktop" then
		if exists window "Desktop" then
			-- get selected desktop
			tell process "Finder" of application "System Events"
				set is_window to focused of window "Desktop"
			end tell
		else
			-- selection is in desktop_object
			set is_window to false
		end if
	end if
end tell

You probably can get rid of some stuff. Interesting puzzle.

Edited: btw, I would stay away from working with both the desktop and the desktop window. Wierd things happen with icons here in Jaguar. I don’t know how it’s going in Tiger.

gl,

Hello kel

I wonder.

I ran your script (after changing the name of the window) as on a french system it is not “Desktop” but “Bureau”.

I just added a display dialog at end to get the result with the script saved as an application.

Even when I double-click the script’s icon from the window, the result is “false” (I am running Mac OS X 10.4.8)

For me, it’s just matter of curiousity because I rarely open the desktop as a window (except when I had too much items on this dawn beast).

Here is the script as I ran it:


tell application "Finder"
	activate
	
	set s to (selection)
	set i to item 1 of s
	set c to (container of i) as alias
	set n to name of c
	if n is "Desktop" then
		(* grab the local name of the Desktop window *)
		set windowName to get localized string "N5"
		if exists window windowName then
			-- get selected desktop
			tell process "Finder" of application "System Events"
				set is_window to focused of window windowName
			end tell
		else
			-- selection is in desktop_object
			set is_window to false
		end if
	end if
	display dialog "" & is_window
end tell

Yvan KOENIG (from FRANCE jeudi 12 octobre 2006 18:51:53)

Hi Yvan,

I don’t have Tiger, so am not sure about this. Using System Events beta in Jaguar, if the front window is the desktop (i.e. desktop object has the focus), then it has no name. As a ui element, it is referenced with ‘window 1’. Something like this might work:


tell application "Finder" to activate
tell application "System Events"
	tell process "Finder"
		set n to name of window 1
	end tell
end tell
-- if front window is desktop object,
-- then it has no name and n is not defined
try
	n
	set prompt to "Finder Window"
on error
	set prompt to "Desktop Object"
end try
activate
display dialog prompt

If using the window’s name still doesn’t work, then you might be able to use something else in System Events like ui elements. In Jaguar, getting ui elements of window 1 returns undefined for the desktop object.

gl,

G’day People
Kel, I had to add ‘try’ to your script to get it to work. It works fine as long as the Window of the Desktop is not frontmost, then it returns "Finder Window’ even when only icons on the desktop are selected.

Thanks anyway

Regards

Santa


tell application "Finder" to activate
tell application "System Events"
	try
		tell process "Finder"
			set n to name of window 1
		end tell
	end try
end tell
-- if front window is desktop object,
-- then it has no name and n is not defined
try
	n
	set prompt to "Finder Window"
on error
	set prompt to "Desktop Object"
end try
activate
display dialog prompt

Model: G5 1.8 GHz
AppleScript: 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Hi Santa,

That means that n is defined. What is n in both cases?

gl,

n is defined as ‘Desktop’ both for the window and actual desktop.

The finder seems unable to distinguish between the two, yet it keeps the icons arranged differently.

Santa

Model: G5 1.8 GHz
AppleScript: 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Edit.

It’s because weird things happen to the desktop icons when the desktop window is re-arranged by scripting, that I want to distinguish between the two.

Hi Santa,

What do you get in both cases when you run this?


tell application "Finder" to activate
tell application "System Events"
	tell process "Finder"
		tell window 1
			set uie to (UI elements)
		end tell
	end tell
end tell
return uie

gl,

When the desktop window is frontmost…

{tool bar 1 of window “Desktop” of application process “Finder” of application “System Events”, static text “Desktop” of window “Desktop” of application process “Finder” of application “System Events”, grow area 1 of window “Desktop” of application process “Finder” of application “System Events”, button 1 of window “Desktop” of application process “Finder” of application “System Events”, button 2 of window “Desktop” of application process “Finder” of application “System Events”, button 3 of window “Desktop” of application process “Finder” of application “System Events”, button 4 of window “Desktop” of application process “Finder” of application “System Events”, group 1 of window “Desktop” of application process “Finder” of application “System Events”, splitter group 1 of window “Desktop” of application process “Finder” of application “System Events”}

When a desktop icon is selected…

{tool bar 1 of window “Desktop” of application process “Finder” of application “System Events”, static text “Desktop” of window “Desktop” of application process “Finder” of application “System Events”, grow area 1 of window “Desktop” of application process “Finder” of application “System Events”, button 1 of window “Desktop” of application process “Finder” of application “System Events”, button 2 of window “Desktop” of application process “Finder” of application “System Events”, button 3 of window “Desktop” of application process “Finder” of application “System Events”, button 4 of window “Desktop” of application process “Finder” of application “System Events”, group 1 of window “Desktop” of application process “Finder” of application “System Events”, splitter group 1 of window “Desktop” of application process “Finder” of application “System Events”}

The process selects and activates the window, so the return is the same.

Santa

Thought I’d try this, but get a script error.

I’m a newbie, so can’t work out why.


tell application "Finder" to activate
set temp to (selection as list)
set temp to (selection)
set x to item 1 of temp
set xx to container of x
tell application "System Events"
	tell process "Finder"
		try
			tell xx
				set uie to (UI elements)
			end tell
		end try
	end tell
end tell
return uie

Santa

Well, I worked out I neglected the try loop, but it still bombs on the line ‘tell xx’. How do I get the UI (user interface?) elements of the container?

tell application "Finder" to activate
try
	set temp to (selection)
	set x to item 1 of temp
	set xx to (container of x)
end try
tell application "System Events"
	tell process "Finder"
		try
			tell xx
				set uie to (UI elements)
			end tell
		end try
	end tell
end tell
return uie

I tried this…



tell application "Finder" --to activate
	try
		set temp to (selection)
		set x to item 1 of temp
		set xx to (container of x)
	end try
	tell application "System Events"
		tell process "Finder"
			try
				tell xx
					set uie to properties of xx
					return uie
				end tell
			end try
		end tell
	end tell
end tell

For a selection in the Desktop Window, returned…

{class:folder, name:“Desktop”, index:4, displayed name:“Desktop”, name extension:“”, extension hidden:false, container:folder “brianchristmas” of folder “Users” of startup disk of application “Finder”, disk:startup disk of application “Finder”, position:{80, 217}, desktop position:missing value, bounds:{48, 185, 112, 249}, kind:“Folder”, label index:0, locked:false, description:missing value, comment:“”, size:3.191848088E+9, physical size:3.234144256E+9, creation date:date “Monday, 2 May 2005 10:39:01 AM”, modification date:date “Thursday, 12 October 2006 3:58:52 PM”, icon:missing value, URL:“file://localhost/Users/brianchristmas/Desktop/”, owner:“brianchristmas”, group:“staff”, owner privileges:read write, group privileges:read only, everyones privileges:read only, container window:Finder window id 9 of application “Finder”}

For a selection on the Desktop…

{class:folder, name:“Desktop”, index:4, displayed name:“Desktop”, name extension:“”, extension hidden:false, container:folder “brianchristmas” of folder “Users” of startup disk of application “Finder”, disk:startup disk of application “Finder”, position:{80, 217}, desktop position:missing value, bounds:{48, 185, 112, 249}, kind:“Folder”, label index:0, locked:false, description:missing value, comment:“”, size:3.191848088E+9, physical size:3.234144256E+9, creation date:date “Monday, 2 May 2005 10:39:01 AM”, modification date:date “Thursday, 12 October 2006 3:58:52 PM”, icon:missing value, URL:“file://localhost/Users/brianchristmas/Desktop/”, owner:“brianchristmas”, group:“staff”, owner privileges:read write, group privileges:read only, everyones privileges:read only, container window:Finder window id 9 of application “Finder”}

As you can see, they’re exactly the same. So it looke like when a desktop window is open, the finder refers all desktop properties to it.

Even when I close the window, the finder still refers to it as the enclosing folder.

Bummer! :frowning:

Santa

Went to the store.

Here’s more info on ui scripting:

http://www.apple.com/applescript/uiscripting/01.html

There’s a ui element inspector application that helps get the ui elements. I just use the Script Editor and get a listing of ui elements at eac h level.

Here are more ideas.

You could try getting from the menu bar a checked menu item item of the Window menu. On my system when the desktop is focused, no windows are checked. Try looking at the Window menu in the Finder when you have the desktop focused.

You could make copies. On my system, when the desktop is focused it copies the names of the selected icons there. Otherwise, when the window is focused it copies the selected items there. e.g.


tell application "Finder" to activate
tell application "System Events"
	tell process "Finder"
		keystroke "c" using command down
	end tell
end tell
tell application "Finder"
	activate
	set c to the clipboard
end tell
c

Tough puzzle.

gl,