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

5

10

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.

Disestablish answered 9/9, 2009 at 9:40 Comment(0)
G
7

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
Gaidano answered 27/10, 2012 at 22:14 Comment(1)
To make this work for python, using your env's default interpreter, change above to a single hyphen: python - "$@" <<'EOF'Bili
F
6

This is just a hack, but how about creating a python, or ruby or perl or bash script that calls php command-line interpreter with the php script you want to execute?

For instance in bash would be as simple as this:

#!/bin/bash   
php myscript.php

Now save it, give it permission for execution:

chmod +x my_bash_script.sh

And voilá. Automator can now run the bash script, which calls the php script.

Flannelette answered 9/9, 2009 at 9:54 Comment(0)
S
4

You can use the "Run Shell Script" action to run a PHP script. One self-contained way is to use <<EOF ... EOF to pass the script to the php command:

Using "execute shell script" action for PHP code

(I would strongly recommend learning Python, Ruby or Perl.. PHP does has some advantages for web-programming, but is just not intended for command-line scripting.. It's certainly doable, it'll just not be nearly as.. pleasant)

Seel answered 25/10, 2009 at 16:12 Comment(0)
C
3

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.

Cystoscope answered 25/10, 2009 at 16:30 Comment(0)
I
1

Automator has a "Run Shell Script" Action just add it to your workflow and follow the instructions on the bottom left.

Infertile answered 9/9, 2009 at 11:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.