Jump to content
heepie

[SOLVED] Pass the result to Alfred by Script Filter

Recommended Posts

Hello, I’m developing bookmark workflow for the minor browser.

Now, I faced the problem.
 
I’m pass Alfred Json by script filter.
 
When I run my code by `python3`, I checked correct outputs like below:
{
  "items": [
    {
      "arg": "https://www.alfredapp.com/help/workflows/inputs/script-filter/json/",
      "subtitle": "https://www.alfredapp.com/help/workflows/inputs/script-filter/json/",
      "title": "Script Filter JSON Format - Workflow Input Objects - Alfred Help and Support",
      "uid": "https://www.alfredapp.com/help/workflows/inputs/script-filter/json/",
      "valid": true
    }
  ]
}

 

But I couldn’t show my result by Alfred. 
 
My code
@dataclass
class UrlItem:
    name: str
    url: str

class EnhancedJSONEncoder(json.JSONEncoder):
    def default(self, o):
        if dataclasses.is_dataclass(o):
            return dataclasses.asdict(o)
        return super().default(o)

# There is Parser class 
          
def main():
   Parser.parseForJson(bookMarkPath)
   json.dump(dict(items=mapToAlfredJson()), sys.stdout, indent=2, sort_keys=True, cls=EnhancedJSONEncoder)
   return 0

def mapToAlfredJson():
    alfredJsonList = []
    for item in bookMarkList:
        alfredJsonList.append(dict(
            title=item.name,
            subtitle=item.url,
            arg=item.url,
            uid=item.url,
            valid=True,
        ))
    return alfredJsonList

if __name__ == "__main__":
    code = main()
    sys.exit(code)

Can I get your help? 😭

Edited by heepie

Share this post


Link to post

when I checked it by debugger, the cause is python3. 

I developed it on python3. But Alfred just support python2, right?

 

Anyway, I solved the problem by replacing ‘dataclass’ to ‘namedtuble’.

It’s possible to use dataclass on python3 later

Share this post


Link to post
5 hours ago, heepie said:

But Alfred just support python2, right?

 

Alfred doesn't care which language you write workflows in.

 

But you obviously can't set Language = /usr/bin/python and write Python 3 code in the Script box because /usr/bin/python is the Python 2 interpreter.

 

If you want to use Python 3, either use Language = External Script or use Language = /bin/bash and treat the Script box like a command line.

Share this post


Link to post

Thank for your comment!

 

Quote

because /usr/bin/python is the Python 2 interpreter.

is it possible to change python2 -> python3?? I couldn't find the solution 😭

 

 

I'm trying it by `External Script`. But I also faced the problem.

1. Alert popup

Reason: launch path not accessible

The external script may not exist, or doesn't have execute (+x) permissions.

Related Workflow Info...
Name: 'Bookmarks'
Folder:/Library/Application Support/Alfred/Alfred.alfredpreferences/workflows/user.workflow.4119C437-9471-463E-A5AE-BC7A9185EC4E

2. Debugger

Bookmarks[Script Filter] Queuing argument '(null)'

So I add 'chmod +x bookmark.py'. But it's not solved

Additionally, when I execute './bookmark.py', the error occur. but 'python bookmark.py' is OK.

 


Could you help me??

Edited by heepie

Share this post


Link to post
1 hour ago, heepie said:

is it possible to change python2 -> python3?? I couldn't find the solution 😭

 

No.

 

1 hour ago, heepie said:

So I add 'chmod +x bookmark.py'. But it's not solved

Additionally, when I execute './bookmark.py', the error occur. but 'python bookmark.py' is OK.

 

You need to add the correct shebang to your script.

 

If python bookmark.py works in your shell, it sounds like you've installed Python 3 via Homebrew. In that case, the shebang would be #!/usr/local/bin/python

 

If that doesn't work, run the following commands in your shell and post the output:

which python

python --version

Share this post


Link to post

Thank you! 

I solved the problem and correctly work! 👍

 

Quote

You need to add the correct shebang to your script.

Also, I studied the concept 'shebang'! 

 

Gooo :D

Share this post


Link to post

@deanishe, I have a question for 'shebang'.

If I execute my script by 'shebang' like '#!/usr/local/bin/python3', it's OK. because python3 is already installed on my env

 

But if python3 is NOT installed on other's env, my script can't be executed. 

 

Is there the way to execute my script even if python3 is NOT installed (like JVM)? 🤔

Share this post


Link to post
2 hours ago, heepie said:

Is there the way to execute my script even if python3 is NOT installed (like JVM)?

 

No. At least not a good one. That's why most workflows are Python 2: it's installed by default on every Mac.

 

It is possible to create a standalone executable from a Python script, but that means bundling large parts of Python with it, and making a 30MB workflow from a 5KB script is pretty user-unfriendly.

Share this post


Link to post
Quote

No. At least not a good one. That's why most workflows are Python 2: it's installed by default on every Mac.

 

OK. I'll develop on python2 env for every users. Thank!

Share this post


Link to post
6 hours ago, heepie said:

OK. I'll develop on python2 env for every users.

 

TBH, you should stick with Python 3. Python 2 is no longer supported and will be removed from macOS soon. Python 3 is installed by default on Catalina. Just not on any older versions of macOS.

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...