Sunday, February 23, 2020

#1 2020-01-22 02:01:29 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 84

preman(), bash function as AppleScript

AppleScript that was inspired from bash function preman()

Bash function from .profile

function preman() {
   man -t $@ | open -f -a "Preview"
}

In terminal we type: preman systemsetup

Here is my AppleScript version.

Applescript:

-- Bash function preman() a.k.a (preview manpage)

on run
   set manPage to (display dialog "Type the manpage you want:" default answer "systemsetup")'s text returned
   if manPage = "" then
       beep 1
   else
       do shell script "man -t " & manPage & " | " & "open -f -a " & "Preview"
   end if
end run

Last edited by Fredrik71 (2020-02-07 02:51:10 am)

Offline

 

#2 2020-01-22 02:53:42 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 351

Re: preman(), bash function as AppleScript

Fredrik71. Thanks for the script, which works well. Reading--and more importantly searching--a man page can be frustrating and this is way better.

Last edited by peavine (2020-01-22 02:58:44 pm)


2018 Mac mini - macOS Catalina

Offline

 

#3 2020-01-22 10:24:55 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 84

Re: preman(), bash function as AppleScript

Happy you like it...

Other way for your search on manpages could be...

Make a script to collect every manpage file on your computer, to a directory...
Next time you do a search you only search in that directory, so much faster...

It take me less 5 sec to search throw 10.000 files smile)

Offline

 

#4 2020-01-23 03:19:38 am

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

Re: preman(), bash function as AppleScript

I'm still using a version of the script posted in this very forum just over fourteen years ago. (https://macscripter.net/viewtopic.php?id=15444, post #5.)  smile

I've found that the down side of saving PDFs is that they don't get updated. I've sometimes laboured for years not realising that the executable described now had new facilities and a new 'man' file!  roll


NG

Offline

 

#5 2020-01-23 08:15:11 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 351

Re: preman(), bash function as AppleScript

Thanks Nigel. I tried your script on Catalina and it threw an error due to the path to the Preview app. For other Catalina users, I've included that change below.

I viewed a sample man page from Fredrik71's script and from your script and they look the same. So, the only significant difference is whether the man page is saved on disk. It's good to have choices 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 /System/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()


2018 Mac mini - macOS Catalina

Offline

 

#6 2020-01-23 10:20:27 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 351

Re: preman(), bash function as AppleScript

I noticed one small issue with the OP's script. If the man page does not exist, an error should be reported, but instead Preview loads but without a document. I tried the following but it did not report an error:

Applescript:

try
   do shell script "man -t xxx | " & "open -f -a " & "Preview"
on error
   display dialog "A man page does not exist."
end try

However, the following does report an error:

Applescript:

try
   do shell script "man -t xxx"
on error
   display dialog "A man page does not exist."
end try

Is there some way to get the first script above to report an error? I assume the answer is redirecting the man-command error but my knowledge of that is pretty nonexistent. Thanks.

Last edited by peavine (2020-01-23 10:37:44 am)


2018 Mac mini - macOS Catalina

Offline

 

#7 2020-01-23 03:48:44 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 84

Re: preman(), bash function as AppleScript

Hi Peavine

You could do something like this..

Change the beep to your like... it use if else to check if the manpage exist... if not = 0
If the result of variable status = 0 we beep 2
if the result of variable status = 1 we beep 3

Applescript:

on run
   set manPage to (display dialog "Type the manpage you want:" default answer "systemsetup")'s text returned
   if manPage = "" then
       beep 1
   else
       set status to do shell script "man -t " & manPage & "; " & "if [ $? -eq 0 ]; then echo 1; else echo 0; fi"
       if status = "0" then
           beep 2
       else
           -- do shell script "man -t " & manPage & " | " & "open -f -a " & "Preview"
           beep 3
       end if
   end if
end run

Last edited by Fredrik71 (2020-01-23 05:50:02 pm)

Offline

 

#8 2020-01-23 04:11:47 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 351

Re: preman(), bash function as AppleScript

Thanks Fredrik71. I tested your script and it correctly returns an error if a man page does not exist. My only concern is that it runs a shell command to see if the man page exists and then a second time to actually create and open the man page. Perhaps that's the best way to do it, though.

I tested a different approach which created a temp file and then opened the temp file in Preview. This only calls the shell once, but it's a bit slow and I'm not sure if the temp files are deleted on reboot.

Applescript:

set tempFile to ((path to temporary items) as text) & "man page " & (random number from 1 to 999999) & ".txt"
set tempPosixFile to POSIX path of tempFile

display dialog "Enter the name of a utility:" with title "Man Page Viewer" default answer ""
set utilityName to text returned of result
if utilityName = "" then error number -128

try
   do shell script "man -t " & utilityName & " > " & quoted form of tempPosixFile
on error
   display dialog "A man page is not availble for " & quote & utilityName & quote buttons {"OK"} cancel button 1 default button 1 with title "Man Page Viewer" with icon stop
end try

tell application "Preview"
   activate
   open file tempFile
end tell

Last edited by peavine (2020-01-24 08:38:05 am)


2018 Mac mini - macOS Catalina

Offline

 

#9 2020-01-23 06:38:51 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 84

Re: preman(), bash function as AppleScript

There are many ways to do it...

I update my script it was type error this one is wrong [ $? -eg 0 ] the correct one is this [ $? -eq 0 ]

To call man pages twice... is not a big deal if you ask me.
The first one we do a test case to return true or false.

That said...

You could maybe copy the status variable and use that to call the function with if else to do what you want. That is... execute man page only once. smile)

The question you should ask yourself

How could AppleScript know if you type wrong name for man page. ??

1. The command man in shell is not AppleScript
2. If the shell return a error... it could be difficult to know.
3. do shell script is a way to extend AppleScript but it will never be Apple Events. smile)

The bash shell could give you a true or false statement, from the command you execute.
This statement you could use to decide what you want to do with AppleScript.

That is how I see it.

What I know about stdout standard output in shell they copy files to /tmp directory or $TMPDIR
Everything will be deleted after a reboot.. so don't worry...

So many man pages do you not read or do you wink.

Offline

 

#10 2020-01-23 06:53:08 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 84

Re: preman(), bash function as AppleScript

If you setup variable to store your tmpfile you also know the name and path.

You could include sleep 1 or higher number so Preview.app have time to read the file.

Before you use the command rm -f your_temp_file_variable

I do exactly this when I use textutil command.

Offline

 

#11 2020-01-23 07:17:40 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 84

Re: preman(), bash function as AppleScript

If you do it like this... you will still have 2 execute of command man

set manual_name to "Whatever"

man -t manual_name; if [ $? -eq 0 ]; then (man -t manual_name | open -f -a "Preview"); else echo No manual entry for manual_name; fi

if you ask me...

I like more beep or display notification and to have display dialog with buttons to click when something is wrong.

To be honest I do not have much experience with error message with AppleScript.

Offline

 

#12 2020-01-23 07:20:33 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 351

Re: preman(), bash function as AppleScript

Fredrik71. Thanks for the comments and suggestions. I typically have a lot of free time and need a new project, so I'll spend some time researching the alternatives.


2018 Mac mini - macOS Catalina

Offline

 

#13 2020-01-24 04:21:16 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 84

Re: preman(), bash function as AppleScript

In Script Editor if you press ctrl + click you will find 'error handlers'
Get the one that say: message and return

Maybe you like that better...

I like to think like this...

if a error handler use 1 = false (error) 0 = true (correct)

Empty string = false, my example beep 1
Wrong string = false, my example beep 2
correct string = true, execute do shell script..

Applescript:

try
   do shell script "man systemsetup1"
on error the error_message number the error_number
   set the error_text to "Error: " & the error_number & ". " & the error_message
   display dialog the error_text buttons {"OK"} default button 1
   return the error_text
end try

Last edited by Fredrik71 (2020-01-24 04:28:45 am)

Offline

 

#14 2020-01-24 08:54:40 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 351

Re: preman(), bash function as AppleScript

Fredrik71. I ran the following command in Script Geek and it took a tenth of a second to execute. Not a deal breaker, but that's a fair amount for miscellaneous error checking.

do shell script "man systemsetup"



An alternative for me is to use the basic approach in Post 1 and then to exit Preview if the man page does not exist. A bit kludgey but it does work reasonably well and it seems the fastest alternative. My script in Post 8 is almost as fast, though. And, as a practical matter, your script in Post 7 seems about as fast.

Applescript:

display dialog "Enter the name of the utility:" with title "Man Page Viewer" default answer ""
set utilityName to text returned of result
if utilityName = "" then error number -128

do shell script "/usr/bin/man -t " & utilityName & " | open -f -a /System/Applications/Preview.app"

tell application "Preview"
   if (count documents) = 0 then quit
end tell

This script works in Catalina; in earlier versions of macOS the paths may have to be changed or just deleted.

BTW, both the above script and that in Post 8 create temporary files but they are deleted on reboot.

Last edited by peavine (2020-01-24 09:25:39 am)


2018 Mac mini - macOS Catalina

Offline

 

#15 2020-01-24 09:34:06 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 351

Re: preman(), bash function as AppleScript

In the end I adopted an approach suggested by the OP. It involves several shell calls, but it just seems to work best that way.

Applescript:


display dialog "Enter the name of a utility:" with title "Man Page Viewer" default answer ""
set utilityName to text returned of result
if utilityName = "" then error number -128

set manFile to ((path to temporary items) as text) & utilityName & " man page .txt"
set manPosixFile to POSIX path of manFile

try
   do shell script "man -w " & utilityName
on error
   display dialog "A man page is not availble for " & quote & utilityName & quote buttons {"OK"} default button 1 cancel button 1 with title "Man Page Viewer" with icon stop
end try

try
   alias manFile
on error
   do shell script "man -t " & utilityName & " > " & quoted form of manPosixFile
end try

do shell script "open -a Preview " & quoted form of manPosixFile

delay 0.5 # the script may not work correctly without this delay

Last edited by peavine (2020-02-20 05:18:40 pm)


2018 Mac mini - macOS Catalina

Offline

 

#16 2020-01-25 11:36:21 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 84

Re: preman(), bash function as AppleScript

Nice Peavine

It looks like you find a good solution that work for you.

I also checked 2 days ago if man command had something I to check if the manual page exist
without execute the command man... But I never did anything about that idea.

The great thing about sharing is to feed someone's mind, so they could create something
that are better and the idea was in first place. Problem for someone could be someone else's
force to find better solutions, to make better ideas. Why would we change something
that do work doesn't mean it's good. And for someone who believe it doesn't matter have never
create anything in the first place.

The -t option do parsing to groff with -Tps option.

If you use that knowledge or if you like the output of the manpage in Preview.

You could use that on other text files.... to pipe to groff with option -Tps before open in Preview.

ex.
cat my_document.txt |  groff -Tps | open -f -a "Preview"

Last edited by Fredrik71 (2020-01-25 11:46:05 am)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)