开发者

Why does it take so long to retrieve the frontmost process via AppleScript?

开发者 https://www.devze.com 2023-03-24 14:43 出处:网络
I recently wrote a utility program called WindowTiler which moves around the currently focused window using global shortcuts. I move around the window via AppleScript and use the following script to g

I recently wrote a utility program called WindowTiler which moves around the currently focused window using global shortcuts. I move around the window via AppleScript and use the following script to get the bounds of the focused window:

tell application "System Events"
  set appName to the first process whose frontmost is true
  set appWindow to the value of attribute "AXFocusedWindow" of appName
  set {w, h} to the size of appWindow
  set {x, y} to the position of appWindow
  set appBounds to {x, y, x + w, y + h}
end tell
{bounds:appBounds}

Over time I realized that my App reacts slowly if not used in a while. After measuring the time performance intensively I found out that the second line of the shown AppleScript is the cause for the slow response. Sometimes the scrip开发者_开发知识库t needed a whole second to execute (on an SSD, as far as I know it's even worse on HDDs).

I don't know why AppleScript needs so long to simply look up the frontmost process - should be a sole request to the Process Manager. Maybe you know why it is so slow and / or can tell me a way to make the script faster.

PS: I configured Xcode to precompile my AppleScripts, when I create my App ("Archive"). The compiled scripts are read-only.


I haven't seen this kind of lag in receiving Carbon-based event hotkeys. Have you added logging to the code to see when your hotkey callback is called? Is that actually taking several seconds, or are you perhaps taking several seconds to process it?

Take a look at DDHotKey for an example of how to do this well (or you may just want to use Dave's code to replace yours).

EDIT

If you're having trouble after the program's been running awhile, you may also want to run it through Instruments. Make sure you're not leaking large amounts of memory or threads. That could give the symptoms you're describing.

EDIT2

Why not load the script and keep it in an ivar or static rather than loading it on demand? Even compiled, you still have to read it off of disk, parse it and build up the data structures. (Also, this question has drifted off of its subject. You should close it out and start a new question about Applescript performance. Otherwise it screws up people who are searching for answers later.)


If you force the script to receive the process (and all its properties), then of course it can be slow sometimes. Especially when debugging. Better and faster is to get just the name of the process. The more properties a process has, the more gain will be gained by obtaining only the name. Also, the focused window of the frontmost process is always window 1.

tell application "System Events"
    set appName to name of first process whose frontmost is true
    tell process appName to tell window 1 to set {{w, h}, {x, y}} to {size, position}
end tell
{bounds:{x, y, x + w, y + h}}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号