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

前端 未结 5 1087
时光说笑
时光说笑 2020-12-25 08:54

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

相关标签:
5条回答
  • 2020-12-25 09:04

    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.

    0 讨论(0)
  • 2020-12-25 09:06

    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
    
    0 讨论(0)
  • 2020-12-25 09:07

    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)

    0 讨论(0)
  • 2020-12-25 09:11

    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.

    0 讨论(0)
  • 2020-12-25 09:21

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

    0 讨论(0)
提交回复
热议问题