kch
kch

Reputation: 79660

AppleScript: Finder: How can I tell if the Desktop has focus?

Upvotes: 1

Views: 1515

Answers (3)

kch
kch

Reputation: 79660

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

Upvotes: 0

regulus6633
regulus6633

Reputation: 19040

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

Upvotes: 0

Ned Deily
Ned Deily

Reputation: 85105

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.

Upvotes: 4

Related Questions