Creating An Information Notification With Shell Scripts And Automator

You can go to the Terminal and use simple one-line shell scripts to get various pieces of information such as the date and time, your Internet IP address and your Wi-Fi network name. Using Automator, you can incorporate these bits of information into a simple notification that appears whenever you press a keyboard shortcut.
You can also watch this video at YouTube.
Watch more videos about related subjects: Automator (50 videos), Terminal (43 videos).

Video Transcript

Hi, this is Gary with MacMost.com. Today let me show you how to get some handy information from shell scripts in the Terminal and turn that into a notification you can bring up with a key press.
MacMost is supported by more than 600 viewers just like you. Go to MacMost.com/patreon. There you could read more about the Patreon Campaign. Join us and get exclusive content and course discounts.
So you probably already know that you can find some useful information if you go into Terminal and use Terminal commands, also called Shell Scripts, to get this information and display it in the Terminal. But you can also use Automator to call these same commands. Then since you're in Automator you can present that as a Notification. So let's look at an example that will display three useful pieces of information whenever you need them.
I'm want to start off here in Terminal looking at the information that we're going to get. The first thing that we're going to get is really easy. It's just simply the date and time. Why would you want to use that? I mean you probably have the date and time at the top of the screen. Right. Well, sometimes like with me I don't like to display the date. It takes up too much space here. But when I want to know the date I have to go up here and click and look at the full date. Other people don't even like to display the time. They don't always want to have a clock in their face. So in the Terminal a simple command date will give you all that information. Not only that but you can customize the format.
So, for instance, I can do date and then in quotes put a plus and then certain symbols. Like %A will give me the day of the week. I could do the same thing, capital Y instead of A, gives me the year. Now if I want to use a much longer string, this, that will format the date to look like this which is a nice format to present. If you really want to know all the different ways you can format the date you can do manual, man, and then date, the command we're using and you go into the manual here. You can see right here that the input format, parse using something called strptime. So if you do a Goggle search on strptime you come up with tons of pages that will spell out all the different formatting options. 
Now let's look at the next thing we want to get. I want to know the name of the WiFi network I'm on. Now you can do this using a command in the Terminal called Network Setup. You can see there's a ton of different information that comes up here when you say Network Setup. It just basically tells you how to use it. So let's look at an example. Here I'm going to use this command, networksetup dash getairportnetwork space en2. What I get here is Current Wi-Fi Network colon and it tells me the name of a WiFi network. In this case I'm using my iPhone to connect, not my regular WiFi network.
Now where did that en2 come from. Well, you have different interfaces for connecting to the internet. You've got ethernet, you've got WiFi, possibly things like Thunderbolt. All sorts of stuff. If you go into About This Mac and then System Report and then down to Network you'll see here different active services. I could see here, for instance, en0 is my ethernet connection, en11 is a direct connection to my iPhone as it's plugged in, and en2 is my WiFi. So en2, the BSD device name for my WiFi network, that's what I want to use. It may be different on yours. It may be en0 or en1. 
Of course you could always just check. If you go in and say en0 you're not going to get a valid result. So you can do en1 that doesn't give you a valid result either. en2, bingo, there you go. Now I don't want to use Current Wi-Fi Network colon space before it so I'm going to modify this a little. If I pipe this command, this is a pipe right here, if I pipe this command through awk I can look for the colon space and get everything after it. So now that command gives me just the exact name of the WiFi network. 
This is handy to have especially if you move around a lot with your MacBook Pro and you want to quickly see what WiFi network you're on without having to go into the menus here and look.
Now the third piece of information that I want to get my IP address. Now if you get your local IP address you're going to get the IP address for the router or WiFi network that you're on. You want to get the IP address out on the internet. So what other devices see you as like when you're connecting to a web server or something like that. What IP address that they think you're coming from. This is really handy for a lot of people especially in office situations where they need to know the IP address for security reasons. 
The only way to really get that is to actually call out to a website and ask it to tell you what the IP address is. There are a ton of different websites that will return this information. So here's one. I'm just going to use cURL which is a way to go out and call out and get a URL. Get information. Kind of like loading a webpage. But this is a very simple webpage that just returns the IP address in plain text format. So when I type that you can see it gives me the IP address.
They're the three types of information that I want to get. So let's do that using Automator. I want to use Automator because I want to make this a QuickAction so I can easily assign a keyboard shortcut to it. So here we go. QuickAction and I'll choose that. Now to use these commands all I need to do is search for Shell and you can see there's Run Shell Script. I can drag that in here. Now I can set the type of shell I want. By default yours might be set to Bash. I'm going to set to Zshell because this is the default now on macOS Catalina. The shells are very similar to each other. Slightly different dialects of the same language. But every once in a while something won't work if you're not using the proper shell. So I'm going to use Zshell for all of these.
First I'm going to paste in the date command here. This is going to return the value for this. I can look at Results and see what the results are here. Before I run it I'm going to set Workflow receives no input because I don't need any input and in any application. Now let's try it out. When I hit Run you can see the results here are that date. Great, so it's working. Now what I want to do is put these results into a variable. I'm going to search here for Variable and I'm going to use Set Value of Variable. I'm going to put that as a connected action here after Run Shell Script. I'm going to click on Create New Variable and assign this variable a name. I'll just call it Date and I'll hit Done.
Now you can see at the bottom it lists the variables I've got. If you don't see that here make sure you click on this button. If you're on this button here get a Log. This button will give you Variables. So this will set the value of the Variable Date to the results of the shell script. I'd like to do it for the other two. I'll do Run Shell Script again and I'll add this to the bottom here. I can Control click here and Ignore Input. So disconnect this. This is a pair of actions that won't run together. Now I'm going to start another pair. This shell script is going to be the one that gets my IP address. So I'll do it like that and then I'm going to do Variable, Set Value of Variable and stick that underneath here. I'm going to select a new Variable and call that IP Address. 
Now I'm going to do this for a third time. I'm going to do Run Shell Script. I'm going to put it underneath here. I'm going to Control click here Ignore Input and I'm going to use the script that will get the WiFi network. I'm going to store that in the variable and I'll call that WiFi. Now I have those three variables there. So now what we need to do is display them in a Notification. Automator has something for this! If I search for Notification you can see Display Notification. So there are two good reasons to use Automator for this. One is I can easily assign this as a service with a keyboard shortcut, which we'll do in a minute. Another is Display Notification action makes things really easy. 
So I'll add this here at the bottom. I'm going to Ignore Input from above this. Now I need to fill these in. Let's assign a title. I'll call this Current Information. I can have a subtitle under it and a message. So this is perfect for three pieces of information. In the Subtitle let's go ahead and put the date. So instead of actually typing there I'm going to drag the Date from there up. Now for message I'm going to drag WiFi here but I can still type in this field. So I'm going to hit a space, put a slash and another space and then I'm going to drag IP Address. Now message is just one line so if I try to put all three of these in the message it would be too long. So it's perfect that I have the Date as the subtitle of this and the message is WiFi / IP Address.
Let's run this and see what happens. There's my Notification. It says Current Information. There's the date. There's my WiFI network and there's my IP Address. All displayed there in quick notification and I can close it. Now since I've already set this up as a QuickAction all I need to do is do File, Save and I can save this as something like Show Current Info. Hit Save. Now this will show up in QuickActions as a Service for anything because I set it up for any application. So for instance, I'll click on the background in the Finder and I'll go to Finder, Services and there's Show Current Info. If I select it there it will bring up that notification right here. If this is the first time you're running the service in the Finder you may actually get a dialogue asking for permission to do so.
Now let's go into System Preferences and I'm going to go and set the keyboard shortcuts. Under Services I'm going to look under General here and there's Show Current Info. I can set a shortcut. So let's do something like Command F1. Now that I've set it to Command F1 I can hit Command F1 and it brings up the information right there. I did that in System Preferences. I can do that in the Finder. I can bring that up. I can do it in any app although I may get asked permission sometimes if it's the first time I'm doing it inside of a certain app. But eventually you won't have those anymore and Command F1 will bring up this information anytime you need it.
Also notice that every time it brings it up I have to go over here and Close it. If I wanted it to appear for just a second and go away on its own I could go into System Preferences, into Notifications, and then I can look for Automator here on the left. I can set the Style. Instead of an Alert I can have it appear as a Banner. So when I change that look what happens. It will come up and then it will go away on its own after a few seconds. Just like that.
So this is just an example of three pieces of information that I could find handy to appear here in a Notification like this. But if you use the Terminal to get other pieces of information or you have Shell Scripts that generate other information you could include them in here just as easily and have them appear as a quick notification with a keyboard shortcut.

Here is the line of code for the date and time:

date '+%A, %B %d, %Y %I:%M:%S %p'

Here is the line of code for the IP address:

curl -s http://ipecho.net/plain

Here is the line of code for the Wi-Fi network name:

networksetup -getairportnetwork en2 | awk -F": " '{print $2}'

Comments: One Comment

    Al Jimenez
    5 years ago

    Great video. Good job. Now I gotta go think of other things to get notified on with this approach
    i.e: uptime="10:28 up 3 days ", pmset -g accps, and pmset -g ac. Endless... :-)

    Thanks.

Comments are closed for this post.