Wednesday, December 12, 2018
  • Index
  •  » Code Exchange
  •  » My Perfect Dialog handler (with custom icons + sound + error handler)

#1 2018-12-07 01:03:55 pm

Riku
Member
Registered: 2016-05-23
Posts: 43

My Perfect Dialog handler (with custom icons + sound + error handler)

I found myself doing this so much that I eventually learned to use handlers and created some for these classic procedures – with all the extras to make the messages more intuitive and unique! I thought I'd share.

After the script, there's tips on how to obtain the custom icons and sound effect that you want to use for this. Although, if you don't want them, you can just remove the icon and sound effect parts from the handlers, it's up to you!

Keyboard input toggle script was a tip by StefanK that I found here the other day, turned them into handlers as is. If you press enter on dialog window, it chooses the default answer, but with the keyboard input toggled on, you can use tab to navigate to any of the two or three answer buttons, and press space bar to select it.

For some reason I didn't get them to work included inside the actual dialog handler, so I made them into separately called handlers. They are just a single line of script, but I made them into handlers because the handler call is much easier to remember than the actual script lines!

Applescript:


-----------------------------------------------------
# Instructions: How to set up the Perfect Dialog handler with custom goodies:
-----------------------------------------------------
# Download an icon file that you want to use as an error message icon. It should be in .icns format.
# Place the icon file into a folder that you want to use as an icon folder, and name the icon file Error.icns
# Similarly, download an icon file that you want to use as a question dialog icon, also of .icns format.
# Name it anything you like, and place it into the icons folder that you created.
# Obtain a short sound effect file that you want to use as error message sound effect. Good formats include .mp3 and .aiff
# Place the sound effect into a folder that you want to use as a sound effects folder.
# Now from the handlers below, replace the sample Finder paths with the paths on your system, in the path formats that the handlers show.
# Test and make sure that this script runs with the Finder paths that you've provided: You see both icons, and hear the sound.
# Save these handlers as a .scpt file in /Library/Application Support/Script Editor/Templates (and make an appropriate category subfolder there if you want to). That way, you can always grab yourself a neat fresh copy of the perfect dialog suite by navigating to Script Editor's menus File > New From Template.

-----------------------------------------------------
# Handlers to toggle keyboard input for dialogs
# To call, sandwich the actual dialog between inputON() and inputOFF()
-----------------------------------------------------
on inputON()
   do shell script "defaults write .GlobalPreferences AppleKeyboardUIMode -int 2" -- All controls
end inputON
on inputOFF()
   do shell script "defaults write .GlobalPreferences AppleKeyboardUIMode -int 0" -- Text boxes and lists only
end inputOFF

-----------------------------------------------------
# Handler for dialogs
# Includes Message, Answers, Icon, Default button, Variable that the answer becomes, Title respectively
-----------------------------------------------------
on dialog(themessage, theanswers, theicon, thedefault, thevariable, thetitle)
   display dialog "
"
& themessage & "
"
buttons theanswers with icon file ("Disk:Enter:Path:To:Icons:Folder:Here:" & theicon & ".icns") giving up after 30 default button thedefault with title thetitle
   set thevariable to the button returned of the result
end dialog

-----------------------------------------------------
# Error handler
# Includes Message, Icon, Title respectively
-----------------------------------------------------
on theErrorHandler(errorMessage, iconfile, title)
   set soundPath to "/Users/Enter/Path/To/Error/Sound/File/Here.aiff"
   do shell script "afplay " & quoted form of POSIX path of soundPath & "> /dev/null 2>&1 &" ---Play sound effect while continuing the script
   display dialog "
"
& errorMessage with icon file ("Disk:Enter:Path:To:Icons:Folder:Here:" & iconfile & ".icns") with title title buttons {"OK"} default button "OK"
   -128 ---Error 128, meaning "user canceled", will quit the script and prevent it from running further
end theErrorHandler
----------------------------------------





### Ask about something
inputON()
set variablefromanswer to dialog("This is a message!", {"Answer1", "Answer2", "Answer3"}, "Error", "Answer2", "variablefromanswer", "This is a title")
inputOFF()

### What to do with something based on user's answer
if variablefromanswer is "Answer1" then
   say "You chose the first option" ---Demo consequence 1. Use this to test that your dialog works how you mean it to work. When it does, you can delete this line or #disable it temporarily.
else if variablefromanswer is "Answer2" then
   say "You chose the second option" ---Demo consequence 2
else if variablefromanswer is "Answer3" then
   say "You chose the third option" ---Demo consequence 3
else ---Error, the script quits. Most likely you chose an answer that doesn't have a consequence written for it.
   theErrorHandler("Couldn't determine which button was pressed!
   "
, "Error", "Stage 1") ---Especially if your script has a lot of if-blocks, it's good practice to give each error a unique stage name in the title, for example an alphabet or a number. This way, even if you run your script from a standalone app and it encounters an error, the error message title conveniently tells you what stage the error happened at so you know where to go and troubleshoot.
end if

You can download icons from places like IconArchive, Deviantart, another Deviantart category, InterfaceLIFT and so on. If you're trying to find some really specific icon and can't find it, you can also make your own, save it as .png file (which, unlike .jpg, supports transparency) and convert it into .icns using software such as image2icon. A .png file to convert into an icon must be square, and its resolution must either be 128x128, 256x256, 512x512 or 1024x1024 pixels.

I made my own sound effects that I use so unfortunately I have no sound effect sources to link, but I'm sure those will be easy to find! You can use your Mac's native sound effect files too.
By request, I can also share my own error sound effect to individuals who ask; The only reason I don't share it right here automatically is that while I'm perfectly fine sharing my creation with individuals, I wouldn't really care for external parties nicking it, because you never know if stuff gets redistributed as freebies behind your back and then used in commercial applications without the creator's knowledge. But, no problem sharing with members of this community. cool My own error sound is made with a soft synth pad sound; It plays three quick notes that create a feeling that something isn't right, but because it's a soft synth sound, it won't be startling or uncomfortable.

Alright, if this speeds up anyone's dialog workflow on the long run, I'm glad!

Last edited by Riku (2018-12-07 01:05:19 pm)


Filed under: Dialog, Error, Handlers, custom

Offline

 
  • Index
  •  » Code Exchange
  •  » My Perfect Dialog handler (with custom icons + sound + error handler)

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)