Howdy. I know I can connect to a server (another Mac on my network) with an AppleScript like this:
open location URL
But, the problem for me is this generates a new Finder window. I am running a script to connect to that server periodically (because sometimes the server machine restarts and the connection gets lost), and the script will make a new window every time. What I really want to do is somehow check to see if I am connected to the server or not, and only run the connection macro if I'm not connected. Ideally I would make the connection in the background, without bringing the Finder to the front. Basically do it in an invisible way.
There are a couple of macOS commands that might tell you if you are connected to a server (or not.) Once you figure out which way works for you, we can probably help you automate it with KM, if you need help.
One command is "ls /Volumes" which seems to show remote mounted file systems.
Another command is "df -h" which will also show you similar information.
Another command is "mount".
Using the output of those commands, plus a KM conditional, you can detect if a remote file system with a particular name is mounted or not. Perhaps like this:
mount volume "smb://serverAddress/shareName" as user name "username" with password "password"
...changing serverName, shareName, username, and password to suit.
You can do this whenever you need to do something with the server -- if you are already connected it will complete without doing anything.
This may be better than the (more usual) UNIX command methods @Airy listed because un-gracefully disconnected shares often remain listed in /Volumes or remembered by mount (you'll often see after a re-mount that these list shareName -- the previous mount -- and shareName-1 -- the new one).
Even better would be to either stop the server from restarting unannounced or, if you can't control that, to connect to the share only when you need it and disconnect when done. Both will help prevent problems and, importantly, potential data loss.
This is fantastic. I am now using the “ls /Volumes” command in an if-then-else, as you suggested, and it looks as if it’s going to work just fine. Thanks a million.
Keyboard Maestro, and this community, to the rescue— again!