如何让Apple Script在发送的iMessage中包含当前时间?
AppleScript to Send iMessage with Current Date/Time
Got it, here's a solid, tested solution to send an iMessage that automatically includes the current date and time to a specific contact. I’ve used this on macOS Ventura and Monterey, so it should work across most recent versions of macOS.
Full Working Script
First, here's a ready-to-use script—just tweak the contact details and message format to your liking:
-- Replace this with your contact's phone number (with country code) or email set targetRecipient to "+15551234567" -- Example: US number, or "john.doe@example.com" -- Fetch and format current date/time (customize the string inside 'date' to change format) set nowDateTime to do shell script "date '+%Y-%m-%d %H:%M:%S'" -- Build your message content set messageText to "Hey! The current date and time is: " & nowDateTime -- Send the iMessage via the Messages app tell application "Messages" send messageText to buddy targetRecipient end tell
Breakdown of Key Parts
Let's walk through what each section does so you can adjust it as needed:
- Target Recipient: Make sure this is a phone number (with country code, e.g.,
+1for US) or email that's already in your Contacts app. The Messages app needs to recognize the recipient to send the message successfully. - Date/Time Formatting: The
dateshell command lets you customize how the time is displayed. Here are some common format codes you can mix and match:%Y: 4-digit year (2024)%m: 2-digit month (03 for March)%d: 2-digit day (15)%H: 24-hour hour (14 for 2 PM)%I: 12-hour hour (02 for 2 PM)%M: Minutes (45)%S: Seconds (30)%A: Full weekday name (Wednesday)%B: Full month name (March)%p: AM/PM indicator
For a more friendly, conversational format, try this:
This would output something likeset nowDateTime to do shell script "date '+%A, %B %d, %Y at %I:%M %p'"Wednesday, March 15, 2024 at 02:45 PM. - Sending the Message: The
tell application "Messages"block instructs macOS to use the native Messages app to deliver your composed text to the specified recipient.
Important Notes
- Permissions: macOS will prompt you to allow the script to control the Messages app the first time you run it. If you need to adjust these later, go to System Settings > Privacy & Security > Automation.
- Contact Recognition: If the script throws an error, double-check that the recipient's number/email matches exactly what’s in your Contacts app. Typos or missing country codes are common culprits.
- Alternative: Select a Contact Interactively
If you don’t want to hardcode the recipient, here’s a version that lets you pick a contact from your Contacts app on the fly:tell application "Contacts" set selectedContact to choose contact with prompt "Pick a contact to send the time update to:" -- Use either email or phone number (uncomment the one you need) set targetRecipient to value of first email of selectedContact -- set targetRecipient to value of first phone number of selectedContact end tell set nowDateTime to do shell script "date '+%A, %B %d, %Y at %I:%M %p'" set messageText to "Hi! Just wanted to share the current time: " & nowDateTime tell application "Messages" send messageText to buddy targetRecipient end tell
Let me know if you hit any snags—permission issues are the most common, but adjusting the date format or recipient should be straightforward once you get the hang of it.
内容的提问来源于stack exchange,提问作者Tim Apple
相关产品推荐
相关产品推荐

