views:

52

answers:

3
  • window of the desktop is rather crippled. Can't get its index.
  • Finder windows does not include the desktop window, so I can't check that the it's the first there.
  • index of the first Finder window is 1 regardless of the desktop having focus. (as long as other Finder windows exist, otherwise it'll fail.)
+3  A: 

Looks like the insertion location property comes close, maybe close enough.

insertion location (specifier, r/o) : the container in which a new folder would appear if “New Folder” was selected

tell application "Finder"
    get insertion location
end tell

Result:
folder "Desktop" of folder "nad" of folder "Users" of startup disk of application "Finder"

There's an ambiguity, though, if the focus is on a Finder window opened to the Desktop folder; that gives the same result as if the focus is on the Desktop background. But maybe that doesn't matter for what you want to do.

Ned Deily
This might just do it. The funny part is I've used it before.
kch
A: 

It looks like you can just check the selection...

set desktopIsFrontmost to false
tell application "Finder"
    if selection is {} then set desktopIsFrontmost to true
end tell
return desktopIsFrontmost
regulus6633
While that depends on what the OP means by "focus", normally `focus` and `selection` are different concepts. For instance, if you click on an item (a folder or file, say) on the Desktop, that means the Desktop background has focus (i.e. no other Finder windows are selected) but the `selection` property reflects the item(s) currently selected, just as it would if you selected them in any Finder window that currently had focus. Each Finder window including the Desktop background pseudo-window has its own current selection value.
Ned Deily
I agree Ned. My method does miss if something is selected on the Desktop.
regulus6633
A: 

Using Ned's answer, here's what I came up with (in rb-appscript):

#!/usr/bin/env ruby
# encoding: UTF-8
require 'pathname'
require 'appscript'
include Appscript

path_to_desktop             = Pathname.new "#{ENV['HOME']}/Desktop"
path_of_insertion_location  = Pathname.new app("Finder").insertion_location.get(:result_type => :file_ref).get(:result_type => :alias).path
path_of_first_finder_window = Pathname.new app("Finder").Finder_windows.first.target.get(:result_type => :alias).path rescue nil
is_desktop_the_active_view  = path_to_desktop == path_of_insertion_location && path_of_first_finder_window != path_to_desktop
kch