Get command line output on console

Given a command line command -- could be anything, e.g. 'ls -l'. I would somehow like to run something from the Docker that would execute this command and display it in an alert dialog on the console.


How do I get started on this?

Mac mini Unibody no ODD

Posted on Dec 12, 2018 2:56 PM

Reply
23 replies

Dec 16, 2018 9:52 AM in response to Barney-15E

After many hours of searching and testing AppleScript, I think I've go this working. I created the following AppleScript:


set theDialogText to do shell script "/usr/local/sbin/apcaccess"

display dialog theDialogText with title "UPS Battery Status" buttons {"OK"} with icon (POSIX path of (path to home folder)) & "/myApps/myIcons/battery-power-icon.icns" as POSIX file


This shows the shell output without wrapping the text. To get it to be something the user can click on, I have to export it to an application.


Seems to work! Comments?

Dec 12, 2018 8:20 PM in response to markfoley

Simplest thing would likely be to create an Automator app or and AppleScript app to run the command and display the output in a dialog. You can run shell scripts in AppleScript, then send the output to a dialog window.

Automator can also run a shell script, but I don't think it has any way to display a dialog except maybe using the Ask for Confirmation.

It could be built inside an Automator App like this:


I'm not sure we are talking about the same Console.

The Console app displays the log files. Logs are created by sending messages to the logging system. There is no "console" short of the Terminal (or another shell emulator). Are you talking about the screen, display, monitor?

Dec 12, 2018 3:28 PM in response to Barney-15E

If you are just wanting to send your commands to a log file, you can do that, but I don't know of a way to configure the shell to automatically do that. Some info here: https://serverfault.com/questions/103501/how-can-i-fully-log-all-bash-scripts-actions


This is for Ubuntu, but may give you some hints on what to look for (and it may work on macOS): https://askubuntu.com/questions/161935/how-do-i-log-all-input-and-output-in-a-terminal-session

Dec 12, 2018 7:31 PM in response to Barney-15E

No, I'm not wanting to send messages to a log file. I want to see the output on the console. I used the example of 'ls -l' to try to keep my question simple, but perhaps that backfired. Specifically, I have a command 'apcaccess' that lists information about the state of the battery backup. Run from a terminal window, it displays information like:


APC : 001,038,0981

DATE : 2018-12-12 22:27:30 -0500

HOSTNAME : mpress.hprs.local

VERSION : 3.14.14 (31 May 2016) darwin

UPSNAME : MIKEUPS

CABLE : USB Cable

DRIVER : USB UPS Driver

UPSMODE : Stand Alone

STARTTIME: 2018-11-12 14:03:09 -0500

MODEL : Back-UPS ES 750

STATUS : ONLINE


What I want to do is create an icon in the Docker the user can click on and have it run that program and send the output to an alert dialog on the console so the user can see it, then close the dialog.

Dec 13, 2018 3:57 PM in response to Barney-15E

I'm very new to Mac and am trying to integrate a few Mac Minis into our office, so my argot may be off. By "console" I mean the user's GUI desktop/Screen/Monitor -- whatever that's called in the Mac-world. In Linux/Windows it's referred to as the Desktop. Perhaps the same for Mac. Doing 'who' in a terminal session shows GUI users with device 'console'. That's why I'm using that term.


Your suggested Automator script does seem to work for what I want. Thanks, I doubt I'd have figured that out on my own. I've saved that to my Documents folder. Now I'd like to change the icon. I've followed the instruction in https://www.sethvargo.com/replace-icons-osx/ which basically entails copying the icon (.icns) to $HOME/Documents/testAutomator.app/Content/Resources (Documents/testAutomator.app is where I saved the app), and I edited the Info.plist file to make the change:


<key>CFBundleIconFile</key>

<string>battery-power-icon</string>


but that doesn't change the icon. I've logged out and back in. Nada. Suggestions?


Dec 13, 2018 4:15 PM in response to markfoley

I would just Get Info on the App and paste a picture into the Icon well. It doesn't change the underlying icon, but puts a custom icon on top of it. Not sure how that would present when run.

For the Battery icon, just set the Finder to display the icon very large and take a screenshot. Cmd-ctrl-shift-4 will give you a selection marquee. Drag around the icon image to copy it to the clipboard. Paste into the Get Info icon well.


Otherwise, I don't know how to make it work.

I use a program called Platypus to bundle AppleScripts and Shell scripts into Apps. It allows you to set a custom icon.


You could just use an AppleScript for the entire thing, and use Platypus to "App"-itize it with your icon. The Full AppleScript is just:

set apcInfo to do shell script "apcaccess"

display dialog apcInfo as text buttons {"Ok"} default button "Ok"


Oops, I didn't see that BDAqua already suggested pasting an image on the icon.

Dec 13, 2018 6:08 PM in response to BDAqua

Ah ah! I was close. I tried dragging/pasting it to the larger icon image at the bottom of the Info dialog. So, after trying what you said, it just put a generic image of ICNS in there, not my actual .icns file. Perhaps this is because I used Preview to convert a .png to .icns? Next I tried copy/pasting a .icns from /System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/, but it wouldn't copy (permissions?). Then I tried manually copying that icon from the /System/.../Resources directory to $HOME/Documents/testAutomator.app/Contents/Resources, naming it as the same name as my original .icns file, but no go. I did log out and back in. In case that made a difference. If I use Finder to navigate to my .app Content/Resources folder, it does show the .icns I copied from the system folder, but it doesn't show in my .app. Advice?


With respect to .apps generally, I saved this to my Documents folder because that's what the default had. Is there a "standard" place to save apps? Documents doesn't really seem like the best place.


Dec 13, 2018 6:51 PM in response to markfoley

You can create an Applications folder in your Home. That's what I do for things I only use.

Otherwise, you can put your App in the Applications folder for every logged in user to use.


You can't paste an icon file into the icon well as it will just use the icon of the icon file which is a generic system icon. You have to paste a picture into the icon well in Get Info.

Dec 13, 2018 10:39 PM in response to Barney-15E

Barney-15E: "You can't paste an icon file into the icon well as it will just use the icon of the icon file which is a generic system icon. You have to paste a picture into the icon well in Get Info."


Again ... Mac newbie here! Please expand. What is an "icon well"? There is a miniature icon at the top of the Get Info dialog with the appname and size. There is a larger icon at the bottom. Is the "icon well" one of these? When you say "You have to paste a picture into the icon well ...", what do you mean? A .png file, not a .icns file?


Dec 14, 2018 10:41 AM in response to VikingOSX

Hmmm, your redirected stdin script doesn't seem to do anything after the final "AS", it just sits there, although I see where you're going with this.


After a bit of googling, I found a suggestion to restart the Finder to get the icon to show: 'killall Finder". That worked and my app now shows the correct icon, so I guess I don't need that 'property' solution.


Is your osascript bit supposed to give me the distinct lines of output? I'll play around with that.

Dec 14, 2018 1:21 PM in response to markfoley

Mark,


I don't have the apcaccess program, so could not test the osascript solution with it. I did test what you see on High Sierra replacing the apcaccess command with the piped output of 1) ls -lt, and 2) sw_vers | awk 'FNR == 2 {print $2}'. The code as shown above will do nothing until a return follows the trailing AS. The dialog appears immediately, and there is no need to perform a killall Finder. I didn't. Any output preceding the pipe symbol should thus appear in the display dialog.


Works in Mojave too:

Dec 15, 2018 11:42 AM in response to Barney-15E

OK, I got the output into a dialog box, as you did. Perhaps I typed something wrong, or perhaps I was trying it from ssh and not a terminal window. I'm actually not testing with the apcaccess program either. I'm using 'ls -l'. So, this all worked at the command line. Can I just modify my .app to use your latest example? I'm not sure I can do that as the actual script bit is included in the redirected input. My goal is to have something in the docker the user can click on.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Get command line output on console

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.