1

我有以下小程序。当我收到即时消息时,我已将它设置为在 ichat 中运行。它通过新消息的咆哮通知我:

on notify_growl(theName, theTitle, theDescription, theImage)
    display dialog theImage
    tell application "Growl"
        notify with name theName title theTitle description theDescription application name "iChat" image theImage
    end tell
end notify_growl

using terms from application "iChat"
    -- register the app with growl
    tell application "Growl"
        set the allNotificationsList to {"Message Received"}
        set the enabledNotificationsList to {"Message Received"}
        register as application "iChat" all notifications allNotificationsList default notifications enabledNotificationsList icon of application "iChat"
    end tell

    -- handle the iChat events
    on message received theMessage from theBuddy for theChat
        if ((application "iChat" is not frontmost) or (status of application "iChat" is equal to away)) then
            notify_growl("Message Received", name of theBuddy, theMessage, image of theBuddy)
        end if
    end message received

    on received text invitation theMessage from theBuddy for theChat
        accept theChat
        if ((application "iChat" is not frontmost) or (status of application "iChat" is equal to away)) then
            notify_growl("Message Received", name of theBuddy, theMessage, image of theBuddy)
        end if
    end received text invitation
end using terms from

问题是,如果我的好友没有关联图像,我会收到错误消息。所以我想在 notify_growl 中添加一个 if 语句,如果 theImage 为空白、null 或其他任何内容,则可以咆哮 sans 图像。显示对话框 theImage,当为空时显示一个显示“msng”的对话框

4

2 回答 2

2

尝试这个 ;)

on notify_growl(theName, theTitle, theDescription, theImage)
    tell application "Growl"
        try
            notify with name theName title theTitle description theDescription application name "iChat" image theImage
        on error
            notify with name theName title theTitle description theDescription application name "iChat"
        end try
    end tell
end notify_growl
于 2011-10-09T00:33:23.340 回答
2

测试missing valueAppleScript 的空值/未定义值的类似物。在您的情况下,如果您只想省略图像,则看起来如下所示:

on notify_growl(theName, theTitle, theDescription, theImage)
    tell application "Growl"
        if theImage is missing value then
            notify with name theName title theTitle description theDescription ¬
                application name "iChat"
        else
            notify with name theName title theTitle description theDescription ¬
                application name "iChat" image theImage
        end if
    end tell
end notify_growl

如果你有一个默认图像,你可以写

on notify_growl(theName, theTitle, theDescription, theImage)
    tell application "Growl"
        if theImage is missing value then set theImage to defaultImage
        notify with name theName title theTitle description theDescription ¬
            application name "iChat" image theImage
    end tell
end notify_growl

对于默认图像,您可以执行以下操作:

set defaultImageFile to open for access POSIX file "/some/file/path"
set defaultImage to read defaultImageFile as "JPEG"
close defaultImageFile

可能有更简单的方法,但这会给你一个图像。但是,它的速度足够慢,可能不适用于您的情况。

于 2011-10-09T06:13:52.567 回答