首页 > 解决方案 > if 和 else 带有附件 applescript

问题描述

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

我在我的 iPhone 中截取屏幕截图并上传到 iCloud,这出现在我的桌面 mac 中,所以,我的脚本从桌面获取这张图片,然后发送它。

但它需要一个“if”或“else”才能知道是否有图像。

如果有图片:发送它 如果没有图片:不要发送它

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

标签: applescript

解决方案


要始终处理您的桌面,最好使用“桌面文件夹路径”。另外,使用“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 存在于您的桌面上。


推荐阅读