Jump to content

Using an external Python script with more than one sys.argv


Recommended Posts

Posted (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 by Jakub Sypiański
Posted

@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}"
    }}
  }}
}}""")

 

Posted

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.

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