Is there a script to launch Mail and check for new Mails

hi,
I used this script on Mojave to start the Mail app and check for any new mails. If any unread mails are there, the Mail app should be kept open otherwise the window should be closed.

tell application "Mail"
launch
activate
delay 4
if (count items of (messages of inbox whose read status is false)) > 0 then
	activate
else
	close every window
end if
end tell

But since Catalina, the Mail app just opens and closes after the delay.
Do you guys have any suggestions?

Make a new Apple Script and put this in the code:

tell application "BetterTouchTool" to set dndEnabled to get_number_variable "SystemDoNotDisturbState"

if dndEnabled is 0 then
	tell application "BetterTouchTool"
		set badgeNumber to get_dock_badge_for "Mail" update_interval 5
		set handoffDevice to get_dock_badge_for "Mail-handoff" update_interval 5
	end tell
	if badgeNumber is not missing value and handoffDevice is missing value then
		if application "Mail" is running then
			return badgeNumber
		else
			return ""
		end if
	else if handoffDevice is not missing value then
		if handoffDevice contains "iPhone" then
			return "iPhone"
		else if handoffDevice contains "iPad" then
			return "iPad"
		else if handoffDevice contains "watch" then
			return "Watch"
		else if handoffDevice contains "Mac" then
			return "Mac"
		end if
	else
		return ""
	end if
else
	return ""
end if

Make sure that 'show in any open touchbar group' is ticked.

Then for the trigger, make an Apple Script (async in background) trigger, and put in this:
if application "Mail" is running then

tell application "System Events"

tell process "Dock"

try

click UI element "Mail" of list 1

end try

end tell

end tell

else

tell application "Mail" to activate

end if

Hi,
First of all, thank's for your reply.
Is there any way to have everything in one script as before? And do you know why my version no longer works on Catalina?

Just to make things clear - the scripts @Future_Face posted do not act like yours. These scripts have to be put into an Apple Script Touch Bar widget. If Mail is in the Dock and open they will count the new notifications.

I just tested your script @Black_Simorgh. It works fine here running from Script Editor. The problem is, I guess, that the scrip just closes the window. So the first time you launch mail with a new window it will work fine, just closing the window at the end if no mail was found. Though, if you run the script later on, there will be no window that can be found.
I would, as a workaround either create a new window at the beginning of the script (can be quite annoying) or quit Mail.app at the end of the script using this:

tell application "Mail"
	launch
	activate
	delay 4
	if (count items of (messages of inbox whose read status is false)) > 0 then
		return "Mail's in"
        -- This basically keeps the app frontmost
	else
		quit
        -- This quits Mail if no new mail was found
	end if
end tell

Actually, I use a combination of multiple scripts in one to start my predefined setup.
Here are the scripts I combined:

tell application "Finder"
	if exists window 2 then
		activate
		tell window 1
			if collapsed then set collapsed to false -- Finder
		end tell
		repeat with w in (get every window)
			select w
		end repeat
		tell application "System Events" to keystroke "t" using {option down, command down}
		if visible is true then set collapsed of every window to true
	else
		if visible is true then set collapsed of every window to true
	end if
end tell

tell application "Mail"
	launch
	activate
	delay 4
	if (count items of (messages of inbox whose read status is false)) > 0 then
		activate
	else
		close every window
	end if
end tell

tell application "Franz"
	launch
	activate
	delay 3
	tell application "System Events" to keystroke "w" using command down
end tell

tell application "Safari"
launch
activate
end tell

Since I run this to start the Apps I'm continually using, it makes no sense to quit the Mail app. And the Mail window should be opened, and just if at the start, there is a new mail, it should be kept open.

Ok then, got it. For the if new mail part, replace this activate with this in order to open a new window if an unread mail is found:

tell application "Mail"
activate
try
id of message viewer 1
on error
makenewmessage viewer
end try
end tell

Alternatively you can put one of the keystroke commands:

tell application "Mail"
    activate
    tell application "System Events"
        keystroke "0" using {command down} --> Message Viewer ⌘0
        keystroke "1" using {command down} --> Inbox ⌘1
        keystroke "2" using {command down} --> Drafts ⌘2
        keystroke "3" using {command down} --> Sent ⌘3
        keystroke "a" using {option down, command down} --> Address Panel ⌥⌘A
        keystroke "0" using {option down, command down} --> Activity ⌥⌘0
    end tell
end tell

I'm not on my Mac RN so I can't provide the entire code :wink: but I think this could work! Looking forward to your feedback,
Cheers :beers:

I tried to run your first script to test it, but I've got a syntax error.
But I combined your second script with mine and got this:

tell application "Mail"
launch
activate
if (count items of (messages of inbox whose read status is false)) > 0 then
	tell application "System Events"
		keystroke "1" using {command down} --> Inbox ⌘1
	end tell
else
	close every window
end if
end tell

And that seems to works. I will know it for sure tomorrow when I start my Mac, but for now, I thank you.

1 Like