How to search for an Email using apple Script

So I have therapy and I do it over zoom but its a pain to search for the email every time is there anyway to use AppleScript to find and open the email when I run the script?


tell application "Mail" to activate
delay 3
tell application "System Events"
	key code 3 using command down
end tell

I tried to use something like this but with a command and I don't know how to press 3 keys since I need to press command option command f and if I use key code then it doesn't hold them down if I use 3 different ones. Is there a way to do this. Someone please help me.


Posted on May 30, 2022 4:09 PM

Reply
Question marked as Top-ranking reply

Posted on May 31, 2022 3:24 PM

Enjoy your AppleScript journey :)


This is a perfect example of the kind of progress most people go through when learning AppleScript.

In the beginning, you start of by trying to replicate the actions you would take normally (press these keys, click these buttons...) into where AppleScript's true strength lies, namely directly manipulating objects (in this case email messages). The joy? pain? comes in making that mental jump.


I have two comments to your script. In your final script you select the message via MessageID, which is perfectly valid and pseudo-unique. Just be aware you can select via any property of the message:


	-- your code
    open (first message of inbox whose message id = MessageID)
	-- check for a sender in a specific mailbox
    set theMsg to first message of mailbox "INBOX" of account "iCloud" whose sender is "hello@somewhere.com".  
	-- check for a subject
    set theMsg to first message of inbox whose subject = "Super Important Message"
    -- check for a flagged message
	set theMsg to first message of inbox whose flag index = 5


As for your earlier approach:


> I don't know how to press 3 keys since I need to press command option command f


There are valid cases where you need to emulate keyboard presses. In this case you need to look at the optional parameters to the keystroke command to incorporate modifier keys:


keystroke "f" using {command down, shift down, option down}


You can use any combination of the modifiers, as needed.

5 replies
Question marked as Top-ranking reply

May 31, 2022 3:24 PM in response to DavidMBeetle

Enjoy your AppleScript journey :)


This is a perfect example of the kind of progress most people go through when learning AppleScript.

In the beginning, you start of by trying to replicate the actions you would take normally (press these keys, click these buttons...) into where AppleScript's true strength lies, namely directly manipulating objects (in this case email messages). The joy? pain? comes in making that mental jump.


I have two comments to your script. In your final script you select the message via MessageID, which is perfectly valid and pseudo-unique. Just be aware you can select via any property of the message:


	-- your code
    open (first message of inbox whose message id = MessageID)
	-- check for a sender in a specific mailbox
    set theMsg to first message of mailbox "INBOX" of account "iCloud" whose sender is "hello@somewhere.com".  
	-- check for a subject
    set theMsg to first message of inbox whose subject = "Super Important Message"
    -- check for a flagged message
	set theMsg to first message of inbox whose flag index = 5


As for your earlier approach:


> I don't know how to press 3 keys since I need to press command option command f


There are valid cases where you need to emulate keyboard presses. In this case you need to look at the optional parameters to the keystroke command to incorporate modifier keys:


keystroke "f" using {command down, shift down, option down}


You can use any combination of the modifiers, as needed.

May 31, 2022 12:57 PM in response to DavidMBeetle

finally found the solution its instant it doesn't search for the email it just opens it tho but for me just as good

the code:

set MessageID to "Your message ID"

tell application "Mail"
activate
open (first message of inbox whose message id = MessageID)
end tell

If you don't know how to find you message ID then just watch this video



[Personal Information Edited by Moderator]


May 31, 2022 12:17 PM in response to 1ssmith

Thing is I'm fine with making scripts I've already written scripts of over 60 lines (formally 100 lines used function to shorten it). But its about opening the email I'm not sure about that part. But thanks for answering, I think you may of misunderstood my question.

This was my last attempt so far

tell application "Mail" to activate
delay 3
tell application "System Events"
	key code 58
	key code 55
	key code 3
end tell

I did this to try to automate opening the email by searching it but just opening it works too is there any way to do so. Please tell me.


I did find something on the internet that works but the thing is it takes too long to find it

on run
	tell application "Mail"
		set myInbox to mailbox "INBOX" of account 1
		set myMessages to every message of myInbox
		
		repeat with theMessage in myMessages
			if read status of theMessage is false then
				
				if my subjectIsInteresting("Link for your session") then
					open theMessage
					delay 4
					close window 1
				end if
				
			end if
		end repeat
		
	end tell
end run

on subjectIsInteresting(subject)
	
	-- do some regex magic here
	
	return true -- for now
	
end subjectIsInteresting

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How to search for an Email using apple Script

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.