Jump to content

Using Query in Python file and setting Clipboard after

Recommended Posts



[Objective] I am trying to build a workflow which gets a DOI link [e.g. https://doi.org/10.1287/orsc.3.3.398]  and sets my clipboard to some text from a pulled JSON file. In this case (Orlikowski, 1992). However, I am having a hard time importing the query from Alfred in to the file, and getting it back. 


I have written the python script, lack the means to use the file. I have made the beginning of the workflow, using regex to focus on the link I need (e.g. 10.1287/orsc.3.3.398) with the regex key  - (\/\d{2}.\d{4}\/\w.*),

def main():
  urlData = "https://api.crossref.org/works/"
  # the query should be inserted on line 44
  urlData += "10.1287/orsc.3.3.398"
  # Open the URL and read the data
  webUrl = urllib.request.urlopen(urlData)
  print ("result code: " + str(webUrl.getcode()))
  if (webUrl.getcode() == 200):
    data = webUrl.read()
    # print out our customized results, but should set this to clipboard
    print ("Received an error from server, cannot retrieve results " + str(webUrl.getcode()))


Link to comment
30 minutes ago, Jenner36 said:

However, I am having a hard time importing the query from Alfred in to the file, and getting it back.


That's the bit of the workflow we need to see to tell you where you're going wrong.


If you want help with a workflow that isn't working, you need to show us the workflow. We can't guess what's wrong with something we've never seen. Please upload the workflow somewhere (Dropbox?) and post a link, so we can have a look for ourselves.


Link to comment

How is that even working at all? External Script requires an executable script, but your script doesn't have a shebang. Is the script you uploaded the one you're actually using it?


In any case, how exactly is this supposed to work? What do you want to enter into Alfred, and what are you expecting to get out, and how?


Link to comment

I’ve rewritten the workflow to show you how to handle input and output. The workflow only shows the generated reference in a Large Type output because I wasn't sure what you want to do with it. That's trivial to replace, though.


Basically, you print to STDOUT to pass output to the next action, and to STDERR to write to Alfred's debugger.


I've tidied up your parsing code a bit (the author name handling is a lot cleaner), but mostly I've added a bunch of "features" to show you how to handle various kinds of input and output. There's now a Script Filter to show you live feedback on the validity of your input (i.e. you have to enter a valid DOI, or it won't proceed). And if there's no input, it will check if there's a DOI on the clipboard and use that instead.


The script_filter.py file shows how to send results for Alfred to show, and how to pass input to following elements. And the lookup.py file also shows how to set workflow variables to pass to downstream elements (see the notify() function).


Both scripts log to Alfred's debugger (via STDERR), and they both catch errors and show them to the user instead of dying silently (which is Alfred's default behaviour). That’s important because urlopen raises an exception if you get a 404, so checking the response code isn’t enough (that code will never run).

Edited by deanishe
Link to comment

I am sorry for the late answer.


But I can see you solved the issue - thank you very much. I'll read through your code, just through skimming it I can see I have a lot to learn. 

As my title here in the forum I am a newbie so I greatly appreciate you taking the time!

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