Search code examples
imagerandomapplescriptimessage

Send random image via iMessage in AppleScript


I'm trying to send a random image from the "lv" folder on the desktop via iMessage.

tell application "Finder"
        set theFile to some file of folder "Macintosh HD:Users:user:Desktop:lv:"
    end tell

set randomImage to POSIX file theFile

tell application "Messages"

    set targetBuddy to "[email protected]"

    set targetService to id of 1st service whose service type = iMessage

    set textMessage to randomImage

    set theBuddy to buddy targetBuddy of service id targetService

    send textMessage to theBuddy

end tell

Using

tell application "Preview"
open theFile
end tell

I can tell that the random image works. But it's not passed to iMessage. The error message says that the "expected type can't be converted". Error code -1700

Thanks for any suggestions!


Solution

  • Change the following line:

        set randomImage to POSIX file theFile
    

    to this:

        set randomImage to (theFile as alias)
    

    and the rest should be fine.