Saturday, October 31, 2020

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

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

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)


I could teach you to cook but I couldn't do anything if you do not have desire or commitment for it.

Offline

 

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

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

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

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)


I could teach you to cook but I couldn't do anything if you do not have desire or commitment for it.

Offline

 

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

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

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

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

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

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)


I could teach you to cook but I couldn't do anything if you do not have desire or commitment for it.

Offline

 

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

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

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

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.


I could teach you to cook but I couldn't do anything if you do not have desire or commitment for it.

Offline

 

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

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

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.


I could teach you to cook but I couldn't do anything if you do not have desire or commitment for it.

Offline

 

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

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

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.


I could teach you to cook but I couldn't do anything if you do not have desire or commitment for it.

Offline

 

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

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

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

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)


I could teach you to cook but I couldn't do anything if you do not have desire or commitment for it.

Offline

 

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

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

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

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. For reasons that are unclear to me, the script does not work properly without the delay command at its end.

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

Last edited by peavine (2020-02-29 08:17:06 am)


2018 Mac mini - macOS Catalina

Offline

 

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

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

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)


I could teach you to cook but I couldn't do anything if you do not have desire or commitment for it.

Offline

 

#17 2020-03-09 10:24:42 am

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

Re: preman(), bash function as AppleScript

The script included below is the same as that in Post 15 above except that it remembers the name of the previously-entered utility. It does this by creating a 1-line text file in the user's preferences folder.

Applescript:

-- Revised 2020.10.11

main()

on main()
   set settingFile to (path to preferences as text) & "Man Page Viewer.txt"
   set defaultAnswer to readSetting(settingFile)
   
   display dialog "Enter the name of a shell command:" with title "Man Page Viewer" default answer defaultAnswer
   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 .ps"
   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"} cancel button 1 default 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
   
   writeSetting(settingFile, utilityName)
   
   delay 0.5
end main

on readSetting(theFile)
   try
       return paragraph 1 of (read file theFile)
   on error
       return ""
   end try
end readSetting

on writeSetting(theFile, theText)
   try
       set openedFile to open for access (file theFile) with write permission
       set eof of openedFile to 0
       write theText to openedFile
       close access openedFile
   on error
       try
           close access openedFile
       end try
   end try
end writeSetting

Last edited by peavine (2020-10-11 06:56:13 pm)


2018 Mac mini - macOS Catalina

Offline

 

#18 2020-03-10 07:47:23 am

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

Re: preman(), bash function as AppleScript

I have a idea for you 'Peavine', first I was thinking why do you not store the last setting in plist format, but maybe a better idea would be to store a list of the last 20 items in a plist.
I have not yet starting to explore property format plist. But when I do I think I will update
my original script to include that that.

peavine wrote:

The script included below is the same as that in Post 15 above except that it remembers the name of the previously-entered utility. It does this by creating a 1-line text file in the user's preferences folder.



Already 1000 visitor, where is the price to me, Nigel?? wink


I could teach you to cook but I couldn't do anything if you do not have desire or commitment for it.

Offline

 

#19 2020-03-10 10:39:48 am

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

Re: preman(), bash function as AppleScript

It could be interesting to build a handler that store recent strings.

If you search on the topic you will find Shane, AppleScriptObjC code...

So to make it work I need a dialog that take input string but also a list...
the list change depend on my prevues input and store the history in plist format.

read, store and update... exactly what I want to learn smile

Last edited by Fredrik71 (2020-03-10 10:47:53 am)


I could teach you to cook but I couldn't do anything if you do not have desire or commitment for it.

Offline

 

#20 2020-03-10 06:03:49 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6473

Re: preman(), bash function as AppleScript

Fredrik71 wrote:

read, store and update



As long as your script is saved as an applet or .scptd file, you could use my PrefsStorageLib -- it makes it reasonably simple and convenient.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#21 2020-03-11 02:01:15 am

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

Re: preman(), bash function as AppleScript

Thanks, Shane I will do that and also study all your wonderful libs and build examples. smile

The code I saw here on 'mscscripter' from you was on this thread.
https://macscripter.net/viewtopic.php?p … 85#p193185

There you had made handlers to read, store and update in AppleScriptObjC.

Shane Stanley wrote:


As long as your script is saved as an applet or .scptd file, you could use my PrefsStorageLib -- it makes it reasonably simple and convenient.


I could teach you to cook but I couldn't do anything if you do not have desire or commitment for it.

Offline

 

#22 2020-03-11 03:46:30 am

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

Re: preman(), bash function as AppleScript

Here is something quick I did with your Library, Shane.
It store the last command in a plist in preference folder.

The way I see it...

display dialog return a string, this string update a array (list) so the recent string will be the first
in the list, every item in the list will be access from chose from list.
the dialog window will have manual input and also a list of previous input.

So I'm guessing for the moment I need
prepare storage for (path to me) default values {command_name1:"",command_name2:"",command_name3:""..... command_name10:""}

is this correct ??

Applescript:

use AppleScript version "2.5"
use scripting additions
use script "PrefsStorageLib" version "1.0.1"

on run
   prepare storage for (path to me) default values {command_name:""}
   set command_name to value for key "command_name"
   
   set manPage to (display dialog "Type the manpage you want:" default answer command_name)'s text returned
   
   set command_name to manPage
   assign value command_name to key "command_name"
   
   if manPage = "" then
       beep 1
   else
       set status to do shell script "man -t " & manPage & "; " & "if [ $? -eq 1 ]; then echo 1; else echo 0; fi"
       if status = "1" then
           beep 2
       else
           do shell script "man -t " & manPage & " | " & "open -f -a " & "Preview"
       end if
   end if
end run


I could teach you to cook but I couldn't do anything if you do not have desire or commitment for it.

Offline

 

#23 2020-03-11 04:49:24 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6473

Re: preman(), bash function as AppleScript

Fredrik71 wrote:

update a array (list) so the recent string will be the first
in the list, every item in the list will be access from chose from list.



I'd store the array under one key, and the default under another. But you might need to use something like Dialog Toolkit and use a text field and a drop-down menu, rather than choose from list, if you want it all in one dialog.

Last edited by Shane Stanley (2020-03-11 04:49:38 am)


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#24 2020-03-11 04:51:03 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6473

Re: preman(), bash function as AppleScript

Shane Stanley wrote:

As long as your script is saved as an applet or .scptd file



I've now updated it to support use in .scpt files, too, by use of a new for domain parameter.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)