Hide Mail at startup

I have a very narrow issue to do with Mail.
I am running Mojave on a 2017 27inch iMac.
My preferred setting for Mail is to have it open at login and remain hidden. That function in Preferences/Accounts/login items/hide checkbox ticked, does not work so the Mail launches and the main page is displayed.
I have removed Mail as a login item.
Instead I have created applications from Script Editor scripts and included one of them as a login item.
All of them have failed except 2. They are al variants of this:
tell application “Mail” to [run/launch/activate]
tell application “System Events”
set visible of process “Mail” to false
end tell
The run version fails after a few restarts.
The others cause Mail to flash open before hiding.
I am currently using this login application, but expect it to fail.
tell application “System Events”
set visible of process “Mail” to false
tell application “Mail” to run
end tell
Any suggestions? Perhaps you or Apple has identified and fixed the bug?

Hi Umberto
And welcome on Macscripter!
Don’t use “Activate” if you want Mail to run in background. Activate fires mail up :stuck_out_tongue:

Stick with this code:

tell app "Mail" to run

Or:

tell app "Mail" to launch

Pay attention, some events need a timeout :

delay 0.5 #half second

Therefore:

tell app "Mail" to run
delay 0.5 #half second
tell application "System Events"
    set visible of process "Mail" to false
end tell

Usually hiding applications isn’t smooth but I can be wrong as my system runs am older Osx. Try.
Remember to format your text by using the formatting button “Applescript” . As that helps us to read your question

Felice anno nuovo 2019

It won’t help. Because by putting Mail.app in login items, you lose control over Mail.app’s startup behavior.

Instead of putting Mail.app, you need the following approach:

  1. Save helper script (like one bellow) as application:
tell application "Mail" to close message viewers
  1. Grant to this helper application permissions to “Control Mail.app

  2. Put in login items this helper app instead of Mail.app

I was never clear as to the difference between activate, launch, and run and decided to do some research. The AppleScript Language Guide states:

https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html#//apple_ref/doc/uid/TP40000983-CH216-SW59

To confirm this I ran the following as an AppleScript application saved to the desktop. I use the term start in a non-technical sense, meaning load into memory.

tell application "Mail" to activate -- starts Mail, shows the interface, gives it the focus

tell application "Mail" to launch -- starts Mail, shows the interface, does not give it the focus

tell application "Mail" to run -- starts Mail, does not show the interface

I retested the above as an AppleScript script run by way of FastScripts and received the same results.

So, it would seem the OP only needs to use the run command. However, I saved the following as an AppleScript application and added it as a log-in item. After rebooting, Mail started but was visible. So, as Joy and KniazidisR note, something more is needed.

tell application "Mail" to run

I did try the other suggestions and none of them worked, although this may be because I’m running Catalina. It’s not precisely what he wants, but the OP may want to try the following, which works as desired on my computer as a start-up application.

tell application "Mail"
	run
	set miniaturized of every window to true
end tell

Fredrik71. I tried your AppleScript version before posting and it did not work. I just tried again and it seems to work fine. So, I must have done something wrong before. Apologies.

I tested our scripts with Script Geek and (just to confirm the results) with Nigel’s timing script and the results were:

Fredrik71’s script - 0.050 seconds

peavine’s script - 0.471 seconds

This surprises me a bit. Perhaps do shell script returns control of the computer while the Mail app is loaded in the background.

An alternative:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

set ws to current application's NSWorkspace's sharedWorkspace()
set theURL to ws's URLForApplicationWithBundleIdentifier:"com.apple.mail"
ws's launchApplicationAtURL:theURL options:(current application's NSWorkspaceLaunchAndHide) configuration:(current application's NSDictionary's dictionary()) |error|:(missing value)

Launching an app is generally an async thing — there’s typically no waiting on it to happen.

Thanks Fredrik71 and Shane for the information. I remained perplexed why my script returned first-run timing results of 0.471 seconds, which turned out to be 0.640 seconds on retesting:

tell application "Mail"
	run
	set miniaturized of every window to true
end tell -- 0.640 seconds

I tested without the miniaturized property and Script Geek returned 0.048 seconds:

tell application "Mail"
	run
end tell -- 0.048 seconds

Just to confirm, I tested with a timing script, which returned 0.648 seconds if the script set the minaturized window property and 0.049 seconds without.

So, it appears that using a script to run Mail is reasonably quick but setting a window property at the same time slows things considerably. Ironically, on retesting, it appears that setting the window property is not necessary and that all that’s required is the run command, although the OP said this didn’t work for him.

Yes, but he was not told that the helper app should be added to the Login Items, not Mail.app.