Jump to content
luckman212

[SOLVED] Script Filter > Choose from recent clipboard history items

Recommended Posts

I'm trying to create a script filter that presents the last 3 items from the clipboard history and takes an action on them. E.g. {clipboard:0} {clipboard:1} {clipboard:2} etc.


My language is bash for now (yes I know I should be using a real language, but this is just for POC)

Here's what I have

printf '%s\n' "<?xml version=\"1.0\"?>"
printf '%s\n' "<output>"
printf '%s\n' "<items>"

printf '%s\n' "<item arg=\"{clipboard:0}\" valid=\"YES\">"
printf '%s\n' "<title>{clipboard:0}</title>"
printf '%s\n' "</item>"

printf '%s\n' "<item arg=\"{clipboard:1}\" valid=\"YES\">"
printf '%s\n' "<title>{clipboard:1}</title>"
printf '%s\n' "</item>"

printf '%s\n' "<item arg=\"{clipboard:2}\" valid=\"YES\">"
printf '%s\n' "<title>{clipboard:2}</title>"
printf '%s\n' "</item>"

printf '%s\n' "</items>"
printf '%s\n' "</output>"

This does not work. It simply displays "{clipboard:0}" etc verbatim.

 

Is there a way to access Alfred internal vars and special placeholders from within a Script Filter?

 

Share this post


Link to post
16 hours ago, luckman212 said:

My language is bash for now (yes I know I should be using a real language, but this is just for POC)

 

There is plenty wrong with Bash, but nothing wrong with you using it. It’s an idiosyncratic but capable glue language that I still use regularly. You can find it pretty much everywhere and be sure it will work with some consistency.


But the way you’re writing it could indeed be improved upon. It makes little sense to keep escaping double quotes when you can use single quotes. Also, there’s no need for printf '%s\n'  or so many of them. A single echo (or a Here Document) suffices.


Your code isn’t working because Dynamic Placeholders aren‘t meant to be used in Alfred’s non-code areas. It wouldn’t be wise for Alfred to replace them everywhere and risk breaking your code. As a workaround, you may connect a Keyword Input to an Argument and Variables Utility that saves the clipboard contents as variables to be used in the Script Filter.


Be aware that there is bound to be some breakage when the raw clipboard contents interact with the code, such as when you have a quotation mark in the clipboard. That is why raw Bash is not a good choice in this case — you need to sanitise your input beforehand, and libraries are good for that.


Here’s a ready-made Workflow. I’ve kept and fixed the Bash example so you can still reference how your code could have been, but the end result uses Ruby.


Finally I’ll ask that in the future, when asking for help with a Workflow, please upload it somewhere as it’s hard to help without access to it. Debugging can already be hard with access to the code, even harder to do so by guessing from a description. There are multiple places where the code or Workflow setup can be wrong. Without looking at it we’re mostly shooting in the dark.


The Reporting Problems with Workflows topic gives a nice overview on how to build an effective report.

Share this post


Link to post

@vitor Wow thank you for this guidance! I understand about the danger of unescaped vars now. Your workflow works great. I will study it and try to educate myself. Thanks for the tip about the heredoc too. I actually knew about those but for some reason I forget to use them. :wacko:

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