Handy Hint for Man Pages

Thank you Nigel & Bruce.

Nigel: My dual-core G5 shipped yesterday (second time around). The first, ordered in late November, arrived with the side door bashed in (as if something sharp and heavy had penetrated the box). Apple snail mailed the required return shipping label from Cupertino, California during the Christmas rush so there was a long pause before I could return it and a pause before they sent a replacement; BUT: it’s on its way to replace a much goosed-up B&W G3/1100!

Bruce: having never seen such a “pointless error”, I’ll take your word for it.

/A

OK, I guess that’s not a great way to describe it.

To clarify, the repeat statement in question is used to validate user input.

A final form that checks to see if there is a saved copy of the man file before getting one and converting it.

on main()
	set theCommand to ""
	
	repeat while theCommand is ""
		display dialog "View man page for this command:" default answer theCommand
		set theCommand to text returned of result
	end repeat
	
	try
		set manFolderPath to (path to documents folder as Unicode text) & "Unix 'man' Pages (PDFs):"
		set manFile to manFolderPath & theCommand & "-ManPage.pdf"
		tell application "Finder"
			if exists manFile then
				open manFile
				return
			end if
		end tell
		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 theCommand & " | /usr/bin/pstopdf -i -o " & manFilePath & "; open -a /Applications/Preview.app " & manFilePath
	on error errorMsg number errorNum
		display alert "Error " & errorNum message errorMsg buttons "Cancel" default button 1
	end try
end main

main()
tell application "Script Editor" to activate -- useful if running this as a script from FastScripts; not necessary if running this as an application.

Thanks, Adam. That’s a noticeable improvement.

I hope the frustration over getting your new G5 is soon over. :slight_smile:

Preview is showing the man page in postscript – no conversion is taking place. Any ideas why that is?

Model: PowerBook G4
AppleScript: 1.9.3
Browser: Safari
Operating System: Mac OS X (10.3.9)

I’m afraid this is the problem, sd: Operating System: Mac OS X (10.3.9). It’s a Tiger only trick (as I indicated at the top of the thread).

Adam,

You’ve given me an idea. I liked your original idea, but hate viewing stuff in Preview. So before I knew it I was hip-deep in writing my first document-based AS Studio app that is a stand-alone man page viewer. I’ll be posting it to Scriptbuilders as soon as I get a creator code from Apple (probably tomorrow).

BTW: I found that piping the man page through “col -bx” and then displaying the result in a fixed-width font made for a nice read.

For those that haven’t tried it, AS Studio makes creating document-handling apps SINFULLY easy. I’m ashamed to admit that once you strip out empty lines and comments, the entire program is…23 lines. And with only that much code, you can write an app that can save the viewed man page and re-open the same document later. I spent more time doing the needed research into HOW to write the thing than I did in writing it.

Hey very nice.

I change the bit that does the display dialog

I used frontmost app to do this so it will bring the dialog to the front as I found the dialog pop up kept being hidden in the background.

on main()
	set theCommand to ""
	tell application "System Events"
		
		set target_app to item 1 of (get name of processes whose frontmost is true)
		
	end tell
	tell application target_app
		repeat while theCommand is ""
			display dialog "View man page for this command:" default answer theCommand
			set theCommand to text returned of result
		end repeat
		
	end tell
	try
		set manFolderPath to (path to documents folder as Unicode text) & "Unix 'man' Pages (PDFs):"
		set manFile to manFolderPath & theCommand & "-ManPage.pdf"
		tell application "Finder"
			if exists manFile then
				open manFile
				return
			end if
		end tell
		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 theCommand & " | /usr/bin/pstopdf -i -o " & manFilePath & "; open -a /Applications/Preview.app " & manFilePath
	on error errorMsg number errorNum
		display alert "Error " & errorNum message errorMsg buttons "Cancel" default button 1
	end try
	
	
end main

main()
--tell application "Script Editor" to activate -- useful if running this as a script from FastScripts; not necessary if running this as an application.

** Note for mod, Please move this to the help area if you feel it should be there. Thanks**

I wanted to have my PDFs In colour like Bwana Man http://www.bruji.com/bwana/index.html does.
So I edited the script to do it. There is most likely a better way but this is what I came up is in the short time I had.

It works but when I first run the script it fails.
Run it a second time looking for the same command it always works??

I have comment out the try statements to see where the bug is and it seems the convert fro rtf to pdf does not happen the first time round.
I ran some tests putting if exists on the html file in the temp folder before the convert to rtf and it finds it but still bugs out on the first run.

Any one got any ideas??

You will need Bwana Man for to use this.
I like Bwana Man and could quit easily a script to do the same thing. But I like the pdf’s

on main()
	set theCommand to ""
	set tmp to path to temporary items folder
	set temp to POSIX path of tmp
	tell application "System Events"
		
		set target_app to item 1 of (get name of processes whose frontmost is true)
		
	end tell
	tell application target_app
		repeat while theCommand is ""
			display dialog "View man page for this command:" default answer theCommand
			set theCommand to text returned of result
		end repeat
		
	end tell
	--try
	set manFolderPath to (path to documents folder as Unicode text) & "Unix 'man' Pages (PDFs):"
	set manFile to manFolderPath & theCommand & "-ManPage.pdf"
	tell application "Finder"
		if exists manFile then
			open manFile
			return
		end if
	end tell
	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 "open man:" & theCommand & " |textutil -convert rtf  " & temp & theCommand & ".html"
	
	my processFile(theCommand, manFilePath, temp)
	do shell script "open -a /Applications/Preview.app " & manFilePath
	--on error errorMsg number errorNum
	--	display alert "Error " & errorNum message errorMsg buttons "Cancel" default button 1
	--end try
	
	
end main

main()
--tell application "Script Editor" to activate -- useful if running this as a script from FastScripts; not necessary if running this as an application.


-- This is from part of the Apple sample code "convert to pdf script"
on processFile(theCommand, manFilePath, temp)
	
	--try
	
	set terminalCommand to ""
	set convertCommand to "/System/Library/Printers/Libraries/./convert "
	set theCommandPath to temp & theCommand & ".rtf"
	set newFileName to theCommand & ".pdf"
	set terminalCommand to convertCommand & "-f " & theCommandPath & " -o " & manFilePath & " -j application/pdf"
	do shell script terminalCommand
	
	--	end try
end processFile

I came back to this today and Had a little debug session :confused:

found that the issue I had above was I needed to break up one line ( do shell script “open man:” & theCommand & " |textutil -convert rtf " & temp & theCommand & “.html” )
and put a 0.5 seconds delay in.
The change looks like this.

on _convert(theCommand, tempFolder)
do shell script “open man:” & theCommand & " |> " & tempFolder & theCommand & “.html”
do shell script “sleep 0.5”
do shell script "textutil -convert rtf " & tempFolder & theCommand & “.html”

end _convert

on main()
	set theCommand to ""
	tell application "System Events"
		
		set target_app to item 1 of (get name of processes whose frontmost is true)
		
	end tell
	tell application target_app
		repeat while theCommand is ""
			display dialog "View man page for this command:" default answer theCommand
			set theCommand to text returned of result
		end repeat
		
	end tell
	try
		set manFolderPath to (path to documents folder as Unicode text) & "Unix 'man' Pages (PDFs):"
		set tempFolder to POSIX path of (path to temporary items folder) as string
		set manFile to manFolderPath & theCommand & "-ManPage.pdf"
		tell application "Finder"
			if exists manFile then
				open manFile
				return
			end if
		end tell
		do shell script ("mkdir -p " & quoted form of POSIX path of manFolderPath)
		set manFilePath to quoted form of POSIX path of manFile
		
		my _convert(theCommand, tempFolder)
		
		my processFile(theCommand, manFilePath, tempFolder)
		do shell script "open -a /Applications/Preview.app " & manFilePath
	on error errorMsg number errorNum
		display alert "Error " & errorNum message errorMsg buttons "Cancel" default button 1
	end try
	
	
end main

main()

on _convert(theCommand, tempFolder)
	do shell script "open man:" & theCommand & " |> " & tempFolder & theCommand & ".html"
	do shell script "sleep 0.5"
	do shell script "textutil -convert rtf  " & tempFolder & theCommand & ".html"
	
end _convert

-- This is from part of the Apple sample code "convert to pdf script"
on processFile(theCommand, manFilePath, tempFolder)
	--try
	
	
	set terminalCommand to ""
	set convertCommand to "/System/Library/Printers/Libraries/./convert "
	set theCommandPath to tempFolder & theCommand & ".rtf"
	set newFileName to theCommand & ".pdf"
	set terminalCommand to convertCommand & "-f " & theCommandPath & " -o " & manFilePath & " -j application/pdf"
	
	
	do shell script terminalCommand
	
	--end try
end processFile

Hi guys, I know this is long after the initial thread, but this post sort of covers a problem of mine.

I want to use Preview to batch convert .eps files into .pdf files. Preview is fast, crops to the .eps bounding box and makes very tight pdf code (16k vs 1.1Mb from Illustrator CS2).

The only problem is I cann’t find a dictionary for Preview and the Preview conversion actions in Automator only convert to bitmap formats.

Could you please tell me how to create a workflow for Preview to convert my .eps to pdfs?

Thanks. s:-)

Model: iMac 17" PPC 2.0Ghz
AppleScript: 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Have a look at post #4. and see if that gives you the “tight” conversions you want.
I do not think you will have much luck trying to get preview to work for you, as you have discovered is not scriptable and does not have a library.

EDIT here is an example.

tell application "Finder"
	set target_path to ((path to desktop folder as Unicode text) as alias)
	set source_item to (choose file)
	set source_itemP to POSIX path of source_item
	set file_name to displayed name of source_item -- get displayed name
	set oldDelims to AppleScript's text item delimiters -- get normal dilm
	set AppleScript's text item delimiters to {"."} --sets new dilm
	set file_name to text item 1 of file_name -- get thye name of file without extension
	set AppleScript's text item delimiters to oldDelims -- resets dilm
	set target_pathP to POSIX path of target_path & file_name & ".pdf" as string
end tell
do shell script (" /usr/bin/pstopdf " & quoted form of source_itemP & " -o " & quoted form of target_pathP)

Mark Hunte,

Thanks for the quick reply.

I’m not a proficient scripter, how exactly would I adapt the script at Post #4?

I did an edit to my post. Did you see it?

Pardon I wasn’t sure that was meant for me.

I read it but can’t follow what it is doing. Probably because of my lack of familiarity with scripting and total ignorance of shell scripts.

I compiled it anyway and tried it.

It asks me to find an individual file, which I did and then it doesn’t ask me for a destination. It just seems to do nothing and closes. What am I missing?

Sorry to trouble you.

btw If I could automate this, I have found a manual way to do the job:

Open dialog in Preview
Navigate to folder full of eps files
Select all in folder
All files open and are converted to pdfs automatically
Close all open windows [cmd-opt-W]
save (twice) for each dialog that comes up [waiting for each dialog to appear - being OSX it doesn’t let me key ahead]
…till no windows are open

It drops all pdf files back into the originating folder

Next step would working out how to get it to step through a series of subfolders doing the same thing.

Thanks for your time and trouble.

The example should have put a pdf file on you desktop named the same as the file but as a pdf file and with a pdf extension.

putting it into an Automator script should not be that hard.

But there is no point me trying it for you unless the file the script creates is what you want.
have a look on the desktop for the file.

Ah thanks very much, I found it (I missed the bit about the desktop) and yes it is a perfectly OK pdf file.

Could you walk me through how you got it to do that please? I gather all the first bit is just getting the location and name of the file.

Is this the conversion bit?:

Wow it is terse!

;))

Now how do i get this into my automator workflow? I built one with everything including custom icon preview. This is the missing bit.

Thanks again. This is brilliant!

Actually It only took 5mins to write the automator action.
I will post it on one of my pages. As I do not think I can post here. But will walk you through here also.

Ok here is how to build the automator action.

In Automator.

From the FINDER actions.

choose the following action and set it as below -Ask for Finder items-

type : Folders
Start at : where ever you want
Prompt : Choose a Folder:

allow multiple Selections (un ticked)

From the FINDER actions.
choose the following action and set it as below -Get Folder Contents-
Repeat for each Subfolder found(ticked)


from the AUTOMATOR actions.

choose the following action and Run Applescript Replace the script in the applescript window with this code.

on run {input, parameters}
	tell application "Finder"
		repeat with i from 1 to number of items of input
			set source_item to item i of input
			
			set source_itemP to POSIX path of source_item
			do shell script (" /usr/bin/pstopdf " & quoted form of source_itemP)
		end repeat
		
	end tell
end run

*** EDIT ****
You are correct in regards to

		do shell script (" /usr/bin/pstopdf " & quoted form of source_itemP & " -o " & quoted form of target_pathP)

being the conversion line.
And Actually I just change the above script since you want the files to be created in the original folder.
The last part of the line which name the pdf file and path is not needed.

This is what its actually doing.
do shell script " /usr/bin/pstopdf ‘/Users/username/Desktop/Photo-5.eps’ "

/usr/bin/pstopdf is shell command that does the conversion.
‘/Users/username/Desktop/Photo-5.eps’ is the unix path to the source file .
the pdf if is created in the same folder as the original if no destination of name is given.

Thanks I’d appreciate a link.

I don’t know if you can fix Apple’s automator actions. The one for Preview’s file conversions should ideally just be extended to do the ps to pdf conversion, then it would be an all in one for graphic designers.

:slight_smile:

Good, you edited you post while I was on the phone.

I’ll set on creating and testing the Automator workflow now using your instructions.

Thanks a million.

Would it be possible to simply insert the pstopdf in the Preview file conversion drop down menu? That would be by far the tidiest solution.

The actual name of the action is : “Change Type of Images”