Part of Nobumi Iyanaga's website. 9/11/04.

logo picture

Unix grep for OS X and Nisus Writer Express

This page replaces the old page entitled Unix grep for Nisus Writer Express, which is now incorporated in this new page.
Version 0.5.1 (a bug fixed version, released on 04-09-10).

Mac OS X is a powerful Unix OS enabling the users to use many Unix tools such as grep, awk, sed, etc. Unfortunately, these are generally command line tools, not very well integrated with the GUI environment. By "well integrated with the GUI environment", I mean not necessarily (or not only) the lack of graphical interface for these tools (there are some for grep for instance), but a good interface that would interact with GUI applications. In the Classic Mac OS, for grep for example, there is/was an incredibly useful application named MgrepApp, which could be used as a tool for a kind of concordance: it could not only find very quickly occurrences of a searched term in multiple files (even in multiple folders if we used the alias functionality), but double-clicking on a found line, we could often open that file with the editor/word-processor (in the case of Nisus Writer) that created it and jump to that line (tag jump functionality).

In OS X, there are some good GUI grep applications, such as MultiFind (free) [this uses OniGuruma Regular Expression Framework for Cocoa] or grepArtee 2.0 (shareware: $8.99). MultiFind in particular has the menu-item named "Jump" with which one can open the file with TextEdit, but it is not possible to select the intended line...

In fact, MgrepApp could achieve the tag jump functionality using AppleScript/AppleEvent which allowed to open the found file (this can be done in OS X also) and select (highlight) such or such byte-offset in the file -- but this functionality is not implemented in almost none of the Cocoa applications (such as TextEdit or Nisus Writer Express).

I present in this page two pairs of VERY BASIC scripts that try to fill this gap. Actually, the result is not satisfactory at all, but *perhaps* (and *hopefully*), this is a little better than nothing.

Note on this version:
A silly bug has been fixed in "open_file_from_grep_res.scpt" and in "open_file_fromGrepRes.scpt": when the found text contained one or more ":", they were removed from the string that was put in the Find dialog box. Now, these ":" are preserved.

The first pair is to be used with the Finder and TextEdit. The utility named "unix_grep" is a little application made with Platypus 2.2, containing a utility named CocoaDialog 1.1.3 and a perl script (these are in "" package, in and CocoaDialog is a very useful utility which " allows the use of common GUI controls such as file selectors, text input, progress bars, yes/no confirmations and more with a command-line application. It requires no knowledge of Cocoa, and is ideal for use in shell and Perl scripts (or Ruby, or Python, or... etc)." This complicated form was needed because AppleScript dialogues seem to not accept non-Roman input (all non-Roman input in dialogues become "??" in AppleScript).

How to use unix_grep

You would put unix_grep anywhere you want, but preferably on the Desktop. You double-click its icon to launch it. A simple dialog appears, prompting you to enter the term to be searched. You enter that term (unfortunately, pasting in this dialog does not work). A file/folder selecting dialog will open, asking you to select a folder containing files to be searched. Please select a folder, NOT a file, containing TEXT files (with the extension .txt) in UTF-8 encoding. If you select a file, that would be a non-sense: a dialog asking you to select a folder will appear, and the application will quit.

If all goes well, the result will be written in the file "grep_res.txt" on the Desktop (or, if this file exists, the file will be named "grep_res1.txt", "grep_res2.txt", and so on) in UTF-8 encoding, and will be opened by TextEdit. If the default file encoding in TextEdit is not set to UTF-8, it will fail to open it...

To configure the option of grep, you will have to open and edit the Perl script inside "" ( Open it with any editor (for example TextEdit), locate the line which says:

my $grep_options = "-Hbn"; # Edit this line if you want to change the grep options...!

and change "Hbn" to any options that you want.

When the file, "grep_res.txt" etc., is opened in TextEdit, you will be (hopefully) able to do a "tag jump" to one of the lines with the AppleScript script named "open_file_from_grep_res.scpt".

How to use open_file_from_grep_res.scpt

Put this script in /Users/[your_account]/Library/Scripts/TextEdit/ (if this folder does not exist, just create it yourself), and activate Script Menu using /Applications/AppleScript/Install Script if it is not yet activated (you can use FastScripts as well).

Select a line in the file "grep_res.txt", etc. Be sure to select the WHOLE line (you can select the return character at the end of the line as well), copy it (Command + C), then choose open_file_from_grep_res from the Script Menu, at the right side of the menu bar.

If all goes well, the file in question will be opened in TextEdit, the Find dialog will open and the text of the line will be entered in that dialog, then the first occurrence of that text in the file will be selected. -- You see how this script is supposed to work with this description: it will take the copied text, split it into the file path and the text of the line, then tries to open the file, open the Find dialog, select all in that dialog, paste the text of the line, and do a "Find Next".

Even if all works well, it does not guarantee that the selected line in the file is the intended line: if the file contains more than one lines of the same text, it will select only the first one... But the most problematic part of this script is when it tries to get the contents of the clipboard; it seems that often, this does not work... So, if the script quits with some error message, you might want to try it again, copiing again the line that you want to see in the file.

The second pair of scripts is to be used with Nisus Writer Express.

The first one is an AppleScript script named UnixGrep.scpt. This is a little grep macro that can be used from within Nisus Writer Express. You can use Nisus Writer Express as the user interface for Unix grep, and search in UTF-8 text files for words containing Unicode characters.

How to install and use UnixGrep.scpt

Put UnixGrep.scpt in /Users/[your_account]/Library/Application Support/Nisus Writer/Macros/ folder.

Download Jon's Commands X from, and put it in /Users/[your_account]/Library/ScriptingAdditions/ (if the folder "ScriptingAdditions" does not exist in your Library, create it; note that there is no space between "Scripting" and "Additions").

I assume that you have launched Nisus Writer Express, and you have a Nisus Writer Express window open on your screen.
I assume also that you have folders full of TEXT files in UTF-8, with ".txt" extension.

Type any word you want to look for in your Nisus Writer Express window, and select it. You can use Unicode characters in this word.

Choosw "UnixGrep" in Macro menu in Nisus Writer Express.

A file dialog will open, asking you to locate the folder in which you want to do your search. Navigate to that folder, and press the button "Choose".

After a while, a new window will open in Nisus Writer Express, with the title "grepRes.txt" (or if this file exists, "grepRes1.txt", "grepRes2.txt", etc.). It displays the result of your grep search. -- This file is generated on your Desktop. -- Be sure that the preference for "Plain Text Encoding" is set to UTF-8.

How to Configure grep options

The default grep options is -Hbn, that means:

There are other options that you can set. To set options:

Choose "UnixGrep" in Macros menu while holding down the Shift key. A list selecting dialog, with the following options, will appear:

Case insensitive
Match words
Extended regex
Recursive search
Print byte offsets
Print line numbers
Print file names
Print multiple lines

You can select multiple options clicking on the items while holding down the Command and the Option keys.
If you select "Print multiple lines", another dialog will appear, asking you to put the number of lines to output, with the default number "1". This is the number of line(s) that will be in the output before and after the matched line. So if it is "1", there will be 3 lines for each matched item. The maximum is "5" in this script.
The item "---" is a simple separator between the search options and the output options. Don't choose this item (if you select it, the options will be set to the "factory default", i.e. "Print file names", "Print byte offset" and "Print line numbers").

When you have selected once the options, they will be the default for next times. If you want to change again the options, choose again "UnixGrep" with the Shift key down.

To view the current options, choose "UnixGrep" while holding down the Option key. The script will display the current options and quit.

If you choose "UnixGrep" while holding down both the Shift key and Option key, you will be able to change the grep options, diaplay these options, and quit the script without performing the search.

Now, with the AppleScript macro open_file_fromGrepRes.scpt, you can do a "tag jump" from a line in the result file "grepRes.txt" (or "grepRes1.txt", "grepRes2.txt", etc.) to the intended file and (hopefully) the line.

How to use open_file_fromGrepRes.scpt

Put the AppleScript macro open_file_fromGrepRes.scpt in the folder /Users/[your_account]/Library/Application Support/Nisus Writer/Macros/.

I assume that you have opened the result file "grepRes.txt" in Nisus Writer Express. Select a line in the file "grep_res.txt", etc. Be sure to select the WHOLE line (you can select the return character at the end of the line as well), then choose open_file_fromGrepRes in the Macro Menu. The intended file will be opened in Nisus Writer Express (provided that the preference for "Plain Text Encoding" is set to UTF-8); the Find dialog will open, and the text of the line selected in "grepRes.txt" will be entered. The first occurrence of that text in the file will be selected.

As I noted for the script "open_file_from_grep_res" (to be used from within TextEdit), this script has the same "weak-point": it selects the first occurrence of the line text in the file, which may be not necessarily the intended line. But as we can use the AppleScript command "get selected text of document 1" in Nisus Writer Express, there is no problem of "Copy" in this script. In this sense, it is a little more reliable than the script for TextEdit.


Please download the package containing these scripts/aplication from this link (268 KB).

I would appreciate any feedback, bug report, suggestions. Thank you in advance!

Go to Research tools Home Page
Go to NI Home Page

Mail to Nobumi Iyanaga

frontierlogo picture

This page was last built with Frontier on a Macintosh on Sat, Sep 11, 2004 at 10:33:50 PM. Thanks for checking it out! Nobumi Iyanaga