Applescript dialog with icon, multiple lines and styled text

Discussion in 'Mac Programming' started by macstatic, Apr 24, 2015.

  1. macstatic, Apr 24, 2015
    Last edited: Apr 24, 2015

    macstatic macrumors 6502a

    Joined:
    Oct 21, 2005
    #1
    I would like to make a dialog box which looks something like the attached image, but am having little success...

    I haven't found a way to make text in bold and/or italic, so the closest is having two lines and emphasizing the drive name with CAPS, but the following only gives me a generic Applescript icon:
    Code:
    display alert "Backup done!" as warning message 
    "The drive BACKUP can now be disconnected" 
    buttons {"OK"} default button "OK"
    On the other hand I've been able to add a stop icon (replace "stop" with "1" for a generic Applescript icon or "caution" for a yellow caution icon), but only with one line of text:
    Code:
    display dialog "Backup done!" 
    with icon stop buttons "OK"
    I was hoping the code below would do it, but no matter how I arrange the code I keep getting error messages. It seems to concern the second line of text. Here's the code (this time with a custom app icon):
    Code:
    display dialog "Backup done!" with icon file
    "Applications:ChronoSync.app:Contents:Resources:AppIcon.icns" 
    with message "The drive BACKUP can now be disconnected" 
    buttons {"OK"} default button "OK"
    ...so what are my options for making a suitable dialog with an icon, two lines of text and prefferably the ability to set the drive name in bold or bold italic? I'm on OSX 10.9.5 Mavericks
     

    Attached Files:

  2. Red Menace macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #2
    You can't mix the the arguments for display alert and display dialog - your last example should be something like:

    Code:
    display dialog "Backup done!" & return & "The drive BACKUP can now be disconnected" with icon file "Applications:ChronoSync.app:Contents:Resources:AppIcon.icns" buttons {"OK"} default button "OK"
    The standard AppleScript dialogs are not customizable beyond the parameters they accept, and do not have any settings for font or text formatting. For that you would need to create your own, for example using the AppleScriptObjC bridge to create a Cocoa panel with a text field containing an attributed string.
     
  3. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #3
    I'd echo what Red Menace said. If you want any kind of UI beyond the results of the 'display alert' command then AppleScriptObjC is probably the best way to go. However, you may want to take a look at Smile here:

    http://www.satimage.fr/software/en/index.html

    That said, I'd still be going for AppleScriptObjC if I was you.

    Good luck
     
  4. macstatic, Apr 28, 2015
    Last edited: Apr 28, 2015

    macstatic thread starter macrumors 6502a

    Joined:
    Oct 21, 2005
    #4
    Thanks, but as Applescript is enough of a challenge for me I think I'll stick with its limitations.

    I wrote the following script in order to unmount and alert the user of a finished backup to an external drive in Chronosync:
    Code:
    tell application "Finder"
    	eject disk "Backup HDD"
    end tell
    display dialog "Backup complete!" & return & 
    "Please disconnect BACKUP HDD from your Mac." 
    with icon file "Applications:ChronoSync.app:Contents:Resources:AppIcon.icns" 
    buttons {"OK"} default button "OK" 
    But it doesn't work quite as expected because the alert doesn't always come up (I'm sure it worked when I tested it with Chronosync open though). This is in 10.9.5. Mavericks.
    Should I use the notification center feature instead (even though it doesn't support using a custom icon as far as I know)?:
    Code:
    display notification "...disconnect backup drive" 
    with title "Chronosync" subtitle "Backup done!" 
    sound name "default"
    (with the "eject disk" command in there somewhere of course -the above is just what I've tested for the actual dialog).
     
  5. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #5
    Not sure about your specific issue. Do you get any error messages? What's in the log?

    Personally I'd check the disk is mounted before attempting to unmount it:

    Code:
    tell application "Finder"
    	if disk "Backup HDD" exists then
    		eject disk "Backup HDD"
    		display dialog "Backup complete!" & return & "Please disconnect BACKUP HDD from your Mac."
    	end if
    end tell
     
  6. posguy99 macrumors 6502

    Joined:
    Nov 3, 2004
    #6
    Nah. The disk not being mounted is the desired state, so what are you going to do with the condition you're checking for?

    Just enclose the unmount in a try block and have the on error clause test for the error code where Finder wasn't able to unmount it.
     
  7. Partron22 macrumors 68000

    Partron22

    Joined:
    Apr 13, 2011
    Location:
    Yes
    #7
    Look into AppleScript Objective C. If you already know AppleScript, the learning curve isn't that bad, and you get a lot more customizability and bang for the effort.
     
  8. superscape, Apr 29, 2015
    Last edited: Apr 29, 2015

    superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #8
    Yeah, you could do it with a 'try' block. Either way, if for any reason the disk *isn't* mounted then the original code will error. Personally I prefer to test for an error condition up front rather than let my code error and deal with that error. Just personal preference, I guess.
     
  9. blaster_boy macrumors 6502

    blaster_boy

    Joined:
    Jan 31, 2004
    Location:
    Belgium
    #9

Share This Page