求助:AppleScript创建Mail邮件时正文首行空白的修复方案
修复Mail应用AppleScript新建邮件首行空行问题
Mail应用在通过AppleScript创建新邮件时,会自动在content属性的内容前添加一个换行符,这是导致正文首行空白的核心原因。以下是两种可靠的修复方案:
方案一:创建邮件后单独设置正文(推荐)
将正文内容的赋值从创建邮件的属性参数中移到后续的邮件对象操作里,直接覆盖默认生成的空行:
property theToAddress : "john@example.com" property theSubject : "Example subject" set theTo to theToAddress set theSubjectLine to theSubject set theBody to choose file with prompt "Please select a file for the body of the email:" set theMessageBody to read theBody as «class utf8» tell application "Mail" -- 先创建邮件,暂不设置content属性 set theNewMessage to make new outgoing message with properties {subject:theSubjectLine, visible:true} tell theNewMessage make new to recipient with properties {address:theTo} -- 单独设置正文内容,覆盖默认空行 set content to theMessageBody end tell end tell
方案二:预处理正文内容(针对硬编码场景)
如果是使用硬编码的正文内容,也可以先去除开头可能存在的换行符(不过方案一更彻底):
-- 示例:处理硬编码内容 set hardcodedBody to "Hello world!" -- 去掉开头的换行符 if hardcodedBody starts with linefeed then set hardcodedBody to text 2 thru -1 of hardcodedBody end if
核心逻辑是避开Mail在创建邮件时自动插入的首行换行,通过后续赋值覆盖默认行为,确保正文从第一行开始显示。
内容的提问来源于stack exchange,提问作者Paul Vincent
相关产品推荐
相关产品推荐

