• 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: Mark Hunte <email@hidden>
  • Date: Fri, 21 Nov 2014 13:32:23 +0000

Thanks Shane,

I adjusted my copy to use a .sdef file and terminology.
Also it will just return the fields without having to make another call asking for them.



The code:




use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"
property theView : missing value
global listOfLabels
-- 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; for givingUpAfter, 0 means never; accessoryView of missing value means none
on display alert message mainText with description theExplanaton as style styleNum and buttons buttonsList suppression showSuppression giving up after giveUp accessoryView theView
set styleNum to styleNum as string
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
set validQStyleType to {"warning", "0", "informational", "1", "critical", "2"}
if styleNum is in validQStyleType then



-- apply the indicated transformation to the Cocoa string
if the styleNum is "warning" or styleNum is "0" then
set the styleNum to current application's NSNumber's numberWithInt:0
else if the styleNum is "informational" or styleNum is "1" then
set the styleNum to current application's NSNumber's numberWithInt:1
else if the styleNum is "critical" or styleNum is "2" then
set the styleNum to current application's NSNumber's numberWithInt:2
end if


else
set errStr to styleNum & " is not valid style " as text
error errStr


end if



tell theAlert
its setAlertStyle:styleNum
its setMessageText:mainText
its setInformativeText:theExplanaton
repeat with anEntry in buttonsList
(its addButtonWithTitle:anEntry)
end repeat
its setShowsSuppressionButton:showSuppression
if theView is not missing value then its setAccessoryView:theView
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
set theAnswers to {}
repeat with i from 1 to count of listOfLabels
set end of theAnswers to ((theView's viewWithTag:i)'s stringValue() as text)
end repeat
return {buttonName, suppressedState, theAnswers}
end display alert message

on View with labels listOfLabels and placeholders listOfPlaceholders
set labelCount to count of listOfLabels
set totalDepth to labelCount * 55 -- each label plus entry field needs 55pts 
set viewList to {} -- list of controls to add to accessory view
repeat with i from 1 to labelCount
set theLabel to (current application's NSTextField's alloc()'s initWithFrame:(current application's NSMakeRect(0, totalDepth - 30 - (i - 1) * 55, 400, 17)))
tell theLabel
(its setStringValue:(item i of listOfLabels))
(its setEditable:false)
(its setBordered:false)
(its setDrawsBackground:false)
end tell
copy theLabel to end of viewList
-- now text entry field
set theInput to (current application's NSTextField's alloc()'s initWithFrame:(current application's NSMakeRect(0, totalDepth - 55 - (i - 1) * 55, 400, 22)))
tell theInput
(its setEditable:true)
(its setBordered:true)
(its setPlaceholderString:(item i of listOfPlaceholders))
(its setTag:i)
end tell
copy theInput to end of viewList
end repeat
-- create a view and add items
set theView to current application's NSView's alloc()'s initWithFrame:(current application's NSMakeRect(0, 0, 400, totalDepth))
theView's setSubviews:viewList


return theView
end View with labels

The sdef code:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dictionary SYSTEM "file://localhost/System/Library/DTDs/sdef.dtd">
<dictionary>

 

    

    <suite name="Display Alert Windows" code="SSMH" description="Commands to edit text">

        

        

        

        <command name="View with labels" code="DSPYVIEW" description="returns a custom view.">
            <direct-parameter type="list" description="A list of custom text fileds,Each field will also use the item as its title"/>
            <parameter name="and placeholders" code="AnTX" type="list" description="A list of placeholders to be displayed in the blank text fields"/>

         

        </command>
        <command name="display alert message" code="DSPYALRT" description="Dialogues Text value">
            <direct-parameter type="text" description="dialogues Text value "/>

            

            <parameter name="with description" code="WhDn" type="text" description="The Dialogues Description Text"/>
             <parameter name="as style" code="Styl" type="style Num" description="Alert style"/>
              <parameter name="and buttons" code="Bttn" type="list" description="The Buttons"/>
               <parameter name="suppression" code="sUpp" type="boolean" description="suppression  allows the user to suppress the display of a particular alert in subsequent occurrences of the event that triggers it."/>
                        <parameter name="giving up after" code="gvUP" type="number" description="0 means never"/>
                         <parameter name="accessoryView" code="ViEW"  type="view"  description="The current view"/>
        </command>

        

     

        <enumeration name="style Num" code="CSEC">
            <enumerator name="warning" code="STwg" description=""/>
            <enumerator name="informational" code="STil" description=""/>
            <enumerator name="critical" code="STcr" description=""/>
        </enumeration>
    </suite>
</dictionary>



The Example code:

use script "Display Alert Windows"

set listOfLabels to {"First name", "Initial", "First name", "Initial", "First name", "Initial", "Second name"}
set listOfPlaceholders to {"John", "Q", "John", "Q", "John", "Q", "Citizen"}
-- build the view


set theView to View with labels listOfLabels and placeholders listOfPlaceholders
--set theView2 to display view with labels listOfLabels and placeholders listOfPlaceholders
-- pass the view to the alert handler
set {buttonName, suppressedState, fields} to (display alert message "Decision time" with description "Enter your name" as style critical and buttons {"Cancel", "OK"} giving up after 10 accessoryView theView without suppression)

--set {buttonName2, suppressedState2, fields2} to (display alert message "Decision time" with description "Enter your name" as style critical and buttons {"Cancel", "OK"} giving up after 120.0 accessoryView theView2 without suppression)
log fields
--log fields2




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

  • Prev by Date: Re: is Shane's advice being collated?
  • Next by Date: Respond to DVD/CD insertion
  • Previous by thread: Re: Scripting alerts
  • Next by thread: Re: Scripting alerts
  • Index(es):
    • Date
    • Thread