• 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: Wed, 19 Nov 2014 14:27:50 +1100

Now for the suppression button. This is simply a matter of making sure it shows -- by calling setShowsSuppressionButton: -- and once the alert is dismissed, checking the state of the suppression button.

This code includes a repeat loop, to show how the suppression button works. Each time through the loop, a dialog appears and a voice speaks; once you click the suppress button, the dialog no longer appears.

The same warning and advice about running in the foreground holds:

use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit" -- required for NSAlert

-- check we are running in foreground
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

-- for styleNum, 0 = warning, 1 = informational, 2 = critical
on displayAlert:mainText message:theExplanaton asStyle:styleNum buttons:buttonsList suppression:showSuppression
set buttonsList to reverse of buttonsList -- because they get added in reverse order cf AS
-- 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
end tell
-- show alert
set returnCode to theAlert's runModal()
-- 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
set buttonName to item buttonNumber of buttonsList
return {buttonName, suppressedState}
end displayAlert:message:asStyle:buttons:suppression:

set showDialog to true -- start off showing dialog

repeat with i from 1 to 10
if showDialog then
set {buttonName, suppressedState} to (my displayAlert:"Decision time" message:("Do you want to munge item " & i & "?") asStyle:2 buttons:{"Cancel", "OK"} suppression:true)
-- if box was checked, we want showSuppression off next time
if suppressedState then set showDialog to false
end if
-- do what needs to be done, depending on buttonName, here
say ("Loop number " & i)
end repeat

-- 
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>)

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