如果和其他附件 applescript

If and else with attachment applescript

我每天都会发送一封附有图片的电子邮件

我在我的 iPhone 中截图并上传到 iCloud,它出现在我的桌面上 mac,因此,我的脚本从桌面上获取这张图片,然后将其作为附件发送。

但它需要一个“if”或“else”来判断是否有图像。

如果有 image:send 如果没有图片:请不要发送

set recipientName to "name"
set recipientAddress to "Mail@mail.com"
set theAttachment to POSIX file "user/desktop/image.png"
set theSubject to "theSubject"
set theContent to "message"

--Mail Tell Block
tell application "Mail"

    --Create the message
    set theMessage to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}

    --Set a recipient
    tell theMessage
        make new to recipient with properties {name:recipientName, address:recipientAddress}
        --Add attachment
        make new attachment with properties {file name:theAttachment} at before the last paragraph


        ##Pause
        delay 3

        ##Send the Message
        --send

    end tell
end tell

要始终解决您的桌面问题,最好使用 "path to desktop folder"。 此外,使用函数 "exists" 检查图像文件是否存在于您的桌面上,如果不存在,您只需使用 "return":

终止脚本
set theAttachment to ((path to desktop folder) as string) & "image.png"
tell application "Finder"
    if not (theAttachment exists) then return -- image does not exist, then termination
end tell

如果您的脚本在这 4 行之后继续,则表示文件 image.png 存在于您的桌面上。