'Why does it sometimes take so long (~10s) to activate open apps via AppleScript?
The simple AppleScript
activate application "Safari"
sometimes takes up to 10s to complete when Safari is already open (but not activated). I use AppleScripts like this to switch between programs using Spark, but similar delays happen using the AppleScript editor or on the command line using
osascript -e 'activate application "Safari"
so the delay isn't in Spark itself. It's not just switching to Safari that has this problem, but it's not every program either. Chrome consistently activates quickly, for instance. Other apps that are particularly slow are Evernote and Terminal.
The slowness happens after Safari hasn't been activated for a while, say, longer than a minute. If I use similar scripts to switch between programs in rapid succession, only the first switch is slow; subsequent switches are quick. So it seems related to OS processes, not just AppleScript.
On the other hand, if I switch programs using Cmd-Tab it loads quickly after I select an application, so it seems like the slowness is tied to the AppleScript activation.
Slight variations of the AppleScript make no difference; for instance,
tell application "Safari" to activate
Also using the full path of the application made no difference.
This is on 10.14.1 on a mid-2014 Macbook Pro with 16GB of RAM. It seems to have gotten worse when I upgraded from Sierra to Mojave, but now that I think about there was some slowness on Sierra too. I've been using the same AppleScripts via Spark for years, and when I started it was always quite quick.
Solution 1:[1]
activate
has never really worked, it's just a glitchy command. This works instantly, at least on my device:
tell application "System Events"
tell process "Safari"
set frontmost to true
end tell
end tell
I hope this can help you! ?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Nicholas Stands with Ukraine |