Jump to content

[Solved] I think I'm not understanding setvar(key, value) correctly

Recommended Posts

Hi folks!


Long time lurker, first time poster, etc., etc.


I'm in the midst of writing a new workflow using Deanishe's wonderful alfred-workflow modules, and I'm hitting a wall in regards to using setvar(var, value). I think that I'm expecting it to work differently than it's working, and that my understanding is flawed and I need some help.


In the my workflow, keyword "su newticket" and run script "new_ticket.py" is the action I'm concerned with. The upper workflow can be ignored and isn't implemented yet. I'm just trying to call a python script, set a variable, and return it as a {var:<name>} variable. If I use a "copy to clipboard" node, I can see the variable I want embedded in JSON inside {query}, but if I try to access {var:message_out}, there's nothing. This is where I'm falling down :)


Workflow here:



Relevant info:
Alfred 3.3.2 [818]
macOS 10.12.4 (Darwin Kernel Version 16.5.0: Fri Mar  3 16:52:33 PST 2017; root:xnu-3789.51.2~3/RELEASE_X86_64 x86_64)
Alfred-Workflow 1.25.2
Any non-system language you're using (e.g. nodejs): none
Any non-system programs you're using (e.g. ffmpeg or youtube-dl) if they aren't bundled in your workflow: none
Any changes you've made to your macOS environment (e.g. via launchctl setenv) or the language you're using (e.g. via Python's usercustomize.py or sitecustomize.py files): none


I appreciate any advice or guidance! Thanks in advance and all that :)



Edited by Tony_Et
Link to comment

setvar() only works in Script Filters.


If you want to set a variable from a normal Run Script action, you need to do that yourself by printing JSON as described in the sticked variables thread, e.g.:


import json

message_str = "this is my message"
output = {
    'alfredworkflow': {
        'arg': 'whatever',
        'variables': {
            'message_out': message_str,

print json.dumps(output)


Link to comment

Ohhhhhhhhhhhh, right. I knew there was a facepalm coming my way. There it is.


In a fit of frustration last night, I almost used json.dumps() (from what I gleaned from your post on it https://www.alfredforum.com/topic/9070-how-to-workflowenvironment-variables/) to shortcut around what I thought setvar() was supposed to be doing, and I completely missed the info that setvar() only works with script filters. Thanks for setting me on the right path! Much appreciated.

Edited by Tony_Et
Link to comment
2 hours ago, deanishe said:

I haven't been able to think of a good name for the class.


Ah, yes, one of the two hard problems in computer science: cache invalidation, naming things, and off-by-one errors.


Joking aside, my real reason for posting is to thank @Tony_Et for the great bug report. You really followed the guide! Thank you. If all users followed your example our work here would be incredibly easier.

Link to comment

:D I've not heard the variation with the off-by-one error before. Even better. 


You're spot on about the bug report. Should really be in the Alfred-Workflow thread, but the library seems to "enjoy" assumed official status (possibly because of the name?)


But that's by the by. You posted an exemplary issue, @Tony_Et.


Took me less than a minute to identify the issue because you gave me everything I needed to do so. 


It's genuinely a pleasure to help posters like you, who make the effort to make it easy to help.


It's also a big help with improving AW's documentation: You clearly tried to figure it out yourself first, but the information you needed was rather between the lines. Now I know what I need to clarify. 


Edited by deanishe
I hate this editor
Link to comment

@vitor and @deanishe, thanks much again. Also, my apologies, I didn't realize that there was a specific Alfred-Workflow forum; I should have looked (and I'll post there next time). I know it's not an official part of Alfred itself, but for my purposes, it's a critical piece of designing workflows.


If it explains anything, My current day job is helping people troubleshoot, so I'm keenly aware of needing to have all the relevant information before solving a problem ;)


Edited by Tony_Et
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...