Jump to content
Tony_Et

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

https://www.dropbox.com/s/8kjpu6jk4ze4moc/StreetUtils.zip?dl=0

 

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

 

-tony.

Edited by Tony_Et

Share this post


Link to post

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)

 

Share this post


Link to post

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

Share this post


Link to post

I should probably split the variables code into a separate class that can be also used in Run Script actions, tbh.

 

The only reason I haven't done that yet is because I haven't been able to think of a good name for the class.

Share this post


Link to post

I wish I could help you in return. I can only think of something vanilla like "WorkflowVar" or "Variable3", or something completely inappropriate like "BestClassEverReturnVar" ;)

Regardless, thanks for the help.

Share this post


Link to post
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.

Share this post


Link to post

: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

Share this post


Link to post

@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

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...