AppleScript:Finder:如何判断桌面是否具有焦点?

发布于 2024-09-15 01:53:41 字数 202 浏览 23 评论 0原文

  • 桌面窗口相当残破。无法获取其索引
  • Finder 窗口 不包括桌面窗口,因此我无法检查它是否是第一个窗口。
  • 无论桌面是否具有焦点,第一个 Finder 窗口的索引 均为 1。 (只要存在其他Finder窗口,否则会失败。)
  • 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.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

天煞孤星 2024-09-22 01:53:41

看起来插入位置属性很接近,也许足够接近。

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"

但是,如果焦点位于打开到桌面文件夹的 Finder 窗口上,则存在歧义。这会产生与焦点位于桌面背景相同的结果。但也许这对于你想做的事情并不重要。

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.

情魔剑神 2024-09-22 01:53:41

看来你可以检查一下选择...

set desktopIsFrontmost to false
tell application "Finder"
    if selection is {} then set desktopIsFrontmost to true
end tell
return desktopIsFrontmost

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
聊慰 2024-09-22 01:53:41

使用内德的答案,这就是我想到的(在 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文