Jump to content

[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?

 

Link to comment
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.

Link to comment
  • vitor changed the title to [SOLVED] Script Filter > Choose from recent clipboard history items

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