Introduction
Ok, so this is Monday morning. Your brain is still sleeping and you need someone to tell you what to do with this bright screen. What about someone that can help you, at least by starting the applications you usually need?
The Code
By writing a simple batch file and letting it run automatically in the start-up phase of our operating system, we can achieve our target: automate the start of usual applications. Let's start from the basics.
Create the Batch File
You can use a lot of different tools to do that, but we will simply use Notepad. Create a new file, save it as "StartApplications.bat" or whatever you want. The important thing here is the ".bat" extension.
Now we can populate the file with our script.
Turn Off the Command Line Echo
In this case, we don't want to print out to the console every command we are using. So the first line of our batch will be...
@echo off
We will use echo to print out information only when we need them.
Start All the Apps or Only the Ones You Need Today
Let's say that you have a list of applications that you use every day. In my case, these applications are: a browser (let's say Chrome), an IDE (let's say Visual Studio), an e-mail client (let's say Outlook), and so on...
So I have my personal list of applications, you will have yours...
But suppose that today you will do something different. We don't want to open all these applications, only to close them after few seconds. We want to choose if we would like to start all these applications or select the ones we need.
And that's how we will do that.
First of all, we print out the list of applications that we can start:
ECHO Here are some apps you will probably need today:
ECHO. -Visual Studio
ECHO. -SourceTree
ECHO. -Outlook
ECHO. -Chrome
ECHO. -Spotify
Then, we choose if we would like to run all the applications or not:
SET /p startall=Do you want to start them all? [y/n]:
IF "%startAll%"== "y" GOTO StartAll
SET all=0
Note the use of the SET
command with the /p
parameter. Using this parameter, we are telling the program to read the value of the startAll
variable from the next user input.
What's the meaning of the GOTO
command, and what will happen when we will go to "StartAll
"? What's the aim of the all
variable?
We will answer this question in a minute, but first, let's say that the user dosen't want to start all the applications, so the startAll
variable is not "y
" and the all
variable is set to "0
". Let's see how to proceed.
Senario #1: We Want to Choose Which Apps Will Be Started
Let's say that today we don't want to read our e-mails or we don't feel like writing code (!!!). No problems, our little program will ask us about each of our apps to see if we want to start it or no:
SET /p visualStudio=Do you want to start Visual Studio? [y/n]:
As before, we are using the SET
command to read the user input. Let's use this value:
IF "%visualStudio%"=="y" GOTO StartVisualStudio:
:AfterVisualStudioStart
[...]
:StartVisualStudio (
CD "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE"
START devenv.exe
IF %all%==0 GOTO AfterVisualStudioStart
)
Here we are. If we need to start Visual Studio, what we need to do is to use the CD
command to reach the executable directory and use the START
command to run the executable (please note that these paths can be different in your machine!).
After the start of Visual Studio, we need to return to our code to ask the user for the other apps. We do this only if the all
variable is set to "0
", so only if we want to choose which apps will be started and which not.
Let's repeat these operations for the other applications using the same schema:
Ask the user: Do you want to start this app?
if yes --> go to the code that start the app and then turn back
If we type something different from "y
", we will simply skip to the other question.
Senario #2: Start All the Apps
Let's say that we want all the apps we have in our list. So let's skip all the previous questions and go to the point immediately before the start of our pseudo routine. As we see before, we do that by calling the GOTO
command:
IF "%startAll%"== "y" GOTO StartAll
Well, so we can place the StartAll
bookmark after the questions block and then set the all
variable to any value different from "0
".
:StartAll
SET all=1
In this way, none of our routines will return to a previous point in the code. That's because we have inserted a specific condition:
IF %all%==0 GOTO AfterVisualStudioStart
All the apps will be started with no user interaction, we just need to grab our hot coffee and wait for them.
Comfortable, don't you think? What a lazy person...
Add the .bat File to the Start-up Programme
So, we are done with our file. Now we need to let this batch run every time we log in.
We can do that by adding our file to the start-up folder. We just need to press Windows key + R and run "shell:startup
” (without the quotes) in the “Open” edit box and click “OK.” By copying the batch here, we are telling Windows to run it every time a new user session starts.
Points of Interest
I hope you have enjoyed this brief article. One of my targets was to explain some of the basic commands that we can use in a batch script and to do this in a practical and funny way. I think that, as programmers, sometimes it is good to see from the outside to apply our knowledge in our daily life.
Attached you can find a copy of my personal batch (please note that I've removed the path to my Spotify executable, sorry, it is top secret :P), so you can start by modifying it if you don't want to start from scratch!
Enjoy!