On Jan 10, 2016, at 2:45 AM, Shane Stanley <email@hidden> wrote:
On 10 Jan 2016, at 7:04 PM, Christopher Stone <email@hidden> wrote:
I use FastScripts for this sort of thing, because it does everything the System Script Menu does and has global and app-specific keyboard shortcuts.
And it works with ASObjC-based UI, unlike the built-in one.
Shane is referring to the fact that if an AppleScriptObjC (ASOC) script displays UI using the AppKit framework, in some contexts AppKit has not been fully initialized and certain things may not work, like acquiring keyboard focus.
This is something that may not have been specifically documented for ASOC scripts, but, like scripting additions, ASOC scripts must call AEInteractWithUser() before displaying UI with lower-level APIs (as opposed to displaying UI via scripting additions).
When a script is being executed by the osascript program, for example, AppKit isn't fully initialized until AEInteractWithUser() is called. The Standard Addition commands that display UI all call this function, so, for example, using “display dialog” will work just fine.
Here's an example:
use framework "AppKit" -- NSAlert is in AppKit use framework "Carbon" -- AEInteractWithUser() is in Carbon
if current application's AEInteractWithUser(-1, missing value, missing value) = 0 then -- -1 is kAEDefaultTimeout activate current application's NSAlert's new's runModal() end if
Note that this isn't merely about using AppKit in certain contexts: every time you invoke a UI scripting-addition command it will call AEInteractWithUser(), and so should ASOC scripts each time they display an alert or dialog, to notify the user that you want their attention. (Even if you recently interacted with the user, because you can't predict when they may switch to another application while your script is running.)
-- Chris Page The other, other AppleScript Chris
|