PHP Classes
PHP Classes
elePHPant
Icontem

How to Automate Actions on Apple Mac OS X using a PHP Shell Script for Automator

Recommend this page to a friend!
  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog How to Automate Actio...   Post a comment Post a comment   See comments See comments (2)   Trackbacks (0)  

Author:

Updated on: 2016-06-08

Posted on:

Categories: PHP Tutorials

Automator is an applications for Apple's Mac OS X that can automate tasks using scripts for performing many types of automated routines.

Automator can be configured to automate tasks using scripts of several possible languages.

Read this article to learn how to automate tasks with Automator using PHP as scripting language to implement those tasks.


 



Contents

Introduction

Modifying Shells.plist to support PHP

Property List Editor

What Your new Shell Script Action Will look like when We Are Done

Conclusion


Introduction

This article with build upon the work of a blog post written by Patrick Patoray. Here have further added more details and explanations based on my research and experience with PHP and Mac OS X.

I would like to thank Patrick for his efforts and InSha'Allah this will serve as a good starting point for using PHP with Apple's Automator and the other actions that are available with Mac OS X.

Modifying Shells.plist to support PHP

In the original article he only demonstrated how to modify the Shells.plist file in the 'Automator Run Shell Script.action' package so that /usr/bin/php is one of the shell scripting languages listed.

I am going to give my own input on how to make that old PowerPC (G4/G5) Mac (or vintage/obsolete Intel Mac) you have in your garage, or basement, or closet collecting dust useful again with PHP.

Do want PHP 7 on that PowerPC Mac (or vintage/obsolete Intel Mac)? With MacPorts you can install any PHP version from 4.4.9 to 7.0.5 on Mac OS X Leopard and up.

This hack of the Shells.plist on Mac OS X Leopard or Mac OS X El Capitan is the same, but you will defiantly notice a difference between actions available on PowerPC and Intel for some of the installed apps you have, like Microsoft Office 2004 for Mac.

Some of the links I have provided bellow also have custom actions available for download and other goodies that let you do things with Apple's Automator that you can't do easily with pure php.

Let's get started. The file we will need to modify is in PLIST XML format and it can be easily edited with the Properties List Editor that comes with Xcode. First you should open up the Terminal.app in /Application/Utilities/ and enter the following:

 
 $ sudo chmod -R 777 /System/Library/Automator/Run\ Shell\ Script.action
 $ open /System/Library/Automator/Run\ Shell\ Script.action/Contents/Resources/Shells.plist

This will give everybody write access to this file and open the plist file in it's default editor. You will need to have Xcode installed on your system to use the Properties List Editor.

The reason I am showing you the Properties List Editor over the manual way is because it easily edits the PLIST format and I wanted to show you how to do this incase your PLIST file is encoded in a way that vim or other editors can not read without converting the file.

I remember seeing something like this in the past, but both my Leopard and El Capitan systems have this PLIST file as plain text XML.

If you feel adventurous enough, you can manually edit the PLIST with vim or another text editor, follow Patrick's example of how to edit the XML and you will not need Xcode.

Just remember that you will need to use special marks for a new line [ 
] and for tab [	 ] when editing the files manually to get the formatting the way you want for your code template. Otherwise you can use control + enter for a newline and option + tab for tab in the Properties List Editor.

You start by clicking on Root, then click 'Add Child'; Type in your PHP executable path as the key name:

Mac OS X System Default: /usr/bin/php

MAMP 1.9 PPC: /Applications/MAMP/bin/php5.3/bin/php

MAMP 3.0: /Applications/MAMP/bin/php/php7.0.0/bin/php

MacPorts: /opt/local/bin/php70

Then change the type to dict. Next you will need click the arrow beside the key you just added and make sure it is highlighted then click 'Add Item' to add two array elements, one called args and the other is called script.

Expand the args array and add 2 items to that, the first item will contain '-r' and the second item will contain '%'. These are the arguments that will be used by your selected PHP executable. Other languages could also be added, just read their man page and other docs for switched used to execute code on the command line. The '-r' (or --run) option is for running code without the need for '<?php ?>' tags and the '%' is a place holder for your php code that you type in the Automator.app Shell Script Action.

Next you should expand the script array and add two items with 'Add Item'. The first Item is the script for handling standard input (STDIN), just like you were directing input from a shell command via pipe (|), or input file redirect (<).

The second item is for handling arguments passed after the command; eg: php cmd.php 'arg1' 'arg 2' 'arg#3'. Based on the original plist file, there appears to only be 2 string items in the 'script' array for the 'Pass input' dropdown menu in the Automator Shell Script Action; first 'to stdin' and second 'as arguments'.

I did something different than Patrick for the STDIN processing. He used a one liner using those entity values I mentioned above.

I am going to do a little more to process the STDIN input by default so it is there by default. stream_set_blocking is set to false for STDIN because the data will be available right away from the previous action so we do not need to wait for input when we use fgets().

stream_set_timeout() is set to 1 sec for STDIN, again so we do not have to wait for more input that wont come. Next the STDIN will be processed with a while loop, line by line with fgets() and it will create an Array and a String variables that you can use after in your custom action.

Please enter the following in the first item of the script array, using control + enter for newline and option + tab for tab:

 
 stream_set_blocking(STDIN, 0);
 stream_set_timeout(STDIN, 1);
 $stdin='';
 $input=array();
 while (FALSE !== ($line = fgets(STDIN))){
    $input[] = trim($line);
    $stdin = $stdin . $line;
 }
 echo $stdin; // optional, just a place holder for now

The second item is the script array will deal with the arguments that were passed to your script from the previous action, this will loop through the values in the $argv array for those items that were passed as arguments. The array_shift($argv) will drop the '-' which is for the first value representing the script (this would be the script name you executed with php 'script'). Please enter the following:

 
 array_shift($argv);
 foreach ($argv as $arg) {
    echo "$arg \n";
 }

Property List Editor

Once you are done with your modifications you can use Disk Utility to repair your permissions to revert the package back to their original permissions; the current version of OS X (10.11) will need to use a command line utility: How to Verify/Repair Permissions Mac OS X. This is the command to fix the permissions on Mac OS X El Capitan (10.11):

 
 $ sudo /usr/libexec/repair_packages --repair --standard-pkgs --volume /

What Your new Shell Script Action Will look like when We Are Done

screen shot of php in the Automator

This is just the starting point of adding the power of PHP into Apple's Automator tool. If you ever get a chance to play around with the tool you will see all the cool things you can do with it, such as database backup to SQLite. There are built in actions that use SQLite DBs. Just take a look at all the classes that are available on this site (phpclasses.org) and you can incorporate them into your work flow.

Do not forget to consult the documentation at php.net for more detailed information on the functions I used to process the input, they should all be available for PHP 5 and 7 for sure.

Also take a look at set_include_path() for specifying a directory for with the classes you would like to use with your script action.

I do plan on covering this topic further by using Xcode to create shell script actions for Apple's Automator using PHP as well as a class I'm working on to simplify the process. You can think of it as adding a Mac GUI interface (Cocoa) to a PHP shell script.

Take a look at VLC-cutter on my GitHub for an example of an Automator Shell Script action, this is written in Bash Shell.

Conclusion

In conclusion you can use this modification on your vintage obsolete Mac and make it useful again especially if you still have one of those PowerPC Macs running Mac OS X Leopard (10.5).

I would advise against using this on El Capitan (10.11) because you could lose this modification when you update your system. Systems that are at their final maximum version are ideal for this hack modification (i.e. Mac OS X 10.5 to 10.9 for sure, maybe 10.10).

Keep in mind that you will also need to modify each system's Shells.plist you want to run these PHP workflows on. Patrick Patoray's blog post is still useful if you want to know how to make these changes manually and not use Xcode nor change the permissions of the package (such as: sudo vi <file>).

More information on Apple's Automator can be found in a book call "Apple Automator with Apple Script Bible" by Thomas Myer (O'Reilly should have it available in multiple formats, like ePub and PDF), Automator.us and MacOSXAutomation.com.

If you liked this article please use the links at the top of this page to share it with your PHP Mac user friends in Twitter, Facebook, etc..

If you questions about any details of adding PHP power scripts to Automator, just post a comment here.




You need to be a registered user or login to post a comment

1,532,477 PHP developers registered to the PHP Classes site.
Be One of Us!

Login Immediately with your account on:

FacebookGmail
HotmailStackOverflow
GitHubYahoo


Comments:

1. Heriyadi Setiana - Heriyadi Setiana (2016-06-08 23:27)
-... - 1 reply
Read the whole comment and replies



  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog How to Automate Actio...   Post a comment Post a comment   See comments See comments (2)   Trackbacks (0)