问题
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.
回答1:
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.
回答2:
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
回答3:
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:
(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)
回答4:
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.
回答5:
Automator has a "Run Shell Script" Action just add it to your workflow and follow the instructions on the bottom left.
来源:https://stackoverflow.com/questions/1398591/how-do-i-execute-a-php-shell-script-as-an-automator-action-on-mac-os-x