# Chained script filter workflow to paste LaTeX commands

## Recommended Posts

Hi there!

Following help from our Guru David, I'm trying to set a chained script filter workflow to paste a text string, chosen from a feedback list. My purpose is to get my personal shortcuts for TeXShop, a TeX editor, working from Alfred. However, there are few points I can't figure out (my competence stop with AppleScript  ).

The first step saves the OS X selection. The second step can be invoked by another hotkey, in case I don't select any text. The script filter in this step provides the text strings that I'm susceptible to need. E.g., \emph{}, \ref{} (minimal example, I'll set many more later).

I would like that the selected text (saved in a .txt file at step 1) comes between the brackets of the commands, like \emph{Previously selected text}. Or nothing if I haven't selected any text.

Also, I would like the second step not to show all the items from the script filter, but rather those who match what I will type in Alfred.

Does one of you know how to fix that? Any help will be much appreciated!

I'll keep the answer simple for now and if you need more I can elaborate further.

What you would need to do is, in the second step (script filter), when generating results, first read the previous input from the user. So grab the contents of your text file, store that in a variable for use. Then, probably have an array of all of the potential options the user would select from, and filter those down based on whether or not they match up with what the user is entering in this step. For each of those results you're generating, set the argument to the command + the previous input. So.. arg="\emph{$previnput}" where$previnput would be the value read from the file from the previous step.

Thank you David! The $previnput fix was obvious actually. However, how can I prevent it to add backslash in case my selection contains spaces? Then, about the array. I understand what you mean and I looked into some workflows to see how it works, specially your Chrome bookmarks workflow. I removed the echo, since I think it has to be part of the filter. Here's the new script: previnput=$(cat saved.txt)

<?xml version="1.0"?>
<items>
<item uid="emphasize" arg="'\\emph{$previnput}'"> <title>'emphasize'</title> <icon>icon.png</icon> <subtitle>\emph{}</subtitle> </item> <item uid="reference" arg="'\\ref{$previnput}'">
<title>'reference'</title>
<icon>icon.png</icon>
<subtitle>\ref{}</subtitle>
</item>
</items>


But then, I have no clue... Maybe something like that?

$input = {query}$options = array ( 'emphasize' , 'reference' ) ;

$results = array(); foreach($options as $input ):$item = array(

// no idea how to code

);
array_push( $results,$item );
endforeach;

// echo arrayToXML( \$results );


Ok, I finally managed to get it work. I'm afraid it doesn't follow the Workflow Best Practices, but...

However, the code or Alfred (I don't know) adds an extra carriage return to the text to paste. Any idea where does it come from?

Ok, I finally managed to get it work. I'm afraid it doesn't follow the Workflow Best Practices, but...

However, the code or Alfred (I don't know) adds an extra carriage return to the text to paste. Any idea where does it come from?

The PHP you pasted as an example was from the original PHP class I had created. That has been significantly updated and is documented on my blog. Check out http://dferg.us/workflows-class/. That will provide you with all the information you need for using it. It should simplify a few things..

I definitely want to use this approach. However, just trying to put the php code in a external file (in the workflow folder) makes my creepy-but-functional workflow ineffective. In the script filter settings I chose the language bin/bash and wrote:

php -f texshop.php -- "{query}"


and it doesn't work anymore. I'm already stuck at this very first step.

I definitely want to use this approach. However, just trying to put the php code in a external file (in the workflow folder) makes my creepy-but-functional workflow ineffective. In the script filter settings I chose the language bin/bash and wrote:

php -f texshop.php -- "{query}"


and it doesn't work anymore. I'm already stuck at this very first step.

If you aren't getting results, I would verify that you are getting valid XML or that you don't have some other kind of errors or something that are being thrown. Drop to a terminal in the workflow folder and run the script from there. If ANYTHING is shown other than the XML, then Alfred won't show the results. So, any errors, warnings, etc.

I started to simplify the script with your read and toxml functions. The script still works well when it is directly written in the Alfred Script Filter, but (exactly the same script wrapped in <?php / ?>) doesn't work from an external file (still the same bash command than previously posted).

Also, running the script from Terminal returned nothing, neither the XML nor an error.