Jump to content
manavortex

[question] Best practice (arg or default/fallback)

Recommended Posts

Posted (edited)

Hello forum, 

What's the best practice for a workflow trigger (keyword or script filter) that uses an optional argument with a fallback value? 

Example 1 (I made this up): I want to more easily yell at people on the internet, so I have a workflow that turns text to caps. 
My clipboard is "foo". 
I call 

caps

and expect my script to receive "foo". 

 

 I call 

caps wasd

and expect my script to receive "wasd". 

 

Example 2: Like example 1, but instead of the contents, I want to use hardcoded text. 

I call 

caps

and expect my script to receive "your mother". 

 

 

 

... What's the best practice to do that? 

 

 

This would be the in-script solution. Is there a built-in way to do this?

import sys
from AppKit import NSPasteboard, NSStringPboardType

def getClipboard(): 
	pb = NSPasteboard.generalPasteboard()
	pbstring = pb.stringForType_(NSStringPboardType)
	return u"Pastboard string: %s".encode("utf-8") % repr(pbstring)

query =  sys.argv[1] if len(sys.argv) >= 2 else getClipboard()
# query =  sys.argv[1] if len(sys.argv) >= 2 else "your mother" #example 2
sys.stdout.write(query.upper())

 

 

Screenshot 2020-04-13 at 16.10.21.png

Edited by manavortex
added example code

Share this post


Link to post

An optional argument to a Script Filter only determines whether the Script Filter will be run before you've entered a query. You still have to action a result to pass control to the next action.

 

With a Keyword, you probably want to set your default/fallback in code. You can do it in Alfred with a pair of conditionals, but that's a lot more complicated than just doing it in code.

 

If it's something you think you'll change a lot, put it in a workflow variable so you can change it from the workflow's configuration sheet.

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