Как я могу проверить, является ли изображение пустым в AppleScript?
У меня есть следующий appleScript. Я установил его в 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
Проблема в том, что если у моего друга нет изображения, я получаю сообщение об ошибке. Поэтому я хочу добавить оператор if в notify_growl, где, если theImage пустое, или null, или что-то еще, рычать без изображения. Диалоговое окно дисплея theImage, когда пусто, показывает диалоговое окно с надписью "msng"
2 ответа
Тест против missing value
, Аналог AppleScript с нулевым / неопределенным значением. В вашем случае это будет выглядеть примерно так, если вы хотите просто пропустить изображение:
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
Там может быть более простой способ, но это даст вам образ. Тем не менее, он достаточно медленный, чтобы не работать в вашем случае.
Попробуй это;)
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