AppleScript: Finder: How can I tell if the Desktop has focus?
-
28-09-2019 - |
Question
window of the desktop
is rather crippled. Can't get itsindex
.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.)
Solution
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.
OTHER TIPS
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
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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow