• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Displaying UI in AppleScriptObjC scripts (was Re: How to Run a "sudo" Terminal Command)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Displaying UI in AppleScriptObjC scripts (was Re: How to Run a "sudo" Terminal Command)


  • Subject: Displaying UI in AppleScriptObjC scripts (was Re: How to Run a "sudo" Terminal Command)
  • From: Chris Page <email@hidden>
  • Date: Tue, 26 Jan 2016 18:51:36 -0800


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

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: Displaying UI in AppleScriptObjC scripts (was Re: How to Run a "sudo" Terminal Command)
      • From: Shane Stanley <email@hidden>
References: 
 >Re: How to Run a "sudo" Terminal Command (From: Christopher Nebel <email@hidden>)
 >Re: How to Run a "sudo" Terminal Command (From: Christopher Stone <email@hidden>)
 >Re: How to Run a "sudo" Terminal Command (From: Shane Stanley <email@hidden>)

  • Prev by Date: Re: Security Update [Was: Re: Script Library Search Order]
  • Next by Date: Re: Displaying UI in AppleScriptObjC scripts (was Re: How to Run a "sudo" Terminal Command)
  • Previous by thread: Re: How to Run a "sudo" Terminal Command
  • Next by thread: Re: Displaying UI in AppleScriptObjC scripts (was Re: How to Run a "sudo" Terminal Command)
  • Index(es):
    • Date
    • Thread