man pages and applescript

i’ve two problems: first, to get a summary of all man-commands in a certain path (to build a list about them)
Second, to debug the text-output of man pages using Applescript: the titles and some words, hyphens have twice characters, punctuations.
try this:

do shell script "man mv"


why not online?


on openManPageOnline(topic)
	set section to word 2 of (do shell script "man " & topic)
	open location "" & section & "/" & topic & "/"
end openManPageOnline

Hi Stefan
nice solution- the only hook here is, that i’m not always online.
Most of my applescripts are done offline and i need the man pages on the fly, always. Another reason i like is to use the tools which i carry in my own boat :wink:

i’m able to filter the biggest bugs with find-replace and applescript’s item del., but its not enough. What a strange man output, its a mess…
Also a summary reference like in Onyx should be possible.

here is another way, it prints out the man page and creates a PDF

property keyterm : "grep"

set keyterm to text returned of (display dialog "" default answer "")
	set manFolderPath to ((path to documents folder as Unicode text) & "Unix_man_Pages (PDFs):")
	set manFile to manFolderPath & keyterm & "-ManPage.pdf"
	set fileURL to CheckManFile(manFile)
	if fileURL is not "" then
		tell application "Safari" to open location fileURL
	end if
	do shell script ("mkdir -p " & quoted form of POSIX path of manFolderPath)
	set manFilePath to quoted form of POSIX path of manFile
	do shell script "/usr/bin/man -t " & quoted form of keyterm & " | /usr/bin/pstopdf -i -o " & manFilePath
	tell application "Finder" to set fileURL to URL of file manFile
	tell application "Safari" to open location fileURL
on error errorMsg number errorNum
	display alert "Error " & errorNum message errorMsg buttons "Cancel" default button 1
end try
-- end clicked

on CheckManFile(m)
	tell application "Finder"
		if exists file m then
			return URL of file m
			return ""
		end if
	end tell
end CheckManFile

A simple command to open it in preview (no internet connection required)

do shell script "man -t grep | open -f -a preview"

There is also the “bwana” application, which opens a man page in your browser, formatted, with links.

Combining the worlds of Unix, Windows and Mac OS you can even directly create MS Word documents from man pages:

do shell script "groff -man -Thtml /usr/share/man/man1/mdutil.1 | textutil -stdin -format html -convert doc -output ~/Desktop/mdutil.doc;open ~/Desktop/mdutil.doc"

Nevertheless this should be avoided at all cost :smiley:

i like such kind of avalanches!
i’m more than impressed.
my script is a bit clumpsy, like this:

set r1 to "/usr/share/man"
set r2 to "/usr/X11/share/man"
set r3 to "/usr/local/clamXav/share/man"
set r4 to "/usr/local/share/man"

set the_pt to {r1, r2, r3, r4}
set ch_pt to choose from list the_pt with prompt "Which path?" default items (item 1 of the_pt)
if ch_pt is false then return 0
set ch_pt to ch_pt as text

set search_str to the text returned of (display dialog "Enter man page" default answer "ssh")
set comp_cmd to ("man " & search_str as text)
set the_page to do shell script comp_cmd 
--filter simple twice characters
set to_write to my find_rep(the_page)

set inputFile to ((path to documents folder) as text) & "Man pages:Man " & search_str & ".txt"

	--open manual file
	do shell script "open " & quoted form of POSIX path of inputFile
on error
	--write manual to file
	open for access file the inputFile with write permission
	write to_write to file the inputFile --starting at eof
	close access file the inputFile
	--open manual file
	do shell script "open " & quoted form of POSIX path of inputFile
end try

on find_rep(the_page) --a bit useless
	set searchList to {"_", "-", "``", "''"}
	set replaceList to {"", "", "`", "'"}
	set T to the_page
	set tid to text item delimiters
	repeat with i from 1 to (count searchList)
		set text item delimiters to item i of searchList
		set T to text items of T
		set text item delimiters to item i of replaceList
		set T to T as string
	end repeat
	set text item delimiters to tid
	return T
end find_rep

a last question:its possible to create a list/summary of man pages given a certain path?