Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I execute a PHP shell script as an Automator action on Mac OS X

I'm tempted by Automator.app's ability to create contextual services in Mac OS X Snow Leopard. I would like to create some keyboard accessible shortcuts to manipulate text snippets by calling a shell script. However, Automator only suggests bash, Perl, Python and Ruby (among others) to allow this. But, since PHP also ships with Mac OS (and, to be honest, it's the only scripting language I fully master), I wonder why I can't run a PHP shell script.

like image 336
Roel Avatar asked Sep 09 '09 09:09

Roel


People also ask

How do I run a shell script on a Mac?

Open Terminal, type in sh /path/to/file and press enter. Faster is to type sh and a space and then drag the file to the window and release the icon anywhere on the window.

How do you automate actions on a Mac?

Simply open Automator, select New from the File menu, then select Application as the type of workflow to create, and click the Choose button. Next, click the Record button in the top right corner, and then perform the task you wish to automate. Automator will try to create a duplicate of your workflow for you.

Do shell scripts work on Mac?

Instead of entering commands and waiting for a response, you can compose shell scripts that run without direct interaction. A shell script is a text file that contains one or more UNIX commands.


2 Answers

dbr's solution above is excellent, but I found that it didn't work for me, perhaps due to a later change in the shell, php, or OS version (I'm using OS X 10.8). After a lot of head-scratching I found that the answer is to quote the heredoc marker.

A useful addition is to use the appropriate syntax to pass the arguments through to the php script. Putting all that together, I'm using:

php -- "$@" <<'EOF'
<?php 
print_r( $argv );
?>
EOF
like image 187
Robert Sinton Avatar answered Nov 10 '22 13:11

Robert Sinton


The list of shells that Automator supports is in

 /System/Library/Automator/Run\ Shell\ Script.action/Contents/Resources/Shells.plist

You can follow this guy's instructions to add an entry for /usr/bin/php. You may want to copy the .action file to ~/Library/Automator first, and work on the copy instead.

Of course, it's probably easier to just wrap it in a bash script, as others have suggested.

like image 40
JW. Avatar answered Nov 10 '22 13:11

JW.