Jakub Sypiański Posted January 2 Posted January 2 (edited) I'm a beginner in programming. I wrote a script that checks for a selected word in online dictionaries of ancient Greek. How it is supposed to work 1. I select a work. 2. I press a hotkey. 3. I select one of about ten dictionaries via a popup in Alfred. 4. Alfred passes two values to the Python script: selectedWord and selectedDictionary. 5. The script prints scrapes the definition and sends it to Large Type. Where problem happens External scripts can't take arguments referenced to as {queries}, they must be sys.argvs. I know how to pass one sys.argv to the script, but I don't know how to pass both selectedWord and selectedDictionary. I looked extensively online, especially here on the forum, but I found no solution. As I'm a beginner, there might be other problems with my code. Workflow: Logemporos 0.2 on Dropbox Alfred: 5.5.1 [2273] MacOS: Sequioia 15.2 (24C101) Edited January 2 by Jakub Sypiański
FireFingers21 Posted January 2 Posted January 2 @Jakub Sypiański I don't have the python libraries installed that you're using, so I can't really help much with debugging the code. That said, check out the Reading Environment Variables documentation, there's a section for Python. Reading variables is slightly different from reading arguments.
Jakub Sypiański Posted January 2 Author Posted January 2 Thank you, @FireFingers21. Actually I reproduced my problem in a very small and simple workflow which doesn't require any libraries. Python variables workflow 0.1 on Dropbox
FireFingers21 Posted January 2 Posted January 2 @Jakub Sypiański Variables set within a Run Script Action aren't automatically accessible to the rest of the workflow chain. One way to make them accessible is to print a specific JSON format. The following Python script will allow {var:logeionURL} to work in your Large Type Output: #!/usr/bin/env python3 import os selectedWord = os.environ["selectedWord"] logeionURL = f"https://logeion.uchicago.edu/{selectedWord}" print(f"""{{ "alfredworkflow" : {{ "variables" : {{ "logeionURL" : "{logeionURL}" }} }} }}""") Jakub Sypiański 1
Jakub Sypiański Posted January 2 Author Posted January 2 Thank you. In case someone else ever runs into the same problem: - My working dictionary workflow: Logemporos 0.4 - A working version of the example workflow: Python variables workflow 0.2
vitor Posted January 2 Posted January 2 For completeness—though @FireFingers21’s excellent response already addressed this specific case—Run Scripts can take multiple arguments. For example, if you use a Hotkey Trigger or Universal Action on selected files, they are passed as multiple arguments. You can also do it from a Script Filter Input by setting arg to an array. There are also several objects, such as Automation Tasks which send out (and receive in) multiple arguments when it makes sense. Naturally, if you’re selecting text Alfred can’t guess if you want one long string or several or where to split them. That is where the Split Arg Utility comes in. 12 hours ago, Jakub Sypiański said: External scripts can't take arguments referenced to as {queries}, they must be sys.argvs. You should always prefer with input as argv over with input as {query} anyway. Jakub Sypiański 1
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now