如何在AppleScript中测试图像是否为空?

时间:2011-10-08 01:27:28

标签: applescript ichat

我有以下AppleScript。当我收到IM时,我将它设置为在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,或者其他什么,以咆哮没有图像。显示对话框theImage,当为空时显示一个对话框,显示“msng”

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:2)

针对missing value测试,AppleScript是否为null / undefined值。在您的情况下,如果您想要省略图像,那将看起来像下面这样:

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

可能有一种更简单的方法,但为您提供图片。但是,它足够慢,可能在您的情况下不起作用。