bencfd Posted March 28, 2013 Share Posted March 28, 2013 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 ). You can download my draft workflow here: http://d.pr/f/3BPN 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! Link to comment
jdfwarrior Posted March 28, 2013 Share Posted March 28, 2013 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. Link to comment
bencfd Posted March 28, 2013 Author Share Posted March 28, 2013 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 ); Link to comment
bencfd Posted March 29, 2013 Author Share Posted March 29, 2013 Ok, I finally managed to get it work. I'm afraid it doesn't follow the Workflow Best Practices, but... Here is a link: http://d.pr/f/SJjW 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? Link to comment
jdfwarrior Posted March 30, 2013 Share Posted March 30, 2013 Ok, I finally managed to get it work. I'm afraid it doesn't follow the Workflow Best Practices, but... Here is a link: http://d.pr/f/SJjW 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.. Link to comment
bencfd Posted April 1, 2013 Author Share Posted April 1, 2013 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. Link to comment
jdfwarrior Posted April 1, 2013 Share Posted April 1, 2013 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. Link to comment
bencfd Posted April 2, 2013 Author Share Posted April 2, 2013 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. Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now