• 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
Re: Scripting alerts
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Scripting alerts


  • Subject: Re: Scripting alerts
  • From: Shane Stanley <email@hidden>
  • Date: Mon, 24 Nov 2014 17:17:17 +1100

The code to show the alert is based on the earlier examples, but it's been consolidated and extended a bit. First, the separate handler to build the accessory view has been eliminated. You now pass the list of controls plus dimensions for the accessory view, and it is built in the handler. And the handler will also retrieve the appropriate final values of the controls, and return it as a list in the final result. This will simplify the code you write to use it.

So here is the new handler:

-- for styleNum, 0 = warning, 1 = informational, 2 = critical; for givingUpAfter, 0 means never
on displayAlert:mainText message:theExplanaton asStyle:styleNum buttons:buttonsList suppression:showSuppression givingUpAfter:giveUp AVWidth:theWidth AVHeight:theHeight AVControls:controlsList
-- make the accessory view
set theAccessoryView to current application's NSView's alloc()'s initWithFrame:(current application's NSMakeRect(0, 0, theWidth, theHeight))
theAccessoryView's setSubviews:controlsList
-- reverse buttons because they get added in reverse order cf AS
set buttonsList to reverse of buttonsList
-- create an alert
set theAlert to current application's NSAlert's alloc()'s init()
-- set up alert
tell theAlert
its setAlertStyle:styleNum
its setMessageText:mainText
its setInformativeText:theExplanaton
repeat with anEntry in buttonsList
(its addButtonWithTitle:anEntry)
end repeat
its setShowsSuppressionButton:showSuppression
its setAccessoryView:theAccessoryView
end tell
-- if giveUp value > 0, tell the app to abort any modal event loop after that time, and thus close the panel
if giveUp > 0 then current application's NSApp's performSelector:"abortModal" withObject:(missing value) afterDelay:giveUp inModes:{current application's NSModalPanelRunLoopMode}
-- show alert in modal loop
set returnCode to theAlert's runModal()
-- if a giveUp time was specified and the alert didn't timeout, cancel the pending abort request
if giveUp > 0 and returnCode is not current application's NSModalResponseAbort then current application's NSObject's cancelPreviousPerformRequestsWithTarget:(current application's NSApp) selector:"abortModal" object:(missing value)
-- get values after alert is closed
set suppressedState to theAlert's suppressionButton()'s state() as boolean
set buttonNumber to returnCode mod 1000 + 1 -- where 1 = right-most button
if buttonNumber = 0 then
set buttonName to "Gave Up"
else
set buttonName to item buttonNumber of buttonsList
end if
-- get values from controls
set controlResults to {}
repeat with aControl in controlsList
if (aControl's isKindOfClass:(current application's NSTextField)) as boolean then
set end of controlResults to aControl's stringValue() as text
else if (aControl's isKindOfClass:(current application's NSPopUpButton)) as boolean then
set end of controlResults to aControl's title() as text
else if (aControl's isKindOfClass:(current application's NSButton)) as boolean then
set end of controlResults to aControl's state() as boolean
else if (aControl's isKindOfClass:(current application's NSPathControl)) as boolean then
set end of controlResults to aControl's |URL|()'s |path|() as text
else if (aControl's isKindOfClass:(current application's NSMatrix)) as boolean then
set end of controlResults to aControl's selectedCell()'s title() as text
else -- NSBox
set end of controlResults to missing value
end if
end repeat
return {buttonName, suppressedState, controlResults}
end displayAlert:message:asStyle:buttons:suppression:givingUpAfter:AVWidth:AVHeight:AVControls:

You can see that the result for each control depends on what type of control it is.

You need to put this, plus all the handlers from the past few posts, in a script library, and precede it with:

use AppleScript version "2.3.1"
use scripting additions
use framework "Foundation"
use framework "AppKit"

For safety's sake, also add this handler:

-- check we are running in foreground
on checkForMainThread()
if not (current application's NSThread's isMainThread()) as boolean then
display alert "This script must be run from the main thread." buttons {"Cancel"} as critical
error number -128
end if
end checkForMainThread

Save it as a .scptd file to ~/Library/Script Libraries/. To run under Mavericks, you need to set it to AppleScript/Objective-C in Script Editor; this step is not required in Yosemite.

If you are running Yosemite, you can test it by putting it all directly in your script, which can be helpful when you start.

In the next post I'll get down to how to actually use it.

-- 
Shane Stanley <email@hidden>
<www.macosxautomation.com/applescript/apps/>

 _______________________________________________
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: Scripting alerts
      • From: Shane Stanley <email@hidden>
References: 
 >Scripting alerts (From: Shane Stanley <email@hidden>)
 >Re: Scripting alerts (From: Shane Stanley <email@hidden>)
 >Re: Scripting alerts (From: Shane Stanley <email@hidden>)
 >Re: Scripting alerts (From: Shane Stanley <email@hidden>)
 >Re: Scripting alerts (From: Shane Stanley <email@hidden>)
 >Re: Scripting alerts (From: Shane Stanley <email@hidden>)
 >Re: Scripting alerts (From: Shane Stanley <email@hidden>)
 >Re: Scripting alerts (From: Shane Stanley <email@hidden>)
 >Re: Scripting alerts (From: Shane Stanley <email@hidden>)
 >Re: Scripting alerts (From: Shane Stanley <email@hidden>)

  • Prev by Date: Re: Scripting alerts
  • Next by Date: Re: Scripting alerts
  • Previous by thread: Re: Scripting alerts
  • Next by thread: Re: Scripting alerts
  • Index(es):
    • Date
    • Thread