Jump to content

Capture Query on Alfred Dismiss?


Recommended Posts

Is there any way to capture the text entered into Alfred if Alfred is "dismissed" rather than pressing enter, tab, etc.? That is, I have a workflow for entering something like the name of a new file, the name of a new folder, and while the optimal process is that this text gets typed in and then I press enter, sometimes the Alfred window gets dismissed by clicking away or the like.

 

I'm imagining something like, for a specific workflow, whatever gets entered is updated to an environmental variable somehow, so that even if the Alfred window is dimissed (or when it's dismissed), the text that's entered is saved and isn't lost. I looked around and I couldn't see any way to accomplish this, and I recognize again this is kind of contrary to Alfred's core behaviors. Anyone know a way to do what I'm describing?

 

Thanks!

Link to comment
Posted (edited)
On 4/30/2022 at 6:44 AM, vitor said:

Is what you’re looking for Alfred Preferences → Advanced → History (both checkboxes)?

 

Thanks for your response! That's a useful feature but not quite what I'm talking about, because that only seems to save the initial query typed into Alfred and not (if I'm understanding correctly) the second or third query that might be typed as part of a workflow. Here's a link to a sample workflow — basically, a list filter with three options, two that send pre-established text to Large Type and one that further prompts you to enter new text before sending it to Large Type. It's that step I'm trying to capture — if the user is entering text as part of a second keyword box in Alfred, is there any way to capture that text if Alfred is dismissed (ESC instead of Enter)?

 

https://drive.google.com/file/d/1E41suUeaqf1YXvD0Ez_Wvt9UhckGL7PL/view?usp=drivesdk

 

Now that I'm typing it, I'm wondering if the first workflow included an external trigger to a second workflow, if that would return Alfred to the "first" position where the text is captured automatically ...? Update: Unless I'm doing it wrong, that solution does not work; Alfred's history retains the original keyword trigger but not anything entered after that and nothing from the workflow run via External Trigger.

Edited by brian_seidman
Update
Link to comment
Posted (edited)

What you can do is replace both the List Filter and Keyword Input with Script Filters.


One way is to have each be a separate Script Filter: the second one writes its own arguments to a file and displays a single option to continue, like a Keyword Input. When you action it, it delete the file. Then the first one checks for the file’s existence, and if it does exist shows something for you to continue.


A better way is to combine both into a single Script Filter, and have it interpret its own argument by using a certain character as breadcrumbs. Your Workflow can be collapsed into two objects: one Script Filter connected to one Large Type. That way, the History feature will get you your complete argument even if you dismissed Alfred’s search box. Here’s the Workflow’s code (Zsh):

# If argument does NOT have a "→" in it, we are on the first step
if [[ "${1}" != *'→'* ]]; then
 
  # Note how "Message" has:
  #   "valid":false which will make it so when you ↵, the Workflow won’t continue.
  #   "autocomplete" which will write that text into Alfred's box, making it a new argument
cat <<JSON
    { "items": [
      {
        "title": "Message",
        "subtitle": "Create a new message to show in large text.",
        "autocomplete": "Message → ",
        "valid": false
      },
      {
        "title": "Message 1",
        "subtitle": "This shows \"Hello world!\" in large text.",
        "arg": "Hello world!"
      },
      {
        "title": "Message 2",
        "subtitle": "This shows "Good-bye!" in large text.",
        "arg": "Good-bye!"
      }
    ]}
JSON
 
else # We are on the second step
 
  # Remove all text from input until the last "→ " (note the space)
  # This is because we only care about that part of the text
  # This syntax is terse but there are many other more readable ways to do it
  # Particularly when using other languages
  readonly comment="${1#*→ }"
 
cat <<JSON
    { "items": [
      {
        "title": "New Comment",
        "subtitle": "Enter new comment text",
        "arg": "${comment}"
      }
    ]}
JSON
 
fi
Edited by vitor
Link to comment

vitor, thank you — that worked perfectly! I actually trigger the workflow with a hotkey, and as I was updating it, I thought I ran into a problem where the Script Filter solution wouldn't work when it was the second step behind a hotkey. But, I changed the hotkey setting from "Pass through to workflow" to "Show Alfred" and now it's working as expected. Thanks again!

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