Jump to content

Inserting a script variable into Notification output?


Recommended Posts

Very simple question here.

 

My workflow runs a simple script, and assigns stdout to a variable:

 

on alfred_script(q)
    set script_output do shell script "<script>"
end alfred_script

 

All I want to do is to show script_output in the post notification, but I can't find any way of doing that, I can set the "Text:" field to either "{query}", which inserts the query text - or I can input raw text, which it just treats as raw text. I can't find any way to specify a variable name other than query, and have Alfred treat it like anything but raw text.

 

Little help? Thanks...

 

Link to comment

I think you've misunderstood how workflows work. Curly braces only work with {query}.
 
It has nothing to do with variables: it's a simple text placeholder. When Alfred runs your script it does a simple search/replace and replaces any instance of {query} with the value of your input. Wrapping anything else in curly braces just gives you a word in curly braces. Input to a script or notification is always inserted via {query}. The names of your variables are entirely irrelevant.
 
The exception is the Run NSAppleScript action you're using. Because this is run as live code within Alfred, it can pass a real variable to your code, instead of replacing the text {query}. That's the q in on alfred_script(q).
 
Try this:
CCSnqru.png

Link to comment

That's what I suspected - thanks for the verification.

 

I'm surprised that Alfred's functionality isn't more robust in this respect. (AppleScript + notification window) and (console session + notification window) must be very routine combinations. Both AppleScript and console sessions can provide output, in the form of stdout. Why not include {output} as an option for the notification center? Seems like a very simple option to implement, and would open up a nice range of functionality.

Edited by sfsdfd
Link to comment

That's exactly how Alfred works. It just uses {query} throughout (rather than {input} and {output}).
 
As far as running scripts is concerned, any output to STDOUT is treated as the input to the following action (i.e. the following action's {query}).
 
The exceptions are Run NSAppleScript (where you use q for input and return for output as described above) and Script Filters, which must output XML to STDOUT.

Edited by deanishe
Link to comment

That's exactly how Alfred works. It just uses {query} throughout (rather than {input} and {output}).

 

As far as running scripts is concerned, any output to STDOUT is treated as the input to the following action (i.e. the following action's {query}).

 

The exceptions are Run NSAppleScript (where you use q for input and return for output as described above) and Script Filters, which must output XML to STDOUT.

Oh! I see!

 

I've been too steeped in JavaScript recently. I presumed that {query} was a static variable from the initial trigger, and that it got passed into every further action but wasn't mutable. I see now exactly what you mean: the query or {q} for each step is the output of the previous step.

 

That makes perfect sense and answers a lot of questions, and will help my script development. Thanks very much!

Link to comment
  • 3 weeks later...

That's exactly how Alfred works. It just uses {query} throughout (rather than {input} and {output}).

 

As far as running scripts is concerned, any output to STDOUT is treated as the input to the following action (i.e. the following action's {query}).

 

The exceptions are Run NSAppleScript (where you use q for input and return for output as described above) and Script Filters, which must output XML to STDOUT.

 

 

Hello deanishe. I have a dumb question and don't know if it's already answered in this thread.

 

I have created a workflow that uses a script filter which searches a web API by a zip code and returns the matching address. Look:

 

L3XOvxa.png

 

Is it possible to pass the workflow results to a Copy to Clipboard output?

Edited by xilopaint
Link to comment

Though the question was directed at deanishe, I’m answering as reply to this tweet.

Yes, it’s possible to pass Script Filter results into Copy to CLipboard. Look at deanishe’s own Glosbe translation workflow that does exactly that.

 

 

Sorry Vítor. Maybe I should had point out that I am still a beginner on coding and can only understand AppleScript at the moment.

 

Could you offer a more detailed answer for AppleScript language?

Edited by xilopaint
Link to comment

I am still a beginner on coding and can only understand AppleScript at the moment.

My advice is that you get away from AppleScript as soon as you can. It’s only useful to interact with some OS X and a loathsome language at every other level.

For the most part, it doesn’t really matter what language you learn programming concepts in, but AppleScript is really an exception, there. It is so different (in a bad way) from other languages, what you learn there can be hard to transpose to others. Add to that the fact it’s so limiting, you shouldn’t be wasting your time with it. Pick any other popular scripting language (ruby, python, even bash) and try that, falling back to AppleScript only when necessary.

Now that we’ve got that out of the way, you don’t need to understand deanishe’s code, just the connections. Simply connect your Script Filter into Copy to Clipboard. Make sure the latter consists of just {query}. Now, whatever you pass as the arg in your Script Filter will be what’s passed into the next stage of the workflow (i.e. it will be copied to the clipboard, in this case).

Link to comment

My advice is that you get away from AppleScript as soon as you can. It’s only useful to interact with some OS X and a loathsome language at every other level.

For the most part, it doesn’t really matter what language you learn programming concepts in, but AppleScript is really an exception, there. It is so different (in a bad way) from other languages, what you learn there can be hard to transpose to others. Add to that the fact it’s so limiting, you shouldn’t be wasting your time with it. Pick any other popular scripting language (ruby, python, even bash) and try that, falling back to AppleScript only when necessary.

Now that we’ve got that out of the way, you don’t need to understand deanishe’s code, just the connections. Simply connect your Script Filter into Copy to Clipboard. Make sure the latter consists of just {query}. Now, whatever you pass as the arg in your Script Filter will be what’s passed into the next stage of the workflow (i.e. it will be copied to the clipboard, in this case).

 

 

Thank you for the advice. In fact, I started from AppleScript because of its similarity with natural english. For a beginner it's easier to understand.

 

Regarding my question, I still can't get the point. I had already connected the Script Filter into Copy to Clipboard but the text copied to clipboard is not the result returned by my script filter (the address) but the typed argument (the zip code).

Edited by xilopaint
Link to comment

Like I said, whatever is in arg (e.g <item uuid='something' arg='my_argument' valid='yes'>) is what will be passed. No way around that, so you’ll need to build your logic with that in mind. The same way you show your results in <title> (or whatever), make them be in arg.

 

 

Now I get it! Thank you, Vítor!

Link to comment
  • 2 years later...

Hi,

 

How can I output variables (not a query) from jsx? 

 

I want to output two separate variables from this script:

 

const frontmost_app_name = Application('System Events').applicationProcesses.where({ frontmost: true }).name()[0]

const frontmost_app = Application(frontmost_app_name)

function run() {
if (['Google Chrome','Google Chrome Canary','Chromium','Opera','Vivaldi'].indexOf(frontmost_app_name) > -1) {
  var current_tab_title = frontmost_app.windows[0].activeTab.name()
  var current_tab_url = frontmost_app.windows[0].activeTab.url()
} else if (['Safari','Safari Technology Preview','Webkit'].indexOf(frontmost_app_name) > -1) {
  var current_tab_title = frontmost_app.documents[0].name()
  var current_tab_url = frontmost_app.documents[0].url()
} else {
  throw new Error('You need a supported browser as your frontmost app')
}


console.log({"alfredworkflow": {"variables": {"var1": current_tab_url, "var2": current_tab_title}}})

}

These are then sent to a notification: https://www.dropbox.com/s/6jpnigr1g6vd1g3/Screenshot 2018-03-27 10.45.57.png?dl=0

 

Link to comment

There is one obvious error.

 

Change this:

console.log({"alfredworkflow": {"variables": {"var1": current_tab_url, "var2": current_tab_title}}})

to this:

return JSON.stringify({"alfredworkflow": {"variables": {"var1": current_tab_url, "var2": current_tab_title}}})

 

If that doesn't fix it, upload your workflow somewhere.

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