确定正在使用的其他应用程序

时间:2014-08-06 22:44:41

标签: objective-c macos safari

我想知道是否有可能在我的程序中确定用户当前正在使用的其他应用程序。例如,用户可能正在使用Safari和Mail。

从那时起,我很想知道我是否可以确定用户是否正在使用打开的应用程序。如果计算机正在使用且用户未使用它,我将确定当前没有任何打开的应用程序正在使用中。如果用户正在主动搜索网络,我将确定当前正在使用Safari(或任何其他互联网浏览器)。

从那时起,我想知道是否有可能看到用户在应用程序中正在做什么......这个主要是针对互联网浏览器。我想知道用户当前在哪个网站。如果普通应用程序无法做到这一点,是否可以在Web浏览器扩展中进行?

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

您可以从“ps”unix命令获得一些提示,但不是您问题的完整答案:

ps aux

大致看一下“STAT”栏: R表示正在运行 +表示互动

准确地说:

 state     The state is given by a sequence of characters, for example, ``RWNA''.  The first character indicates the run state of the process:

           I       Marks a process that is idle (sleeping for longer than about 20 seconds).
           R       Marks a runnable process.
           S       Marks a process that is sleeping for less than about 20 seconds.
           T       Marks a stopped process.
           U       Marks a process in uninterruptible wait.
           Z       Marks a dead process (a ``zombie'').

           Additional characters after these, if any, indicate additional state information:

           +       The process is in the foreground process group of its control terminal.
           <       The process has raised CPU scheduling priority.
           >       The process has specified a soft limit on memory requirements and is currently exceeding that limit; such a process is (necessarily) not swapped.
           A       the process has asked for random page replacement (VA_ANOM, from vadvise(2), for example, lisp(1) in a garbage collect).
           E       The process is trying to exit.
           L       The process has pages locked in core (for example, for raw I/O).
           N       The process has reduced CPU scheduling priority (see setpriority(2)).
           S       The process has asked for FIFO page replacement (VA_SEQL, from vadvise(2), for example, a large image processing program using virtual memory to sequentially
                   address voluminous data).
           s       The process is a session leader.
           V       The process is suspended during a vfork(2).
           W       The process is swapped out.
           X       The process is being traced or debugged.

从那里,您可以了解用户正在使用的内容,但这并不意味着他正在与之交互。

误报:将进行批量繁重处理的后台服务器(它们将处于R状态)。


另一种看待它的方法是找到当前Foregroud中的哪个应用程序。

尝试:

osascript -e 'tell application "System Events"' -e 'set frontApp to name of first application process whose frontmost is true' -e 'end tell'

来自shell。

你可以让它运行:

while sleep 5; do osascript -e 'tell application "System Events"' -e 'set frontApp to name of first application process whose frontmost is true' -e 'end tell'; done

即便如此,尝试一下,然后按F11和F12:你会在Expose / Dashboard上看到,否则,这不能正确刷新......

请参阅: http://alvinalexander.com/mac-os-x/applescript-unix-mac-osx-foreground-application-result