Jump to content

ZotQuery: an Alfred workflow for Zotero


Recommended Posts

Hmm...What are your export preferences? Markdown or Rich Text? Which CSL Style? 

 

Also, are you certain that you are connected to the internet when you try to export? Currently, all exporting uses Zotero's web API, so the workflow makes an https call to the Zotero server to get the citation/reference. I'm only asking to eliminate the simplest problem. 

Link to comment

Ok. Well to debug, I'm going to need you to do some Terminal work. I will try to walk you though all of the necessary steps tho. First, obviously, simply open the Terminal app on your Mac. Next, you will need to open Alfred's preferences, go the Workflows page, find ZotQuery, double click on one of the script filters (`zot` will work) and click the "Open workflow folder" button in the bottom-right above the "Save" button. This will open a finder window with all of the ZotQuery python scripts. Going back to the Terminal, type `cd ` (note the space) and then go to the Finder window for the ZotQuery folder and drag the folder icon at the top of the Finder window into the Terminal. This will automatically copy the folder path into your Terminal command such that it looks something like this:

`cd /Users/USERNAME/Library/Application\ Support/Alfred\ 2/Alfred.alfredpreferences/workflows/user.workflow.E127B2CC-1F13-4485-AF32-4022D5E52A6F`

Once this is in the Terminal window, simply press Return. This will change the present working directory to the ZotQuery folder. 

 

For the next step, you will need to have the Zotero key for one of the items in your Zotero library. There are a number of ways to find this info. The easiest will probably be to open ZotQuery JSON cache of your Zotero library. This file is found here:

`~/Library/Application Support/Alfred 2/Workflow Data/com.hackademic.zotquery/`

In this folder you will find a file titled "zotero_db.json". Open it. Every item will begin with an "id": [n], where n is some number. The next line will have the key-value pair: "key": [x], where x is some combination of uppercase letters and numbers. Simply copy any one of these keys to your clipboard. 

 

Once you have an item key, go back to the Terminal and type:

`python action_export-citation.py "[KEY]"`.

Note that you will need to type the quotation marks and then paste the item key into the [KEY] field. Once typed, press enter. Whatever the Terminal spits out, copy it and post it back here. We will go from there.

 

Hope we get this solved soon,

stephen

Link to comment

Traceback (most recent call last):


  File "action_export-citation.py", line 6, in <module>


    from dependencies.pyzotero import zotero


  File "/Users/username/Google Drive/Backups/Alfred.alfredpreferences/workflows/user.workflow.B8689F89-C42E-4FDD-88F3-F6157749F7E9/dependencies/pyzotero/zotero.py", line 42, in <module>


    import requests


ImportError: No module named requests


username:user.workflow.B8689F89-C42E-4FDD-88F3-F6157749F7E9 username$ 


Link to comment

Okay. This means that your Python distribution doesn't have the requests module installed. I'm not certain why the configurator didn't install it tho. But, the solution should be:

Open Terminal again, this time you don't need to change the directory or anything. Simply enter 

easy_install requests

Let me know what the Terminal spits out. 

Edited by smarg19
Link to comment

v1020-wn-25-46:~ username$ sudo easy_install requests


Password:


Searching for requests


Reading http://pypi.python.org/simple/requests/


Best match: requests 2.2.1


Downloading https://pypi.python.org/packages/source/r/requests/requests-2.2.1.tar.gz#md5=ac27081135f58d1a43e4fb38258d6f4e


Processing requests-2.2.1.tar.gz


Running requests-2.2.1/setup.py -q bdist_egg --dist-dir /tmp/easy_install-Wezeyj/requests-2.2.1/egg-dist-tmp-P_0TVY


Adding requests 2.2.1 to easy-install.pth file


 


Installed /Library/Python/2.7/site-packages/requests-2.2.1-py2.7.egg


Processing dependencies for requests


Finished processing dependencies for requests


v1020-wn-25-46:~ username$ 


Link to comment

Last login: Fri Jan 31 15:08:37

v1020-wn-25-46:~ username$ cd /Users/username/Google\ Drive/Backups/Alfred.alfredpreferences/workflows/user.workflow.B8689F89-C42E-4FDD-88F3-F6157749F7E9 

v1020-wn-25-46:user.workflow.B8689F89-C42E-4FDD-88F3-F6157749F7E9 username$ python action_export-citation.py "[ABCD2345]"

Traceback (most recent call last):

  File "action_export-citation.py", line 6, in <module>

    from dependencies.pyzotero import zotero

  File "/Users/username/Google Drive/Backups/Alfred.alfredpreferences/workflows/user.workflow.B8689F89-C42E-4FDD-88F3-F6157749F7E9/dependencies/pyzotero/zotero.py", line 44, in <module>

    import feedparser

ImportError: No module named feedparser

v1020-wn-25-46:user.workflow.B8689F89-C42E-4FDD-88F3-F6157749F7E9 username$ 

Edited by zfolgerl
Link to comment

Hmm... Looks like another Python module isn't installed. This is odd. Can you run the same Terminal test, but with the command (from the workflow folder, like before):

python config_install-python-dependencies.py

This code is supposed to check for any missing Python modules and install them for you. I want to know why it isn't in your case. 

Link to comment

Zotquery prompted me to install osascript. Also, here is what terminal gave me:


 


Last login: Fri Jan 31 15:38:23 on ttys000


v1020-wn-25-46:~ username$ cd /Users/username/Google\ Drive/Backups/Alfred.alfredpreferences/workflows/user.workflow.B8689F89-C42E-4FDD-88F3-F6157749F7E9 


v1020-wn-25-46:user.workflow.B8689F89-C42E-4FDD-88F3-F6157749F7E9 username$ 


v1020-wn-25-46:user.workflow.B8689F89-C42E-4FDD-88F3-F6157749F7E9 username$ python config_install-python-dependencies.py


  Running setup.py (path:/private/tmp/pip_build_root/feedparser/setup.py) egg_inCleaning up...nstalled feedparseredparser


v1020-wn-25-46:user.workflow.B8689F89-C42E-4FDD-88F3-F6157749F7E9 username$ 


Link to comment

That looks like the config script downloaded feedparser. So, maybe things will work now. 

 

If not, part of the problem might be the path to the workflow. I see that it is within a Google Drive backup. I'm not certain why Alfred is opening this folder. There should be a folder at

/Users/[username]/Library/Application Support/Alfred 2/Alfred.alfredpreferences/workflows/user.workflow.B8689F89-C42E-4FDD-88F3-F6157749F7E9

This is the proper directory for ZotQuery. Try opening this folder, changing directory to here in the Terminal and running the install_dependencies script again from the Terminal. (on a side note, I will ask the Alfred developers why "Open workflow folder" opened a backup).

Link to comment

Wait, do you mean that what's in the clipboard isn't what you wanted? There may be a difference in jargon:

 

Reference = short 

Citation = full

 

So, in Chicago, exporting a References gets (Author Date), but exporting a Citation gets Author. Date. Title. Pub.

 

Sorry if there was some confusion

Link to comment

python action_export-ref.py


 


Last login: Fri Jan 31 18:10:40 on ttys000


username:~ username$ cd /Users/username/Google\ Drive/Backups/Alfred.alfredpreferences/workflows/user.workflow.A86DE4D1-8975-498F-9FC0-613D05E03899 


username:user.workflow.A86DE4D1-8975-498F-9FC0-613D05E03899 username$ python action_export-ref.py


Traceback (most recent call last):


  File "action_export-ref.py", line 26, in <module>


    item_key = alp.args()[0]


IndexError: list index out of range


username:user.workflow.A86DE4D1-8975-498F-9FC0-613D05E03899 username$ 


 


python action_export-citation.py


 


username:user.workflow.A86DE4D1-8975-498F-9FC0-613D05E03899 username$ python action_export-citation.py


Traceback (most recent call last):


  File "action_export-citation.py", line 31, in <module>


    ref = zot.item(item_key, content='bib', style=prefs['csl'])


  File "/Users/username/Google Drive/Backups/Alfred.alfredpreferences/workflows/user.workflow.A86DE4D1-8975-498F-9FC0-613D05E03899/dependencies/pyzotero/zotero.py", line 134, in wrapped_f


    retrieved = self._retrieve_data(func(self, *args))


  File "/Users/username/Google Drive/Backups/Alfred.alfredpreferences/workflows/user.workflow.A86DE4D1-8975-498F-9FC0-613D05E03899/dependencies/pyzotero/zotero.py", line 243, in _retrieve_data


    error_handler(self.request)


  File "/Users/username/Google Drive/Backups/Alfred.alfredpreferences/workflows/user.workflow.A86DE4D1-8975-498F-9FC0-613D05E03899/dependencies/pyzotero/zotero.py", line 1228, in error_handler


    raise error_codes.get(req.status_code)(err_msg(req))


dependencies.pyzotero.zotero_errors.ResourceNotFound: 


Code: 404


URL: https://api.zotero.org/users/968223/items/NKT78PX8?content=bib&style=apa&key=w3QeCc7a33EvUk84PtY1egvO


Method: GET


Response: Item does not exist


username:user.workflow.A86DE4D1-8975-498F-9FC0-613D05E03899 username$ 


Link to comment

Okay, there are two issues. First, you will need to open the "action_export-citation.py" script to change one line of the code. The errors shows me that at some point, a commented out line that I use for testing wan't left commented out. Thus, the script is searching your Zotero library for one of my Zotero item keys. In order to fix this, simply open the script, and below the line that has 

item_key = alp.args()[0]

you will find something like 

item_key = 'NKT78PX8'

However they are, they need to like this:

item_key = alp.args()[0]
#item_key = 'NKT78PX8'

The line with an actual item key needs a hash mark before it to comment it out. This will solve your first problem.

 

The second problem is that you forgot to enter an item key when you where testing these scripts. That's the error from the "action_export-ref.py" script. you need to enter something like this into the Terminal:

python action_export-ref.py "[KEY]"

to test the scripts. 

Link to comment

Not a problem. Just posted a new version to Packal with the bug fixed. I've also cleaned up my error logging, so it should be easier to track to errors without having users go to the Terminal themselves. 

 

So, please update your version!

Link to comment

Thanks for the updates and extensions of functionality – this is quite amazing. Have ZotQuery mostly running but can't access the z:settings – where I would like to change to rtf export. (It fails silently whenever I try to use the string "z:settings".) Also – is there a way to reconfigure ZotQuery? Whenever I attempt to re-configure, the message conveys "Already configured". So it seems a clean re-install is not possible. Cache reports success and all the searches except the collection one go well. Any ideas?

Link to comment

As to reconfiguring, open ~/Library/Application Support/Alfred 2/Workflow Data/com.hackademic.zotquery/ and then delete the first-run.txt file. Then run "z:config". 

 

 

As to debugging the settings script. Put this script into the Applescript Editor and run it, then report the errors. 

on run argv
	--- Set the path to the preferences file
	set path_ to (path to "cusr" as text) & "Library:Application Support:Alfred 2:Workflow Data:com.hackademic.zotquery:prefs.json" as text

	--- try to open the file and read it
	set the file_ to open for access file path_
	set json_ to (read file_)
	close access file_
	
	-- simplify JSON
	set {astid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {return & linefeed, return, linefeed, tab, character id 8233, character id 8232, ":", "\"", ",", "{", "}"}}
	set json_l to text items of json_
	set AppleScript's text item delimiters to astid
	
	-- remove empty items
	set itemsToDelete to {"", " "}
	set cleanList to {}
	repeat with i from 1 to count json_l
		if {json_l's item i} is not in itemsToDelete then set cleanList's end to json_l's item i
	end repeat
	
	-- JSON keys to AS vars
	repeat with i from 1 to count cleanList
		if cleanList's item i = "format" then
			set format_pref to item (i + 1) of cleanList
		else if cleanList's item i = "csl" then
			set csl_pref to item (i + 1) of cleanList
		end if
	end repeat
	
	-- Preferences dialog
	set id_query to display dialog "Next, choose your Export Style and Export Format for ZotQuery" buttons {"Cancel", "Next"} default button 2 cancel button 1 with title "ZotQuery Preferences"
	
	-- User choose CSL style
	choose from list {"chicago-author-date", "apa", "modern-language-association", "rtf-scan", "bibtex"} with title "ZotQuery Preferences" with prompt "Use which CSL style?" default items {csl_pref}
	set csl_pref to item 1 of result
	
	-- User choose export format
	choose from list {"Markdown", "Rich Text"} with title "ZotQuery Preferences" with prompt "Export citations and references in which text format?" default items {format_pref}
	set format_pref to item 1 of result
	
	---Write the data to the settings file
	set the file_ to open for access file path_ with write permission
	set eof of file_ to 0
	set json to "{" & return & tab & "\"format\": \"" & format_pref & "\"," & return & tab & "\"csl\": \"" & csl_pref & "\"" & return & "}"
	write json to file_
	close access the file_
end run

Let me know. 

Link to comment

Thanks for responding. Have reconfigrued ("Cache success"). Ran your script – here the output:

error "Ende der Datei erreicht." number -39 from file "Pundarika:Users:amw:Library:Application Support:Alfred 2:Workflow Data:com.hackademic.zotquery:prefs.json"

[The German bit simply means: "reached end of file"]

Thanks for having a look.

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