如何使用 applescript 按应用程序顺序枚举正在运行/打开的应用程序?

发布于 2024-12-20 17:31:25 字数 148 浏览 2 评论 0原文

我的最终目标是确定最近使用的网络浏览器。即,用户可能同时运行 Safari 和 Firefox,但他们最近使用的是哪一个?

因此,我试图枚举打开的应用程序,按其窗口/可见性排序。

Cocoa / Applescript 是否提供了检索此信息的方法?

My end goal is to determine the most-recently-used web browser. I.e., a user may have both Safari and Firefox running, but which were they using most recently?

And so, I'm trying to enumerate the open apps, ordered by their window / visibility.

Does Cocoa / Applescript offer a way to retrieve this information?

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

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

发布评论

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

评论(1

诗酒趁年少 2024-12-27 17:31:25

您可以使用 NSWorkspace 的 runningApplications 方法和 NSRunningApplication 的 < code>launchDate 属性让您接近您所需要的。

如果此代码位在控制台中看起来像这样,请检查输出:

NSArray * arrayOfRunningApps = [[NSWorkspace sharedWorkspace] runningApplications];
if(arrayOfRunningApps)
{
    for(NSRunningApplication * aUserApp in arrayOfRunningApps) 
    {
        NSLog( @"%@ launch time is %@", [aUserApp localizedName], [aUserApp.launchDate description] );
    }
}

至于按某些标准进行排序,这实际上取决于您的实现。

一些注意事项:这些 api 是从 Snow Leopard (10.6) 开始引入的,
它们仅适用于用户可以看到的应用程序(在扩展坞中),而不是 UNIX 进程或管理/根守护程序或其他任何东西。

我还不确定 Applescript,但我希望您能够使用这些信息到达您需要去的地方。

You can use NSWorkspace's runningApplications method and NSRunningApplication's launchDate property to get you close to what you need.

Check out what the output if this code bit looks like in your console:

NSArray * arrayOfRunningApps = [[NSWorkspace sharedWorkspace] runningApplications];
if(arrayOfRunningApps)
{
    for(NSRunningApplication * aUserApp in arrayOfRunningApps) 
    {
        NSLog( @"%@ launch time is %@", [aUserApp localizedName], [aUserApp.launchDate description] );
    }
}

As for sorting on some criteria, that's really up to your implementation to do.

A couple caveats: these api's were introduced as of Snow Leopard (10.6) and
they only work on apps that the user can see (in the dock), not UNIX processes or admin/root daemons or whatever.

And I'm not certain about Applescript yet, but I'm hoping you'll be able to use this information to get to where you need to go.

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