PHP Calls macOS Apple Script Primer Tutorial

PHP Calls macOS Apple Script Primer Tutorial

PHP Calls macOS Apple Script Primer Tutorial

Yesterday’s Apple Script Execution of Start Word Suggestions for Wordle Tutorial struck a chord of interest for us. Its use of …

  • macOS command line via Terminal application …
  • Apple Scripting … and …
  • “open” command on that command line

    … opens the door to an idea we’ve wondered about before … perhaps involve multiple web browser “brands” in a single …

    • web application
    • command line application
    • curl web application

    … using … you guessed it … PHP in (what we see as) its 3 modes of use.

    Here’s our first draft of apple_script_url.php we want you to download to (the Document Root of) a macOS MAMP local Apache/PHP/MySql web server environment, ideally, though you can tailor for another macOS local web server arrangement, perhaps …


    <?php
    // apple_script_url.php
    // RJM Programming
    // February 2022

    $ts=date('_dmY_his', time());

    if (isset($argc)) {
    if (PHP_OS == "Darwin") {
    if ($argc >= 2) {
    if (file_exists(getenv('HOME') . '/Desktop/MyAppleScript' . $ts . '.scpt')) {
    unlink(getenv('HOME') . '/Desktop/MyAppleScript' . $ts . '.scpt');
    }
    file_put_contents(getenv('HOME') . '/Desktop/MyAppleScript' . $ts . '.scpt', "tell application \"Terminal\"\n activate\n do script \"cd '" . getenv('HOME') . '/Desktop' . "'\" in window 1\n do script \"open '" . $argv[1] . "'\" in window 1\nend tell");
    exec("cd " . getenv('HOME') . '/Desktop ; open MyAppleScript' . $ts . '.scpt');
    }
    }
    } else if (isset($_GET['url'])) {
    if (PHP_OS == "Darwin") {
    if (file_exists(getenv('HOME') . '/Desktop/MyAppleScript' . $ts . '.scpt')) {
    unlink(getenv('HOME') . '/Desktop/MyAppleScript' . $ts . '.scpt');
    }
    file_put_contents(getenv('HOME') . '/Desktop/MyAppleScript' . $ts . '.scpt', "tell application \"Terminal\"\n activate\n do script \"cd '" . getenv('HOME') . '/Desktop' . "'\" in window 1\n do script \"open '" . str_replace('+','%20',$_GET['url']) . "'\" in window 1\nend tell");
    //exec('open \"' . getenv('HOME') . '/Desktop/MyAppleScript' . $ts . '.scpt\"');
    passthru("cd " . getenv('HOME') . '/Desktop ; open MyAppleScript' . $ts . '.scpt');
    } else if (strpos($_SERVER['SERVER_NAME'], 'localhost') === false) {
    echo "<html><body onload=\" window.open('HTTP://localhost:8888/apple_script_url.php?url=" . str_replace('+','%20',$_GET['url']) . "','_self'); \"></body></html>";
    }
    }
    ?>

    … and if you have downloaded, you may get some joy with it calling Google into a new web browser arrangement via the pressing of an Apple Script window Play button … or … get to the macOS Terminal prompt and type …


    php /Applications/MAMP/htdocs/apple_script_url.php https://google.com

    … or …


    curl "HTTP://localhost:8888/apple_script_url.php?url=HTTPs://google.com"

    If this was interesting you may be interested in this too.

This entry was posted in eLearning, Event-Driven Programming, Tutorials and tagged , , , , , , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>