Jump to content
b-dubs

[SOLVED] Getting variables out of python script

Recommended Posts

I have a python script that is being run as an external script, using Python 3, and I wanted to get a variable out of my script to post in a notification. The script queries a MYSQL database, creates a CSV file from the database and then emails it. All I want to do is get a single variable out of my script and use it in the notification, but I can't seem to get notifications running at all with the workflow. Even if I try to just post a "Success" notification, nothing appears. What am I missing?

Edited by b-dubs

Share this post


Link to post

Welcome @b-dubs,

 

When asking for help with a Workflow, please upload it somewhere as we can’t properly help you without access to it. Debugging can already be hard with access to the code, and you’re asking us to guess yours from a description. There are multiple places where your code or Workflow setup may be going wrong. Without looking at it we’re shooting in the dark.


Read the Reporting Problems with Workflows topic, as it gives a nice overview on how to build an effective report.

Share this post


Link to post

Ok. I played with it a little and got it to send a notification, but I have to sit and wait for the python script to finish running before doing anything else, or Alfred won't send the notification. Is that normal? The workflow can be found here:

 

https://www.dropbox.com/s/xsjkanvaod7uvo4/Attendance Pad.alfredworkflow?dl=0

 

Also, how does one send queries from Alfred into python? I have an if/else statement in my python script that checks to see if arguments have been passed and, if not, just find the date of the last Sunday and use that to query the database. When I do that I get a python error that the date is formatted incorrectly, even if I don't send anything along.

 

Thanks

Share this post


Link to post
20 hours ago, b-dubs said:

When I do that I get a python error that the date is formatted incorrectly, even if I don't send anything along.

 

Bug in Alfred. With External Script, Alfred always passes an argument, even if you have the Script Filter set to No Argument. So check that sys.argv[1] isn't an empty string before trying to parse it.

 

20 hours ago, b-dubs said:

or Alfred won't send the notification. Is that normal?

 

Absolutely. Alfred won't run the next element until the current one has finished. And as you've connected it to a Script Filter, the notification also won't be run until you action the Script Filter result.

 

I'm not sure exactly what your workflow is ultimately supposed to do, but for what it currently does, I think you're going the wrong way about it. You should probably use a simple Keyword (not a Script Filter) and connect it to two outputs: the notification and the script that queries the database and sends the email.

 

The Script Filter is inappropriate because Alfred will (try to) run it after every single keypress, not just once when you've finished entering a valid date. Its purpose is to show live results or feedback, and that isn't what you're using it for. The only reason to use a Script Filter here would be to show an error message if the input is invalid.

Edited by deanishe
Speling & grammer

Share this post


Link to post
48 minutes ago, deanishe said:

 

Bug in Alfred. With External Script, Alfred always passes an argument, even if you have the Script Filter set to No Argument. So check that sys.argv[1] isn't an empty string before trying to parse it.

 

I'm not sure exactly what your workflow is ultimately supposed to do, but for what it currently does, I think you're going the wrong way about it. You should probably use a simple Keyword (not a Script Filter) and connect it to two outputs: the notification and the script that queries the database and sends the email.

 

Thank you!

 

Using a keyword trigger to run an external script, which then triggers a post notification did exactly what I was looking for. Strange bug with the passing of arguments no matter what, but checking for an empty string also fixed that problem. All is working now.

 

Thanks!

Share this post


Link to post
44 minutes ago, b-dubs said:

Strange bug with the passing of arguments no matter what, but checking for an empty string also fixed that problem

 

Might be intentional for all I know. If the argument is guaranteed to be present, you don't need to check for its presence, just whether it's empty or not.

Share this post


Link to post

@deanishe interesting - I can see why this is behaviour (and predictable)... I'm not sure it would be wise to change this behaviour to not pass an argument as this may cause regression issues.

Share this post


Link to post
54 minutes ago, deanishe said:

@Andrew Yeah, it’s probably best to leave it as it is. 

 

Perhaps worth revisiting for Alfred 4?

 

Looking back through my notes, I think the original reason it's like that is because argv came after {query} (which is just string replaced in the script) - An empty argument for {query} was always represented as empty string as there was no obvious or predictable way to string replace "not set" for {query}.

 

I'll probably leave this as is.

Share this post


Link to post
40 minutes ago, Andrew said:

I'll probably leave this as is.

 

Makes sense where there's a {query} option, but what about External Script?

Share this post


Link to post

@deanishe I've made a note to have a think about this - but that would then make the behaviour different if you were to migrate an internal to an external script.

 

I do agree that with an external script, it makes sense to differentiate between no argument and empty argument.

Share this post


Link to post

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
×