AppleScript创建Mail应用默认签名时语法错误求助
AppleScript设置Mail默认签名的语法错误解决
问题描述
尝试使用以下AppleScript代码在Mail应用中创建并设置默认签名时,触发语法错误,提示“类名不能跟在应用程序常量或参数之后”,且default signature被高亮:
tell application "Mail" set newSignatureContent to "Content of the new signature" set newSignatureName to "NewSignature" -- Create the signature set newSignature to make new signature with properties {content:newSignatureContent, name:newSignatureName} -- Set the signature as default for all existing accounts set allAccounts to every account repeat with currentAccount in allAccounts set signatures of currentAccount to {newSignature} set **default signature** of currentAccount to newSignature end repeat end tell
错误原因
- 非法格式字符:代码中错误地用Markdown加粗标记
**包裹了default signature,AppleScript无法识别这种非语法格式的字符,直接导致语法错误。 - 带空格属性的引用问题:即使去掉
**,default signature是包含空格的属性名,AppleScript中设置这类属性时,必须通过the关键字修饰,或者将属性名用引号括起来,否则会被解析为无效的语法组合。
修正后的代码
tell application "Mail" set newSignatureContent to "Content of the new signature" set newSignatureName to "NewSignature" -- 创建新签名 set newSignature to make new signature with properties {content:newSignatureContent, name:newSignatureName} -- 为所有现有账户设置默认签名 set allAccounts to every account repeat with currentAccount in allAccounts -- 注意:该行会替换账户下所有现有签名,如需保留原有签名,可替换为: -- set signatures of currentAccount to signatures of currentAccount & newSignature set signatures of currentAccount to {newSignature} -- 方式1:使用the关键字引用带空格的属性 set the default signature of currentAccount to newSignature -- 方式2:用引号包裹属性名(两种方式二选一即可) -- set "default signature" of currentAccount to newSignature end repeat end tell
额外说明
- 原代码中
set signatures of currentAccount to {newSignature}会直接覆盖该账户下的所有现有签名,如果需要保留原有签名,建议替换为set signatures of currentAccount to signatures of currentAccount & newSignature,将新签名追加到签名列表中。
内容的提问来源于stack exchange,提问作者Smaug2504
相关产品推荐
相关产品推荐

