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.
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.
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With