A Script For Scripting A Script

I make a lot of scripts. I have scripts intended for long term management of tasks and files. I have made short term scripts, or single-use scripts to batch manipulate a directory of files in some custom manner. I have scripts that run at launch and other scripts to interact and automate the gathering of information from web based services.

It occurred to me that I had never automated the process of making a script.

I know there is not much to starting a new script, but thats not really the point of automation. It is simply about reducing the amount of un-necessary repetition in your workflow. I realized that when I start a new script its almost always a shell script, almost always on the desktop, and there is almost always the same bit of text inside the script to get me started.

Here is an example of my own boiler-plate shell script:

Not much to it, but it is a great starting point. You are probably wondering why I have an empty main function even though it does not offer much in the way functionality. I find it beneficial to break up shell scripts in to functions for increased readability as well as code re-usability. A well written shell function can be copied and pasted in to any other future scripts without having to change much code.

Lets take a look at the code to generate this script:

You will probably want to update the NAME variable so that it reflects your own name. You don’t have to change the DIR variable unless you want your scripts to go some where other than your desktop. The script creates the file on the desktop, generates the boiler-plate content, sets executable permissions on the script, then opens it in your default text editor.

I should note that you do have the ability to set a filename when creating a new script. The IF/ELSE statement at the end of this code checks to see if you have sent it a filename, if it does not find one it simply names it script.sh. You do not need to type out the .sh extension because it is automatically added for you in the script. Also, don’t use spaces in your script filenames.

I use this script in conjunction with an Alfred extension for optimal productivity. I am attaching the extension below so feel free to download and try it out.

I simply launch Alfred and type either script or script someFilename

If shell scripts aren’t your thing, this script could be easily adapted to whatever scripting language you prefer. If you need any help modifying it, just let me know and I will do my best to help you out.

Enjoy

Download


Start a new script extension