Saturday, October 31, 2020

#1 2005-12-30 11:37:48 am

Adam Bell
Administrator
From:: Nova Scotia, Canada
Registered: 2005-10-04
Posts: 4666

Handy Hint for Man Pages

Today's MacOSXhint makes a handy one-liner AppleScript as well for getting shell script man pages as easily read PDF's in Preview.

Applescript:

do shell script "man -t " & (text returned of (display dialog "Enter Name of Shell Function Man Page" default answer "grep" buttons {"Go Get It"} default button 1)) & " | open -f -a /Applications/Preview.app"

TIGER ONLY - For those running Panther, Bwana is recommended - it shows man pages in Safari.


Mac mini running 10.14.6, 2011 27" iMac as display.

Offline

 

#2 2005-12-30 12:25:16 pm

Bruce Phillips
Administrator
Registered: 2004-07-16
Posts: 2649

Re: Handy Hint for Man Pages

See also: Help with User Input Text fields

Applescript:

property theCommand : ""

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
   do shell script "/usr/bin/man -t " & quoted form of theCommand & " | open -f -a /Applications/Preview.app"
on error errorMsg number errorNum
   display alert "Error " & errorNum message errorMsg buttons "Cancel" default button 1
end try

Offline

 

#3 2005-12-30 12:49:25 pm

Adam Bell
Administrator
From:: Nova Scotia, Canada
Registered: 2005-10-04
Posts: 4666

Re: Handy Hint for Man Pages

Good stuff, Bruce. How does the Postscript to PDF conversion happen - does Preview do it? Is there a way to get the man page to text?


Mac mini running 10.14.6, 2011 27" iMac as display.

Offline

 

#4 2005-12-30 01:00:18 pm

Bruce Phillips
Administrator
Registered: 2004-07-16
Posts: 2649

Re: Handy Hint for Man Pages

I saw your post over at MacOSXHints…

To answer the question, Preview does the conversion. However, you could also do it this way:

Applescript:

property theCommand : ""

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
   get quoted form of ("/tmp/" & theCommand & "-ManPage.pdf")
   do shell script "/usr/bin/man -t " & quoted form of theCommand & " | /usr/bin/pstopdf -i -o " & result & "; open -a /Applications/Preview.app " & result
on error errorMsg number errorNum
   display alert "Error " & errorNum message errorMsg buttons "Cancel" default button 1
end try

One nice thing about this script is that Preview.app won't ask if you want to save the file.

Adam Bell wrote:

Is there a way to get the man page to text?


Hmm… redirection would get you started:

“man echo > /tmp/test.txt” wrote:

NNAAMMEE
     eecchhoo -- write arguments to the standard output


Note that the double-lettered words are those that would appear bold when viewed in the terminal…

[Another edit] It seems there are extra characters betweens those letters (their not visible in BBEdit, or while editing this post).

[Yet another edit] It seems that character is ASCII character 8.

Offline

 

#5 2005-12-30 09:48:59 pm

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5300

Re: Handy Hint for Man Pages

That's excellent! Thanks, Adam and Bruce. It's taken some of the slog out of trying to understand those appalling 'man' descriptions!  smile

For my own use, I've adapted Bruce's later script to save a permanent copy of the PDF in my Documents folder, for perusal at leisure. I've also made 'theCommand' (and all other variables) local, since as a property, it dooms the script to open the same manual page forever....  smile

Applescript:

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 At Ease documents folder as Unicode text) & "Unix 'man' Pages (PDFs):"
       do shell script ("mkdir -p " & quoted form of POSIX path of manFolderPath)
       set manFilePath to quoted form of POSIX path of (manFolderPath & theCommand & "-ManPage.pdf")
       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()


NG

Offline

 

#6 2005-12-31 11:15:07 am

Adam Bell
Administrator
From:: Nova Scotia, Canada
Registered: 2005-10-04
Posts: 4666

Re: Handy Hint for Man Pages

Great stuff. I've given it a hot key. I'm going to play with it a bit too - shouldn't it check whether the directory exists and whether a copy of that man page already exists in manFilePath and show me that rather than fetching and converting again?

But the inevitable question (from me): the script includes this part

Applescript:

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

and while I understand what it does, it hasn't occurred to me why it's done.

I've never seen this before: "At Ease documents folder". Since I get the same answer without "At Ease", why is it there?


Mac mini running 10.14.6, 2011 27" iMac as display.

Offline

 

#7 2005-12-31 01:30:54 pm

Bruce Phillips
Administrator
Registered: 2004-07-16
Posts: 2649

Re: Handy Hint for Man Pages

Adam Bell wrote:

while I understand what it does, it hasn't occurred to me why it's done.


If theCommand is "", then the script won't do much. However, in some scripts, having no user input could result in some pointless errors…

Offline

 

#8 2005-12-31 07:34:11 pm

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5300

Re: Handy Hint for Man Pages

Adam Bell wrote:

Great stuff. I've given it a hot key. I'm going to play with it a bit too - shouldn't it check whether the directory exists and whether a copy of that man page already exists in manFilePath and show me that rather than fetching and converting again?


Could do. I might do something like that myself. It's just that my G5 is so fast that it doesn't make much difference.  big_smile

I've never seen this before: "At Ease documents folder". Since I get the same answer without "At Ease", why is it there?


Sorry, Adam. I'm always worrying people by doing that. The reasons for the 'At Ease' part are lost in the history of AppleScript, before my time. I think there was once an Apple technology of that name. Until about Panther or Tiger, the offical 'path to' parameters for the Applications and Documents folders were respectively 'At Ease applications folder' and 'At Ease documents folder'. This wasn't widely known because there was some technical reason why these terms couldn't be shown in the Standard Additions dictionary (!), and in any case, most people preferred the shorter string hacks 'path to "apps"' and 'path to "docs"'.

Nowadays the 'At Ease' part isn't necessary, but it still works. I use it partly out of habit and party because my other machine still runs Jaguar. And I like my scripts to work across as wide a range of systems as possible.  This one doesn't work in Jaguar, but the habit's still there. smile

It's 2006 here. Happy New Year to you when it arrives.  smile

Last edited by Nigel Garvey (2005-12-31 07:36:20 pm)


NG

Offline

 

#9 2006-01-01 10:12:14 am

Adam Bell
Administrator
From:: Nova Scotia, Canada
Registered: 2005-10-04
Posts: 4666

Re: 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


Mac mini running 10.14.6, 2011 27" iMac as display.

Offline

 

#10 2006-01-01 10:42:55 am

Bruce Phillips
Administrator
Registered: 2004-07-16
Posts: 2649

Re: Handy Hint for Man Pages

Adam Bell wrote:

having never seen such a "pointless error", I'll take your word for it.


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.

Offline

 

#11 2006-01-02 02:31:37 pm

Adam Bell
Administrator
From:: Nova Scotia, Canada
Registered: 2005-10-04
Posts: 4666

Re: Handy Hint for Man Pages

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

Applescript:

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.


Mac mini running 10.14.6, 2011 27" iMac as display.


Filed under: Script, Finder

Offline

 

#12 2006-01-03 05:08:26 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5300

Re: Handy Hint for Man Pages

Thanks, Adam. That's a noticeable improvement.

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


NG

Offline

 

#13 2006-02-03 12:01:51 pm

sdrenker
Member
Registered: 2005-06-21
Posts: 22

Re: Handy Hint for Man Pages

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)

Offline

 

#14 2006-05-01 11:15:22 am

Adam Bell
Administrator
From:: Nova Scotia, Canada
Registered: 2005-10-04
Posts: 4666

Re: Handy Hint for Man Pages

sdrenker wrote:

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).


Mac mini running 10.14.6, 2011 27" iMac as display.

Offline

 

#15 2006-05-18 01:27:42 am

Kevin Bradley
Administrator
From:: Independence, MO
Registered: 2006-03-13
Posts: 548
Website

Re: Handy Hint for Man Pages

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.


Nitewing '98
--
I distrust morning people, largely because I suspect them of getting together early one day while the rest of us were asleep and setting up the rules of civilization.

Offline

 

#16 2006-05-24 06:41:26 pm

mark hunte
Member
From:: Hello, London Calling
Registered: 2006-03-20
Posts: 1030
Website

Re: Handy Hint for Man Pages

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.

Applescript:

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.


Tap,tap,tap , hello , testing 1..2,  1...2,
http://www.markosx.com/thecocoaquest/


Filed under: Script, Finder, System

Offline

 

#17 2006-05-26 01:18:48 am

mark hunte
Member
From:: Hello, London Calling
Registered: 2006-03-20
Posts: 1030
Website

Re: Handy Hint for Man Pages

** 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



Applescript:

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

Last edited by mark hunte (2006-05-26 01:20:00 am)


Tap,tap,tap , hello , testing 1..2,  1...2,
http://www.markosx.com/thecocoaquest/


Filed under: Script, Finder, System

Offline

 

#18 2006-06-27 01:07:38 pm

mark hunte
Member
From:: Hello, London Calling
Registered: 2006-03-20
Posts: 1030
Website

Re: Handy Hint for Man Pages

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

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


Applescript:

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


Tap,tap,tap , hello , testing 1..2,  1...2,
http://www.markosx.com/thecocoaquest/


Filed under: Finder, System

Offline

 

#19 2007-05-20 05:13:52 am

gastroboy
Member
Registered: 2007-05-20
Posts: 8

Re: Handy Hint for Man Pages

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)

Offline

 

#20 2007-05-20 06:01:40 am

mark hunte
Member
From:: Hello, London Calling
Registered: 2006-03-20
Posts: 1030
Website

Re: Handy Hint for Man Pages

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.

Applescript:

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)

Last edited by mark hunte (2007-05-20 06:29:51 am)


Tap,tap,tap , hello , testing 1..2,  1...2,
http://www.markosx.com/thecocoaquest/


Filed under: Finder

Offline

 

#21 2007-05-20 06:47:43 am

gastroboy
Member
Registered: 2007-05-20
Posts: 8

Re: Handy Hint for Man Pages

Mark Hunte,

Thanks for the quick reply.

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

Offline

 

#22 2007-05-20 11:47:01 am

mark hunte
Member
From:: Hello, London Calling
Registered: 2006-03-20
Posts: 1030
Website

Re: Handy Hint for Man Pages

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


Tap,tap,tap , hello , testing 1..2,  1...2,
http://www.markosx.com/thecocoaquest/

Offline

 

#23 2007-05-20 05:22:30 pm

gastroboy
Member
Registered: 2007-05-20
Posts: 8

Re: Handy Hint for Man Pages

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.

Offline

 

#24 2007-05-20 05:43:31 pm

mark hunte
Member
From:: Hello, London Calling
Registered: 2006-03-20
Posts: 1030
Website

Re: Handy Hint for Man Pages

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.


Tap,tap,tap , hello , testing 1..2,  1...2,
http://www.markosx.com/thecocoaquest/

Offline

 

#25 2007-05-20 05:54:02 pm

gastroboy
Member
Registered: 2007-05-20
Posts: 8

Re: Handy Hint for Man Pages

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?:

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


Wow it is terse!

wink)

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!

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)