skiaddict1 skiaddict1 - 4 months ago 11
AppleScript Question

Applescript & display dialog with icon stop & System Events

I have a totally stupid problem that's driving me nuts. I need to access a node in a plist file, and if there's a problem I want a dialog displayed with the 'stop' icon. Here's the code:

tell application "System Events"
tell property list file (plistFile as text)
try
tell contents
set backupDayTemp to value of property list item plistElementBackupDay of property list item chosenBackupDisk
end tell
on error m number n from f to t partial result p
if n = -1728 then
display dialog "Can't find " & plistElementBackupDay & " key for disk " & chosenBackupDisk & " in plist file." buttons {"Damn", "Oh dear"} default button "Damn" with title myName with icon stop
return
else
-- otherwise, pass the error on
error m number n from f to t partial result p
end if
end try
end tell
end tell


It seems the compiler thinks System Events is the one that should understand the token "stop", since it's in bold blue font, not the purple italics that it would normally be in. And when the error handler is run I get told "System Events got an error: property list file [path to file] doesn't understand the "stop" message".

My question is: how on earth do I tell System Events that the token is not for it? I've tried putting a
tell me to
in front of the
display dialog
, but that didn't help. I also tried
using terms from application "Standard Additions"
but when I run it, I just get asked to locate the dictionary for Standard Additions.

Tearing my hair out!!!

Answer

The easiest solution is to put the error handling out of the application tell block

try
    tell application "System Events"
        tell property list file (plistFile as text)
            tell contents
                set backupDayTemp to value of property list item plistElementBackupDay of property list item chosenBackupDisk
            end tell
        end tell
    end tell
on error m number n from f to t partial result p
    if n = -1728 then
        display dialog "Can't find " & plistElementBackupDay & " key for disk " & chosenBackupDisk & " in plist file." buttons {"Damn", "Oh dear"} default button "Damn" with title myName with icon stop
        return
    else
        -- otherwise, pass the error on
        error m number n from f to t partial result p
    end if
end try
Comments