简体   繁体   English

查找当前活动的 Finder 窗口/文件夹

[英]Find the currently active Finder window/folder

Is there any way to determine the currently active window, or a folder, in the Finder?有没有办法在 Finder 中确定当前活动的 window 或文件夹? I need this to determine, in some sense, an appropriate "default" location in which to do some particular things in my app.在某种意义上,我需要它来确定一个适当的“默认”位置,在我的应用程序中执行一些特定的事情。

Actually, does this question even make sense?其实,这个问题还有意义吗? Does this concept of a "currently active Finder window/folder" even exist in the first place?这种“当前活动的 Finder 窗口/文件夹”的概念是否一开始就存在? If it does not, I kindly ask how to get the currently selected Finder item.如果没有,请问如何获取当前选择的 Finder 项目。

Yes, the concept of the currently active Finder window does exist, as well as the currently selected item.是的,当前活跃的Finder window 的概念确实存在,当前选中的项目也是如此。

For example, the following AppleScript gets the selection which is the current selection in the frontmost window.例如,下面的 AppleScript 获得了最前面的selection中的当前选择。 Since this returns a list of files or folders even if there is a single item, the next line gets the first item out of that list (after making sure that the count of the list is greater than 0).由于即使只有一个项目,这也会返回文件或文件夹的列表,因此下一行会从该列表中获取第一个项目(在确保列表的计数大于 0 之后)。 You can then ask the Finder for the container window of the selected item, which will return a Finder window object.然后,您可以向 Finder 询问所选项目的container window ,这将返回Finder window object。

tell application "Finder"
    set selectedItems to selection

    if ((count of selectedItems) > 0) then
        set selectedItem to (item 1 of selectedItems) as alias
        container window of selectedItem
    end if

end tell

I'm pretty sure the code sidyll posted will work okay in 10.5 and earlier, but it errors out in 10.6 due to the inevitable changes and quirkiness that AppleScript seems to have from one version of OS X to the next.我很确定发布的代码 sidyll 在 10.5 及更早版本中可以正常工作,但由于 AppleScript 从一个版本的 OS X 到下一个版本的不可避免的变化和古怪,它在 10.6 中会出错。

[EDIT] Actually, I just figured out what's going on. [编辑] 实际上,我只是弄清楚发生了什么。 I usually have the Finder's Inspector window open all the time (the dynamic Get Info window you get through Command-Option-i), the upper right panel in the image below:我通常让 Finder 的 Inspector window 一直打开(通过 Command-Option-i 获得的动态 Get Info window),下图中的右上方面板:

在此处输入图像描述

That image shows 3 different classes of windows:该图像显示了 3 个不同类别的 windows:

1) The upper left, a Get Info window, is an information window , which inherits from the generic window class. 1)左上角,一个Get Info window,是一个information window ,它继承自通用的window ZA2F2ED4FDC40ABBDDZC21。

2) The upper right, an Inspector window, is a plain window . 2) 右上角,一个 Inspector window,是一个普通的window

3) The lower image shows a Finder window , which inherits from the generic window class. 3) 下图显示了一个Finder window ,它继承自通用window class。

If I run the following script with the setup of windows shown above:如果我使用上面显示的 windows 设置运行以下脚本:

tell app "Finder"
    every window
end tell

it returns the following result:它返回以下结果:

{ window "mdouma46 Info" of application "Finder", information window "mdouma46 Info" of application "Finder", Finder window id 1141 of application "Finder"} { 应用程序“Finder”的window “mdouma46 Info”,应用程序“Finder”的信息 window “mdouma46 Info”,应用程序 Finder”的Finder window id 1141}

So, what was happening in my case is that, since the Inspector window is a floating utility panel, if it's currently being shown, asking the Finder for window 1 will always return the Inspector panel, since it's always floating in front of the other windows.所以,在我的情况下发生的事情是,由于 Inspector window 是一个浮动实用程序面板,如果它当前正在显示,向 Finder 询问window 1将始终返回 Inspector 面板,因为它总是浮动在另一个 window 1 的前面,因为它总是浮动在其他 window 1 前面.

So the error I was getting when running the code was:所以我在运行代码时遇到的错误是:

error "Can't make «class fvtg» of window 1 of application \"Finder\" into type alias."错误“无法将应用程序 \"Finder\" 的 window 1 的 «class fvtg» 转换为类型别名。” number -1700 from «class fvtg» of window 1 to alias编号 -1700 从 window 1 的 «class fvtg» 到别名

(In other words, the Inspector window, a plain window , doesn't have the FileViewer target ( fvtg ) property; only Finder window s do). (换句话说,Inspector window,一个普通的window没有 FileViewer 目标( fvtg )属性;只有Finder window有)。

So, your code will work fine as long as the user doesn't have the Inspector window, the Preferences window, or a Get Info window that is frontmost.因此,只要用户没有位于最前面的 Inspector window、Preferences window 或 Get Info window,您的代码就可以正常工作。 By changing window to Finder window , though, you can make sure that you only look at the file viewer windows that have the target property.但是,通过将window更改为Finder window ,您可以确保只查看具有target属性的文件查看器 windows 。

So, like this:所以,像这样:

NSDictionary *errorMessage = nil;

NSAppleScript *script = [[[NSAppleScript alloc] initWithSource:
    @"tell application \"Finder\"\n"
        " if ((count of Finder windows) > 0) then\n"
           "  return (POSIX path of (target of Finder window 1 as alias))\n"
        "end if\n"
     "end tell"] autorelease];

if (script == nil) {
    NSLog(@"failed to create script!");
    return nil;
}
NSAppleEventDescriptor *result = [script executeAndReturnError:&errorMessage];
if (result) {
    // POSIX path returns trailing /'s, so standardize the path
    NSString *path = [[result stringValue] stringByStandardizingPath];
    return path;
}

I needed to do this in a project in the past and recurred to AppleScript:我过去需要在一个项目中这样做,并重复到 AppleScript:

// Get path
NSAppleScript *script = [[NSAppleScript alloc] initWithSource:
                        @"tell application \"Finder\"\n"
                         "  return POSIX path of (target of window 1 as alias)\n"
                         "end tell"];
NSDictionary *errors = nil;
NSAppleEventDescriptor *descriptor = [script executeAndReturnError:&errors];

if ((errors != nil) || (descriptor == nil)) {
    // There is no opened window or an error occured
} else {
    // what was retrieved by the script
    path = [descriptor stringValue];
}
[script release];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM