简体   繁体   中英

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.

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

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.

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:

对PHP代码使用“执行shell脚本”操作

(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)

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.

Automator有一个“运行Shell脚本”操作只需将其添加到您的工作流程并按照左下角的说明操作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM