Jump to content

Help understanding the "with space" option in the Script Filter object


Recommended Posts

Alfred 5.1.1-2138

 

I have a bunch of Script Filters that have the "with space" checkbox enabled. Not sure when it started, but I noticed that the Script Filters are running even when I don't have the space after the trigger keyword. Screenshot examples below.

 

Am I doing something wrong here or is this a bug?

 

screenshot_jfEkV7Fb.thumb.png.d26b6ff37a550878f1b13530ddf5903b.png

 

image.thumb.png.94012ed8f618816534e7cdebf99ca180.png

Edited by luckman212
Link to comment
  • luckman212 changed the title to "with space" keyword/argument option being ignored? (Script Filter)

@Andrew Hmm.  Maybe I mis-understand these options then. Here's what I "want" not sure if there's a way to make Alfred behave like this:

 

- Do not run script filter until & unless there's a space after the keyword

- When script does run, don't require an argument (but do pass an arg if it's supplied)

- If keyword is "sn" and I type "sn " (space after) then script should run passing empty arg (``) or no arg. (Either would be fine!)

 

What currently happens if I set it to "Argument required" is:

 

- Script does not run with just "sn " (makes sense)

- I have to type 2 spaces to get the script to run with an empty arg.

 

image.thumb.png.2f5059d93d55f7ff1dbf284ea85e3360.png

 

screenshot_JDfvS79d.thumb.png.e8096cc78f6145c2a323e01877c4b4c5.png

Link to comment
3 hours ago, luckman212 said:

I have to type 2 spaces to get the script to run with an empty arg.

 

It’s not running with an empty argument, but with a space. If you type something else instead, that’s what it’ll run with. If that works for your case, set it to Argument Required and untick with space. You can take it further by (in the code) ignoring the case of a single space.

Link to comment

Ok. I could be going crazy, but- has this always been the behavior? I have a number of script filters and I never remember them behaving this way. Now I have to go back and change my scripts to parse the ' ' (space) arg as if it were an empty arg. I am 99% sure I never set my script filters up this way before...

 

edit: I just scrolled through and fiddled with ~a dozen workflows and I am now even more sure that something has changed... 

 

edit2: for sure all of my script filters where "Alfred filters results" was enabled are now semi-broken. I don't see anything in the change logs but @Andrew was something tweaked on this?

 

The problems, as I see it currently:

 

1) If script filter options are changed to "Arg required" (without space) as Vitor suggested, scripts need to be adjusted to automatically parse/trim a single space and process as if "no args" were supplied.

2) There's now seemingly no way to have a Script Filter where "Alfred filters results" is checked, a parameter is NOT required, and also require the user to have a space after the keyword. In my mind, a SF set up as the screenshot below should NOT run until a space is entered at the end of the keyword... and yet, it does... 

 

screenshot_UmOb9U9W.thumb.png.91e3b5d577ab1ae69f027aca5e7cebd0.png

Edited by luckman212
Link to comment

To quadruple check, I ran the Getting Started → Script Filter workflow (whose jsonformat also uses with space, Argument Optional, and Alfred Filters Results) in Alfred 5.1.1 (2138), 5.0 (2057), 4.8 (1312), and 4.6.7 (1305). It worked the same in all of them, showing results without having to type the space.

Link to comment

@vitor can you please test the attached workflow and let me know if you believe that the behavior of the .sf3* keywords is correct/expected?

 

I feel that since the script filter is configured to require a space, that it should NOT be running the script in this position:

image.thumb.png.af6bd8d52858e6f9738a9bde9a7c4470.png

image.thumb.png.f6eeb832bb9eb7215048ebd96438718b.png

 

 

Download Workflow:  sftest.alfredworkflow

image.thumb.png.ceff3f51efeb924d11223a04ebfc250b.png

Edited by luckman212
Link to comment

@vitor You're right (of course 🙂), I tested & the behavior is the same going back even as far as Alfred 4.7.1306.

 

I can't imagine how I never noticed this! But it raises a question. Consider the following situation:

 

ScriptFilter 1:

- trigger keyword: `app`
- with space

 

ScriptFilter 2:

- trigger keyword: `appstore`
- with space
- Alfred filters results

- needs to be able to run with no args

 

How can Alfred be configured such that:

- You want to run the `appstore` script
- do not trigger `app` script to run as you are typing `a-p-p-s..`
- do not trigger `app` to run with `store` as a parameter
- still be able to run `app` without any args to show all results
 

Here's another test workflow to play around with this:

https://github.com/luckman212/alfredworkflows/raw/master/sftest2.alfredworkflow

 

In general, I don't understand why enabling the checkbox "with space" would not prevent Alfred from running the script UNTIL a space is typed. This seems wholly counterintuitive.

 

@Andrew regarding what you said above:

Quote

You have "Argument Optional" which means it'll run as soon as there is a keyword match, regardless of the "with space" option.

would you consider changing this behavior, or at least making a global option to NOT run the scripts until the user types the space? For me, some of my scripts return lots of results, so what ends up happening is the results I'm looking for get "pushed down" or cluttered with unintended Script Filter results. 

 

(Not to mention, sometimes there are wasted API calls because scripts are firing when they shouldn't be)

Edited by luckman212
Link to comment
3 hours ago, luckman212 said:

In general, I don't understand why enabling the checkbox "with space" would not prevent Alfred from running the script UNTIL a space is typed.

 

Because the argument is set to optional. Thus requiring the space would be contradictory and confusing. With the suggestion above of disabling the space and making the argument required you can control the exact behaviour in the code. Make it terminate early if it doesn’t start with a space and strip it if it does:

 

if not sys.argv[1].startswith(' '):
  exit(0)

if len(sys.argv[1]) > 1:
  a = sys.argv[1].lstrip()
else:
  a = '(nothing)'

 

You seem to be thinking of it in reverse. with space is the default option, meaning the special case is when it’s disabled not when it’s enabled. It controls whether a space makes the Script Filter trigger again: if you have the option off and type app then a space, it triggers twice; if you have the option on it only fires once by ignoring the space.

 

Think of disabling with space as a type of snippet prefix for Script Filters. It allows for the special case of (for example) a single Script Filter with the .sf3 keyword which then parses a b c d in the code itself to show different results. Or having the same Script Filter fire for app with a certain set of results but be different when you append store. Thus allowing (in the same script) for app and appstore rather than app and app store.

Link to comment

Ok thanks @vitor I need to spend more time wrapping my head around this. I can't believe I've been using Alfred for 5 years now and just now coming to understand how this worked. Guess I never paid much attention to it.

 

Still think it would be very nice to have a global option (even a hidden defaults write com.runningwithcrayons.Alfred alternate_withspace_handling -bool TRUE would be fine)

Link to comment

The keyword -> space -> argumenthandling is a standard paradigm across Alfred's entire usage, and has been since inception; Think web searches, the open keyword, or using spell.

 

The Script Filter "with space" option allows you to alter this behaviour, and make the space significant in the processing of the typed argument. Think of this option as relating to the argument, not the keyword.

 

In the case of "with space" / "arg optional", you get the following (by design) matching with keyword "key":

 

key   : matches and script is run
key␣  : matches but script is NOT re-run
key␣a : matches and script is run with argument a

... also ...

keya  : does NOT match

 

In the case of "no space" /  "arg optional", you get the following (by design) matching with keyword "key":

 

key   : matches and script is run
key␣  : matches and script is run with argument ␣
key␣a : matches and script is run with argument ␣a

... also ...

keya  : matches and script is run with argument a

 

If you change the above two examples to "arg required", then the script is only run when there is an argument, and as you can see in the second example, the ␣ is being treated as an argument.

 

As this hopefully clarifies, the "with space" option is just giving more power and flexibility to you, the developer.

 

I won't be adding a defaults write to change this framework level behaviour, as it would almost certainly break workflows which leverage this intrinsic behaviour.

 

I'm going to change the title of this and move it to workflow help.

 

Cheers,

Andrew

Link to comment
  • Andrew changed the title to Help understanding the "with space" option in the Script Filter object

Alright, thank you for the help & explanation @Andrew and @vitor

 

I understand now.

 

The only suggestion I might leave you with is this:

 

If the "with space" option relates to the arg rather than the keyword, I think it would make more sense to place the checkbox to the right of the popup. So it would read (left to right) in a way that better describes the behaviour.

 

E.g. "Keyword totp — argument required with space"

image.thumb.png.28f1f010cd7e5b0f8b7cf03207b4b777.png

 

Instead of "Keyword totp with space, argument required"

screenshot_sHHGbfeB.thumb.png.ff30859e318facb2b8f03e587e75b41b.png

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