Jump to content

Q How to pass OS X clipboard to PHP script in workflow

Recommended Posts



Today I was trying to create simple (I thought :-) ) workflow to convert new line in text into html <br /> tag. 

So I created workflow with keyword (no argument required because I assumed that I will always copy to clipboard text to convert before will use workflow). 

Then I added Run script (change language to /usr/bin/php) and in script put: 

$ciag = {clipboard};

$ciag = nl2br($ciag);
echo $ciag;
Finally I added Output Copy to Clipboard.
Of course it is not working because can not recognize {clipboard} variable. I receive "Parse error: parse error in Command line code on line 1" error. If I replace {clipboard} with some string in double quotes it working OK but can not find way to pass my OSX clipboard to this PHP script (to be precise to $ciag variable). 
I was spend 20 minutes googling and can't find answer. Can anybody help me?
Link to comment

'pbpaste' is the command-line command that gets the text on the clipboard and outputs it.


That said, if you want to act on the currently selected text w/o using pbpaste, you have to create a workflow triggered by a keyboard shortcut and select the argument as the selected text or the contents of the clipboard.


The past explanation is that once Alfred displays the input box it becomes the selected application and no longer has access to the previous application's selected text. It's possible this functionality will evolve though.

Link to comment

Thank you for answer but it seems like 'pbpaste' not paste clipboard text from clipboard to PHP variable i.e.:


$ciag = 'pbpaste';


is not working. 

I think it is OS X Terminal commands but thanks second part of your post I found solution and my workflow is now working OK. 


I created workflow triggered by a keyboard shortcut and selected the argument as the selected text. Then using Run script (PHP):

$ciag = '{query}';
$ciag = nl2br($ciag);
echo $ciag;
Next Output "Copy to Clipboard" And it working :-)
Now I'm wondering why when I replaced script in my original workflow for this new one it not working. It just paste copied text but not replacing new lines to <br />. Is any difference between using keyword trigger, copy text and then use '{query}' in script versa trigger by a keyboard and selected the argument as a selected text (or contents of the clipboard) and then use '{query}' in script? It must be because first version (with keyword trigger) is not working :-(
Edited by ArekS
Link to comment

To run pbpaste, you have to use back ticks `, not normal apostraphes. Therefore, in bash you would




echo $clip


Or, do whatever you want with the clip contents. A single quote (or apostraphe) tells bash it is a string, while back ticks tell bash to execute the command inside and return to the variable.

Link to comment
  • 2 weeks later...

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