9

我正在开发一个小型 Mac 应用程序,该应用程序通常会在后台不可见地运行。但是,当用户在桌面或 Finder 中的其他位置重命名文件时,应用程序的主要功能就会发挥作用。发生这种情况时,我想显示一个类似于用户通过 Finder 更改文件扩展名时出现的对话框。因为这需要我的应用程序获得最前面的焦点(而不是 Finder),所以当用户在我的对话框中点击“确定”时,我想将 Finder 作为最前面的应用程序返回。

我目前正在使用 Apple 的Process Manager功能SetFrontProcessWithOptions(),但在以下情况下遇到了麻烦:

  • 用户在其工作区的某处打开一个 Finder 窗口
  • 然后用户点击他们的桌面,使窗口失去焦点。
  • 用户重命名桌面上的文件
  • 我的应用程序使自己专注于使用SetFrontProcessWithOptions()
  • 用户OK在对话框中点击,我的应用程序使用 Finder 聚焦SetFrontProcessWithOptions()
  • 当 Finder 重新聚焦时,它聚焦用户之前打开的窗口,尽管事实上当 Finder 之前处于最前面时它没有聚焦。

如果在重命名桌面上的文件之前在另一个空间中打开了 Finder 窗口,这会变得非常烦人:在这种情况下,在对话框中点击“确定”会导致 Finder 自动切换空间并返回窗口。

这只是因为SetFrontProcessWithOptions()函数的性质,它只能聚焦给定应用程序的窗口。由于桌面显然不算作window,因此该函数反而会找到另一个要聚焦的窗口,尽管事实上用户之前没有将该窗口聚焦。

如果有人对如何做这样一种基于对话的事情有更好的想法,那就太好了,甚至可能根本不需要聚焦和不聚焦 Finder。

编辑:我发现了一种在大多数情况下修复此行为的有点丑陋的方法,但它涉及脚本桥,并且如果其中的项目被重命名,它不会重新聚焦桌面。这是我这样做的代码:

FinderApplication * app = [SBApplication applicationWithBundleIdentifier:@"com.apple.finder"];
if (!app || ![app isRunning]) {
    SetFrontProcessWithOptions(&processSerial, kSetFrontProcessFrontWindowOnly);
    return;
}
SBElementArray * selArray = app.selection.get;
if ([selArray count] == 0) {
    SetFrontProcessWithOptions(&processSerial, kSetFrontProcessFrontWindowOnly);
    return;
} else {
    FinderWindow * window = [[[selArray objectAtIndex:0] container].get containerWindow].get;
    if ([window isKindOfClass:NSClassFromString(@"FinderFinderWindow")]) {
        SetFrontProcessWithOptions(&processSerial, kSetFrontProcessFrontWindowOnly);
    } else {
        // TODO: this is where I'd insert code to select the item
        // on the desktop...
    }
}
4

3 回答 3

2

您是否考虑过简单地调用-[NSApp hide:nil]?也就是说,让系统担心如何重新激活之前处于活动状态的应用程序,并确保您的应用程序不再处于活动状态。

顺便说一句,您观察到的行为,即 Finder 在接收到活动状态时激活窗口而不是桌面的行为与您在 Command-Tab 离开然后返回时发生的情况相同。或者 Command-Tab 离开,然后隐藏您切换到的应用程序。因此,它可能被认为是正确的行为。另一方面,在我的测试中,当 Finder 专注于桌面但在不同空间有一个窗口时隐藏前台应用程序不会切换到其他空间。它做你想做的事:以桌面为中心激活 Finder。

最后,-[NSRunningApplication activateWithOptions:]SetFrontProcessWithOptions().


如果您真正想要的只是一种运行相当于

tell application "Finder"
    activate
    select the desktop's window
end tell

在 Objective-C 中,我看到了两个选项。首先,使用 Scripting Bridge API:

[self.finder activate];
[(FinderWindow*)self.finder.desktop.containerWindow select];

(我假设您已经掌握了在 Finder 中使用 Scripting Bridge 的基础知识。如果没有,Apple 有一个ScriptingBridgeFinder 示例。出于某种原因,它位于遗留文档部分。)

其次,您可以使用NSAppleScript

NSAppleScript* s = [[NSAppleScript alloc] initWithSource:@"tell application \"Finder\"\nactivate\nselect the desktop's window\nend tell"];
NSDictionary* err = nil;
if (![s executeAndReturnError:&err])
    /* handle error */;

然而,在我对 Snow Leopard 的测试中,两种方法都无法切换到以桌面为中心的 Finder。但是,您的 AppleScript 代码也没有从 AppleScript 编辑器运行。

于 2013-02-24T19:03:54.527 回答
1

如果桌面上没有选择,恐怕亚历克斯的解决方案不起作用。相反,这个 AppleScript 应该更可靠地判断窗口或桌面是否具有焦点:

tell application "Finder"
    get insertion location
end tell

如果它有焦点,它将返回桌面文件夹的路径。

虽然这仍然存在两个问题:

  1. 如果用户打开了桌面窗口,则上述脚本返回相同的信息。即无法区分桌面和具有焦点的桌面窗口。如果也有解决方案,那就太好了。
  2. 自 10.7 以来 Finder 中存在一个错误(仍然存在于 10.8.2 中,并且为 Apple 所知(请参阅http://openradar.appspot.com/9406282)导致 Finder 报告有关最近打开的窗口的错误信息,甚至上面的脚本不可靠。

但是,要重新选择桌面,我现在自己找到了解决方案:

tell application "Finder"
    activate
    select the desktop's window
end tell

MacScripter.net 上还有一个关于此的线程,我在其中更多地解释了这些选项:http ://macscripter.net/viewtopic.php?pid=160529

于 2013-02-24T09:47:58.640 回答
0

为什么不调用实际的 Apple Script:

NSDictionary *errorInfoDictionary = nil;
NSAppleScript *script = [[NSAppleScript alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"FocusOnFinder" ofType:@"scpt"]] error:&errorInfoDictionary];

// OR

// NSAppleScript *script = [[NSAppleScript alloc] initWithSource:@"tell application \"Finder\"\n activate\n select the desktop's window\n end tell"];

if (!errorInfoDictionary)
{
    if (![script executeAndReturnError:&errorInfoDictionary])
    {
        NSLog(@"%@", [errorInfoDictionary description]);
    }
}
else
    NSLog(@"%@", [errorInfoDictionary description]);

FocusOnFinder.scpt 的内容

tell application "Finder"
   activate
   select the desktop's window
end tell

如果 Finder 未运行或已最小化,则没有效果。

于 2013-02-25T01:11:55.420 回答