如何用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
tesseractfor OCR, which isn't pre-installed on macOS. Install it via Homebrew withbrew 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
imageslist 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 newMessageto send it automatically.
Alternative for Built-In OCR (No Tesseract)
If you prefer using macOS's native OCR via Automator:
- Open Automator > Create a new "Quick Action".
- Add the "Extract Text from Image" action.
- Save it as "ExtractImageText".
- 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
相关产品推荐
相关产品推荐

