Jump to content

Bash Output to Notification


Recommended Posts

I feel like I must be too stupid to use Alfred and maybe I shouldn't even try to do this anymore.

 

I just want to take the output of a bash command and post to OSX Notification; I honestly can't believe how so dramatically different (difficult/frustration/inane) trying to do this simple thing is compared with the amazing ease of use and simple to understand the rest of Alfred typically is.

 

I will post my workflow as it is currently, but it should be noted this is perhaps the 37th attempt to figure it out:

 

Workflow view attached, where contents of 'Terminal Command' are as follows:

 

export LPR
LPR=$(/usr/local/bin/xmda -l | grep {query})
echo $LPR

 

Terminal Command

xmda is simply a program (which works perfectly fine at bash prompt) that outputs a number of lines of text. Here I am simply filtering that output using grep, and again this works at a bash prompt without fail.  I am using the full path, although that is not necessary at a bash prompt (however it works either way, this is not a path issue). 

 

Post Notification

As the image shows, I have {query} in the Text field, conforming I believe anyway, to the ahem, unique manner in which Alfred requires {query} to be used as a "placeholder" variable of sorts, rather than simply allowing normal variable use. Neverminding the latter, I am confounded and feel like I am doing this right.  I have searched and read numerous articles and this forum, but each bit of help I can find either seems to fail for me or doesn't come quite close enough to do exactly what I am trying to in this case —leaving me to wonder if there is something "off".

 

I have tried to do something like this perhaps a dozen times in Alfred, each time going away insanely frustrated --I can get around in bash and understand basic programming reasonably well. I think that contributes to the way I feel and why I always end up walking away from each effort.

 

here' s the part where I get schooled, and I stand humbly by with great appreciation for any help that can be provided by those more knowledgeable (and patient) than I!

 

--Thank you for reading.

how_to_notify.png

Link to comment

The problem is you’re using a Terminal Command. What you want is a Run Script with bash as the language.


A Terminal Command will spawn a terminal (i.e. an external app) and tell that app to do things. It has no way for that app to return things to the Workflow itself — it’s a separate thing. By doing it in a Run Script (which you should be doing most of the time anyway), the end of your script will then be passed along and things will work as you expect.


Terminal Command definitely has its place, but I see it used wrong more often than I see it used when appropriate. It seems like a perfectly normal mistake for users new to scripting to do, though. I wonder if Alfred should show a warning on the difference between the two methods whenever a user adds a Terminal Command.

Edited by vitor
Link to comment
  • 2 weeks later...

I was initially completely befuddled because I knew for certain that I had tried that and every other imaginable method/approach.  

 

In the end, apparently it is not acceptable to use a pipe (e.g., | pbcopy, or as I had used above | grep) to the end of the line or it wipes out the value of {query}.  Is there a way around that?

 

I re-worked my process in order to get the output into my clipboard another way, and ultiamtely I'm really happy it works, I just think there must be something unnecessarily confusing about it or I wouldn't have run into this problem, it is just too easy!??   I don't know...

 

 

Edited by apgarmd
Link to comment

Pipes work fine. Any script output to STDOUT will be passed as input to any downstream Post Notification output.

 

You're probably doing something wrong, like printing an empty line that's causing your actual message to disappear.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...