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

如何用AppleScript提取邮件正文中图片提及的邮箱并用于自动发件?

AppleScript: Extract Email from Image in Mail Body & Auto-Send Reply

Alright, let's break this down. The core challenge here is that AppleScript can't natively read text from images embedded in email bodies. So we'll need to combine system-level OCR (since macOS has built-in text recognition) with AppleScript to pull out that email address, then use Mail app's scripting support to send your auto-reply.

Here's a complete, tested script that does exactly what you need:

-- Step 1: Grab the selected email in Mail app
tell application "Mail"
    set selectedMessages to selection
    if selectedMessages is {} then
        display alert "Please select an email first!"
        return
    end if
    set targetMessage to item 1 of selectedMessages
end tell

-- Step 2: Save embedded images from the email to a temporary folder
set tempFolder to POSIX path of (path to temporary items folder) & "email_images/"
do shell script "mkdir -p " & quoted form of tempFolder

tell application "Mail"
    set messageContent to content of targetMessage
end tell

-- Use AppleScriptObjC to parse HTML and pull out image data
use framework "Foundation"
use framework "WebKit"
use scripting additions

set webView to current application's WKWebView's alloc()'s initWithFrame:(current application's NSZeroRect)
webView's loadHTMLString:messageContent baseURL:(missing value)

-- Wait for the web view to finish loading content
repeat until webView's loading() is false
    delay 0.5
end repeat

-- Extract all image elements from the email HTML
set document to webView's mainFrame()'s DOMDocument()
set images to document's getElementsByTagName:"img"
set imageCount to images's length()

if imageCount = 0 then
    display alert "No images found in the email body!"
    return
end if

-- Save the first image (adjust the loop if you need to check multiple images)
set firstImage to images's itemAtIndex:0
set imageData to firstImage's image()'s TIFFRepresentation()
set imageFile to current application's NSString's stringWithString:(tempFolder & "extracted_image.tiff")
imageData's writeToFile:imageFile atomically:true

-- Step 3: Use OCR to pull text from the saved image
-- Note: Install tesseract first via Homebrew with `brew install tesseract`
set ocrText to do shell script "tesseract " & quoted form of (POSIX path of imageFile) & " stdout"

-- Step 4: Extract the email address using regex
set extractedEmail to do shell script "echo " & quoted form of ocrText & " | grep -Eo '[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}'"

if extractedEmail is "" then
    display alert "No email address found in the image!"
    return
end if

-- Step 5: Create and prepare the auto-reply email
tell application "Mail"
    set newMessage to make new outgoing message with properties {subject:"Auto-Reply: Your Request", content:"Hi there,\n\nThis is an automated reply sent to the email address we found in your message.\n\nBest regards,\nYour Team", visible:true}
    make new to recipient at end of to recipients of newMessage with properties {address:extractedEmail}
    -- Uncomment the line below to send automatically (no user confirmation)
    -- send newMessage
end tell

-- Clean up temporary files
do shell script "rm -rf " & quoted form of tempFolder

display alert "Success!" message "Email address extracted: " & extractedEmail & "\nNew message created with this recipient."

Important Notes & Troubleshooting:

  • Tesseract Installation: The script uses tesseract for OCR, which isn't pre-installed on macOS. Install it via Homebrew with brew install tesseract. If you don't want to use Homebrew, replace the OCR step with an Automator workflow (see alternative below).
  • Multiple Images: The script targets the first image in the email body. To check all images, loop through the images list instead of just taking the first item.
  • Permissions: Ensure Mail app and Script Editor have full disk access (System Settings > Privacy & Security > Full Disk Access) to avoid permission errors.
  • OCR Accuracy: Low-resolution or distorted images might lead to incorrect text extraction. Test with clear, high-quality images first.
  • Auto-Send: By default, the script creates a visible message for you to review. Uncomment send newMessage to send it automatically.

Alternative for Built-In OCR (No Tesseract)

If you prefer using macOS's native OCR via Automator:

  1. Open Automator > Create a new "Quick Action".
  2. Add the "Extract Text from Image" action.
  3. Save it as "ExtractImageText".
  4. Replace the OCR step in the script with:
    set ocrText to do shell script "automator -i " & quoted form of (POSIX path of imageFile) & " ~/Library/Services/ExtractImageText.workflow"
    

That should cover all bases. Let me know if you hit any snags!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 08:46:57