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

隐身守侯 提交于 2019-11-30 00:47:12

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.

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

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)

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 has a "Run Shell Script" Action just add it to your workflow and follow the instructions on the bottom left.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!