Selecting an item on the desktop when there's a window open

I am trying to find a solution for a “background” process that briefly takes focus and then wants to give focus back to the current app.

I have a problem with the Finder:

If there’s at least one Finder window open but the focus was on the Desktop, I cannot figure out how to re-focus the desktop.

This code can tell me if the Desktop has the focus.

tell application "Finder"
	get insertion location
end tell

I could then also get the current selection in order to restore that later.

However, as soon as my process switches back to the Finder (using SetFrontProcessWithOptions(kSetFrontProcessFrontWindowOnly)), the Finder gives focus to its front window. And there seems to be no way around this.

So, what I am trying with AppleScript is to see if I can take away the focus from the window and give it back to the Desktop again. However, code like this doesn’t have any effect, i.e. the front window remains focused:

tell application "Finder"
	set insertion location to folder "Desktop" of folder "myname" of folder "Users" of startup disk
end tell

Any idea how to solve this?

(I’ve also asked this on SO if someone wants to catch the Bounty there: http://stackoverflow.com/questions/10950802)

Hello.

Try setting the target of the front window instead, if you wants the folder of the front window to change permanently. You may also want to consider the folder the view the finder window is in.

Target is the way to go when you are setting a finder window to point at some folder. I think you would have to save the insertion location of the window you are leaving as well, and set it back upon return. ( I think you may be able to move to the previous view directly by using something like command-( but I haven’t tried it.)

If you just want a quick peak at selected items on the desktop, then this works. (You must hide the desktop again with an identical do shell script.) The insert location returns nothing if nothing is selected.


do shell script "/Applications/Utilities/Expose.app/Contents/MacOS/Expose 1"

tell application "Finder"
” This errs if the insertion location is empty
	set a to its insertion location as text
	display dialog a
end tell

Nevermind, I’ve found a solution, it seems:

tell application "Finder"
	activate
	select the desktop's window
end tell

Hello.

Just remember that you won’t have an insertion point, if no items are selected.

What makes you believe that? In my tests, this is not the case.

However, the long-standing bug in the Finder (since 10.7) is causing trouble: It often reports the wrong selection (and insertion location) for newly opened windows. Damn Apple for not fixing this well-known bug!

I’m sorry, on Snow Leopard


tell application "Finder"
	activate
	select the desktop's window
end tell

Doesn’t seem to do much at all. It really doesn’t change anything.

It works on my Snow Leopard machine. The Finder’s frontmost open window is no longer the active one, while the desktop itself is.

Err…

It does work for me as well, when I execute it from the menu and can watch what finder is doing. :|.
And I also get the insertion location. From both ways to get at the Desktop window, the one I posted above as well.

Can you rephrase that, please? I have no idea what you’re saying. If you’re German, please write it in German and I’ll translate.

Are you saying that you have a reliable way to tell if the desktop or a window has the focus? Because I can’t accomplish this. When I get the insertion location, it generally works, but it fails on 10.7 and later for just-opened windows - both “insertion location” and “selection” then return incorrect results, and that’s a well-known bug (e.g. several Radars exist, as I was told when I filed my own).

Here’s the code I use to tell if the desktop has the focus:

tell application "Finder"
	if insertion location is equal to folder "Desktop" of home then
		if target of first window is equal to insertion location then
			get "desktop window"
		else
			get "desktop"
		end if
	else
		get "other"
	end if
end tell

It fails in 10.7 and later if you do this: Close all Finder windows, run the script → it correctly returns “desktop”. Go back to Finder and press Cmd-Shift-H to open the Home folder. Now that window has the focus. Run the script again → it still says “desktop”, that’s the bug. If you switch once more to Finder and run the script, it correctly says “other”.

The other (smaller) issue with this script is that it can’t tell the difference between the desktop having the focus or a window of the Desktop having the focus. In that ambiguous case it returns “desktop window”.

We were just correcting the assertion in post #6 that your work-round doesn’t work in 10.6.8. It does. But we’re unable to say anything about 10.7. Sorry for any confusion.

Yes. I ran your script from within Script Debugger, and I couldn’t see anything happen. When running it from the Script Menu, the situation changed, and your script worked as Nigel Garvey described.

What you describe, about errantly reporting, reminds me of a workaround Christopher Stone has made for a similar issue, when Finder fails to update the data. He then “tickles” finder, by activating another window before reactivating the former. Then the datastructures (object - model) is up to date.

Maybe you should give it a try.

PS. And what I said in sloppy language maybe, was that I didn’t get a runtime error when trying to get the insertion location when I either got it with your snippet, or tried to get it by activating the Desktop window with exposé.app. DS.

:slight_smile:

Odd, I used SD as well. Version 4.5.

What kind of window should I activate? I guess you meant to bring another process to front, then back to the Finder?

Hello. running in ScriptEditor under 10.8.2.

When I run the script posted in message #9, the log report is :


tell application "Finder"
	get insertion location
		--> folder "Desktop" of folder "<userAccount>" of folder "Users" of startup disk
	get folder "Desktop" of home
		--> folder "Desktop" of folder "<userAccount>" of folder "Users" of startup disk
	folder "Desktop" of folder "<userAccount>" of folder "Users" of startup disk = folder "Desktop" of folder "<userAccount>" of folder "Users" of startup disk
		--> error number -1708
	get folder "Desktop" of folder "<userAccount>" of folder "Users" of startup disk
		--> folder "Desktop" of folder "<userAccount>" of folder "Users" of startup disk
	get folder "Desktop" of folder "<userAccount>" of folder "Users" of startup disk
		--> folder "Desktop" of folder "<userAccount>" of folder "Users" of startup disk
	get target of window 1
		--> error number -1728 from window 1

So I edited it a bit to get rid of the error number -1708.


tell application "Finder"
	if insertion location as text is equal to folder "Desktop" of home as text then # EDITED
		if target of first window is equal to insertion location then
			get "desktop window"
		else
			get "desktop"
		end if
	else
		get "other"
	end if
end tell

With these changes, the log report becomes:


tell application "Finder"
	get insertion location
		--> folder "Desktop" of folder "<userAccount>" of folder "Users" of startup disk
	get folder "Desktop" of folder "<userAccount>" of folder "Users" of startup disk
		--> "Macintosh HD:Users:<userAccount>:Desktop:"
	get folder "Desktop" of home
		--> "Macintosh HD:Users:<userAccount>:Desktop:"
	get target of window 1
		--> error number -1728 from window 1

So, I made new changes to take care of the fatal error number -1728 :


tell application "Finder"
	if insertion location as text is equal to folder "Desktop" of home as text then
		try
			first window
			get "desktop window"
		on error
			get "desktop"
		end try
	else
		get "other"
	end if
end tell

This time, the log report is :


tell application "Finder"
	get insertion location
		--> folder "Desktop" of folder "<userAccount>" of folder "Users" of startup disk
	get folder "Desktop" of folder "<userAccount>" of folder "Users" of startup disk
		--> "Macintosh HD:Users:<userAccount>:Desktop:"
	get folder "Desktop" of home
		--> "Macintosh HD:Users:<userAccount>:Desktop:"
	get window 1
		--> error number -1728 from window 1
end tell
(*Résultat :
"desktop"
*)

Yvan KOENIG (VALLAURIS, France) dimanche 24 février 2013 16:42:31

Okay, here’s an improved version that deals with the case of no open windows. It works on 10.8.2 in Script Editor for, me as well:

tell application "Finder"
	if insertion location is equal to folder "Desktop" of home then
		try
			set topwin to first window
		on error
			set topwin to null
		end try
		if topwin is not null and target of topwin is equal to insertion location then
			get "desktop window"
		else
			get "desktop"
		end if
	else
		get "other"
	end if
end tell

Still, this all doesn’t deal with the case of getting the wrong information when selecting the desktop and then opening a new window with Cmd-Shift-H.

Hello.

I posted a way to do it in post #2 but it is not particularily discréte. :slight_smile:

You repeat the call to exposé app to restablish the focus on the app that was current when you called it.

Abusing Expose for that is kind of wrong, IMO. Here’s a “cleaner” way:

tell application "Finder"
	activate
	tell me to activate
	activate
	...
end tell

Oh silly me! That, of course, won’t work, because it will take away the focus from the Desktop before I can ask about it.