How do I display image data with display dialog?
I am trying to take the image data from a buddy in Messages and display dialog using that image.
tell application "Messages"
set image_data to image of buddy 1
end tell
set image_string to image_data as string
display dialog "Hello" with icon image_string
Unable to make data into string error returns as a consequence
image_data is of class data and cannot be coerced to string. icon doesn’t recognize string representation of an external icon.
The solution is to write the data into a temporary image file.
I added also error handling in case the buddy doesn’t have an image.
tell application "Messages"
set image_data to image of buddy 1
end tell
set isValid to image_data is not missing value
set buddyImage to (path to temporary items folder as text) & "buddyImage.tiff"
if isValid and (writeFile from image_data into buddyImage) then
display dialog "Hello" with icon (alias buddyImage)
else
display dialog "Hello" with icon caution -- this line is executed when there is no image or writing the image fails
end if
on writeFile from theData into theFile
try
set fileDescriptor to open for access file theFile with write permission
write theData to fileDescriptor
close access fileDescriptor
return true
on error
try
close access file theFile
end try
return false
end try
end writeFile