0

当其他(不相关的)应用程序更改状态(即关闭、最小化)时,applescript(或者可能是其他一些脚本)是否有可能接收通知?目标是维护最近关闭或最小化的 5 个应用程序的列表

4

2 回答 2

0

你真的不能用applescript做到这一点。如果您知道objective-c,尽管它相当微不足道。NSWorkspace 发布关于很多东西的通知,其中包括你想知道的关于应用程序隐藏、激活等的所有信息。请查看此处的类文档,了解你可以获得的所有通知。

基本上你所要做的就是编写一个小的后台应用程序来接收这些通知,然后你可以对它们做任何你想做的事情。像下面这样的简单调用将为您注册所有通知,然后在“nsworkspaceNotification:”方法中使用一个简单的“if 语句”来确定您正在接收哪些通知,并允许您对它们采取行动.

[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(nsworkspaceNotification:) name:nil object:nil];
于 2013-01-02T01:16:40.047 回答
0

设置触发器的一种方法可能是http://www.dssw.co.uk/blog/2011/01/28/how-to-run-an-applescript-when-switching-to-battery-power/

以下脚本将为您提供当前最小化的所有应用程序(由于某种原因,除了 Finder,确定的错误),您可以将其设置为间隔运行,甚至将其设置为在后台连续运行(性能将受到影响我想),但希望它能让你更接近解决方案(当然要意识到这并不能真正解决任何窗口问题的状态更改触发器)

property top5 : {"None", "None", "None", "None", "None"}
property oldest : 0


tell application "System Events" to set _list to (name of application processes)

repeat with i from 1 to number of _list
    set current_name to item i of _list

    if top5 does not contain current_name then
        tell application "System Events" to tell application process current_name to set win_num to (number of every window)
        if not win_num is 0 then
            try
                tell application current_name
                    set _windows to every window
                    repeat with i from 1 to number of _windows
                        set this_window to item i of _windows

                        try
                            log "mini"
                            if miniaturized of this_window is true then
                                my addNewToTop5(current_name)
                                exit repeat
                            end if
                        end try

                        try
                            log "collapsed"
                            if collapsed of this_window is true then
                                my addNewToTop5(current_name)
                                exit repeat
                            end if
                        end try

                    end repeat
                end tell
            end try
        end if

    end if

end repeat

log top5

on oldestOfTop5()
if oldest is 5 then
    set oldest to 1
else
    set oldest to oldest + 1
end if
return oldest
end oldestOfTop5

on addNewToTop5(_item)
set item oldestOfTop5() of top5 to _item
end addNewToTop5
于 2013-01-01T16:04:26.213 回答