Jump to content

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

Recommended Posts

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 


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 


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



Screenshot 2020-04-13 at 16.10.21.png

Edited by manavortex
added example code
Link to comment

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.

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