New Empty File Tutorial
In this tutorial we will learn how to make a simple contextual menu item which allows us to create a new empty file in current folder (or Desktop).
We will use "touch" command-line tool which changes modification date for existing files but also creates new files.
This tutorial is not intended to teach how to use Terminal, command-line tools, shell or Unix in general but the following short introduction and step by step instructions should allow you to get started with OnMyComamnd without any prior knowlege.
Before writing a command description for OMC you should try executing your command in Terminal and see how it works. Once you are satisfied with the results you can move it over to OMC.
So let's start: open "Terminal.app" (located in /Applications/Utilities folder) and type the following:
This simple command creates a file named "newfile.txt" in current working directory - if you opened a new Terminal window it is your home directory: /Users/you. Please note that the second time you call this command it will not create a new file but will change the modification date of the existing file. To get more information about "touch", type "man touch" in Terminal:
If you want to create a new file in directory other than current working directory you need to specify the path to new file, for example:
If you are not familiar with Unix, the tilde "~" character used in path above specifies current user home folder so the whole path is expanded to "/Users/you/Desktop/MyNewFile.txt".
Armed with this basic information about "touch" tool and Unix we can start OMCEdit to design our command:
Command description editor dialog starts. Enter a name of this new command. This is the name that will appear in contextual menu.
Choose "First Level" in location popup button. This way your contextual menu item will be placed in root menu. You can also specify a submenu for grouping several commands or use default submenu: "On My Command".
Next, choose "Folder" for activation mode. "Activation Mode" controls in what situation your contextual menu item is going to show up. If you choose "Folder" as your activation mode, this menu item will appear only when you control click on folder. It will not appear when you control click on selected text in some editor and it will not appear if you click on file. You can experiment with different activation modes and see when the menu items shows and when it does not.
The last and the most important thing is to write the command itself. In this example we will just use the command we tested in Terminal to create the new file on Desktop.
You can ignore the rest of options available in OMCEdit for now: your first OMC command is ready - it was not very painful, was it? You can save it now.
|Switch to Finder and control-click on Desktop. You should see your new contextual menu item like this:
|Now when you choose this item, your new file should be created on Desktop. If you don't see it, Finder might have not refreshed it so you need to select something on Desktop to kick Finder.
This first command is very easy but not very powerful. It always creates the file on Desktop no matter which folder you control-click on. It other words it does not use any context information. Suppose we want to create a new file in the folder on which you control-clicked (including Desktop). How would we do it? Fortunately OMC makes it very simple and you should probably pay attention to what will be presented below because it is the heart of OMC and the reason why it is so flexible and powerful.
OMC has a concept of special objects (or special words) which are replaced at runtime with proper information. One of those special objects we are going to use in our command is __OBJ_PATH__. This object is replaced at runtime with Unix-style path to file or folder you control-clicked on. If you clicked on Desktop, __OBJ_PATH__ will become /Users/you/Desktop. Special words begin and end with double underscore, all characters are uppercase and never contain a space. Words starting with "__OBJ_" prefix get their values from context, in other words, from object you ctrl-clicked on. Another example of context object is __OBJ_TEXT__ which is resolved at runtime to selected text. When you click on "Add Object" button in OMCEdit, you can insert those words into your command:
|We want to replace our fixed path in original command with the special word to make the path dynamic:
|Now when you choose the "Create New Empty File" item from contextual menu it will create "MyNewFile.txt" in the folder you clicked on. If it works for you: congratulations, you've just concluded the basic OMC tutorial. If you want to build more powerful commands there is much much more to learn about OMC. More documentation is available in Command Description Manual. The documentation is quite technical and describes command format as it is saved in plist file (XML). Luckily all options are avialable through OMCEdit GUI so you can learn a lot just by clicking around and exploring in OMCEdit.
Still having fun? Here is a little homework: Finder has problems with refreshing newly created items so sometimes they don't appear immediately where they should. OMC has a way to notify Finder about changed files or folders. Add this "kick Finder" option to your command description.