How can I test if an image is empty in AppleScript?
I have the following applescript. I have it set to run in ichat when i recieve an IM. It notifies me via growl of new messages:
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
The issue is that if my buddy doesn't have an image associated, I get an error. So I want to add an if statement in notify_growl where if theImage is blank, or null, or whatever, to growl sans the image. The display dialog theImage, when empty shows a dialog that says "msng"
Test against missing value
, AppleScript's analogue of a null/undefined value. In your case, that would look something like the following if you want to just leave out the image:
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
If you instead had a default image, you could write
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
For a default image, you could do something like this:
set defaultImageFile to open for access POSIX file "/some/file/path"
set defaultImage to read defaultImageFile as "JPEG"
close defaultImageFile
There may be an easier way, but this will get you an image. However, it's sufficiently slow that it might not work in your case.
Try this ;)
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
精彩评论