You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MacOS Sonoma下AppleScript发送SMS失败,请求修复方案

AppleScript解析Excel发送SMS失败的修复方案

问题描述

编写的AppleScript脚本可正常解析Excel并发送iMessage,但尝试发送SMS时始终触发“无法发送”提示。

原脚本代码:

Set file_Name to "/Target.xlsx"
set messageToSend to "Testing an app I wrote to sent mass text messages. Just ignore."
set greeting to "Good afternoon"

tell application "Microsoft Excel"
activate
open file_Name

    tell active sheet
        tell used range
            set RowCount to count of rows
        end tell
        log "RowCount is " & RowCount
    end tell -- active sheet

end tell -- application

repeat with theIncrementValue from 1 to RowCount
tell application "Microsoft Excel"

        tell active sheet
            set theCell to "A" & theIncrementValue & ":A" & theIncrementValue
            log "theCell is " & theCell
            set phone to string value of range theCell as string
            log "number  is " & phone

            set theCell to "B" & theIncrementValue & ":B" & theIncrementValue
            log "theCell is " & theCell
            set recipient to get value of range theCell as string
            log "Name is " & recipient

            tell application "Messages"
                set phoneNumber to phone
                set persMessageToSend to greeting & " " & recipient & ", " & messageToSend

                try
                    set iMessageService to (1st account whose service type = iMessage)
                    set iMessageBuddy to participant phoneNumber of iMessageService
                    send persMessageToSend to iMessageBuddy
                    log ("sent as iMessage to: " & phoneNumber)
                on error
                    try
                        set iMessageService to (1st account whose service type = SMS)
                        set iMessageBuddy to participant phoneNumber of iMessageService
                        send persMessageToSend to iMessageBuddy
                        log ("sent as SMS to: " & phoneNumber)
                    on error
                        log ("ERROR: COULD NOT SEND TO: " & phoneNumber)
                    end try
                end try
            end tell
        end tell -- active sheet
    end tell -- application

end repeat

错误信息:

错误:每次尝试发送SMS号码时都会触发“无法发送”提示。


问题分析与修复步骤

  1. 号码格式不规范
    SMS对号码格式要求严格,必须是带国家码的标准格式(如中国号码需为+8613xxxxxxxxx),原脚本读取的号码可能包含空格、括号等非数字字符,或缺少国家码。

  2. SMS账号获取逻辑兼容问题
    在Messages的AppleScript中,SMS对应的账号service type并非总是精确匹配SMS,部分系统中标识为cellular,需要兼容两种匹配方式。

  3. 上下文嵌套冲突
    在Excel的tell块内嵌套Messages的tell块可能导致上下文混乱,建议先批量读取所有联系人数据,再单独处理消息发送。


修改后的完整脚本

set file_Name to "/Target.xlsx"
set messageToSend to "Testing an app I wrote to send mass text messages. Just ignore."
set greeting to "Good afternoon"
set countryCode to "+86" -- 根据目标号码地区修改国家码(如美国为+1)

-- 批量读取Excel联系人数据
tell application "Microsoft Excel"
    activate
    open file_Name
    tell active sheet
        tell used range
            set RowCount to count of rows
        end tell
        set contactList to {}
        repeat with rowNum from 1 to RowCount
            -- 读取并清理手机号:去除非数字字符
            set phoneRaw to string value of range ("A" & rowNum) as string
            set cleanedPhone to do shell script "echo " & quoted form of phoneRaw & " | tr -cd '[:digit:]'"
            -- 拼接标准格式号码
            set formattedPhone to countryCode & cleanedPhone
            
            -- 读取联系人姓名
            set recipientName to string value of range ("B" & rowNum) as string
            
            -- 存入列表
            set end of contactList to {phone:formattedPhone, name:recipientName}
        end repeat
        close active workbook saving no -- 关闭Excel释放资源
    end tell
end tell

-- 循环发送消息
tell application "Messages"
    -- 预获取可用账号
    try
        set iMessageAccount to first account whose service type is "iMessage"
    on error
        set iMessageAccount to missing value
    end try
    
    try
        set smsAccount to first account whose service type is in {"SMS", "cellular"}
    on error
        set smsAccount to missing value
    end try
    
    repeat with contact in contactList
        set phoneNumber to phone of contact
        set recipientName to name of contact
        set persMessageToSend to greeting & " " & recipientName & ", " & messageToSend
        
        set sentSuccessfully to false
        -- 优先尝试iMessage
        if iMessageAccount is not missing value then
            try
                set iMessageBuddy to participant phoneNumber of iMessageAccount
                send persMessageToSend to iMessageBuddy
                log ("sent as iMessage to: " & phoneNumber)
                set sentSuccessfully to true
            on error errMsg
                log ("iMessage failed for " & phoneNumber & ": " & errMsg)
            end try
        end if
        
        -- iMessage失败则尝试SMS
        if not sentSuccessfully and smsAccount is not missing value then
            try
                set smsBuddy to participant phoneNumber of smsAccount
                send persMessageToSend to smsBuddy
                log ("sent as SMS to: " & phoneNumber)
            on error errMsg
                log ("ERROR: COULD NOT SEND TO " & phoneNumber & ": " & errMsg)
            end try
        end if
    end repeat
end tell

额外注意事项

  • 权限配置:确保Messages和Excel均被授予“自动化”权限(系统设置 > 隐私与安全性 > 自动化)。
  • 测试验证:先测试单个已知可接收SMS的号码,确认格式和账号配置正确后再批量发送。

内容的提问来源于stack exchange,提问作者jh45

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 18:20:38