<?php
 // pbcopy_ideas.php
 // RJM Programming
 // December, 2025
 // Use macOS pbcopy to copy command line results into buffer
 $verb="uuidgen";
 if (isset($_GET['verb'])) {
   $verb=str_replace('+',' ',urldecode($_GET['verb']));
 } else if (isset($_POST['verb'])) {
   $verb=str_replace('+',' ',urldecode($_POST['verb']));
 }
 if (trim($verb) == '') { $verb="uuidgen"; }
 if (PHP_OS == 'Darwin') {
  if (strpos($verb, ' -') === false) {
    if (isset($_GET['passthru']) || isset($_POST['passthru'])) {
    if (strpos(('~' . $verb . ' '), '~say ') !== false) {
    exec(str_replace('saJUNKy ','say -i --progress ',$verb));
    exit;
    } else {
    passthru($verb . " | pbcopy; pbpaste");
    }
    } else {
    $res=shell_exec("ls -clt " . $verb . " | pbcopy; pbpaste");
    echo "<html><head><meta charset='UTF-8'/></head><body><p title='This is in your clipboard now.' style='font-family:courier new;'>" . str_replace(" ", "&nbsp;", str_replace("\t", "&#9;", str_replace("\n","<br>",$res))) . "</p></body></html>";
    }
  } else if (isset($_GET['passthru']) || isset($_POST['passthru'])) {
    if (strpos(('~' . $verb . ' '), '~say ') !== false) {
    exec($verb);
    exit;
    } else {
    passthru($verb . " | pbcopy; pbpaste");
    }
  } else {
    $res=shell_exec($verb . " | pbcopy; pbpaste");
    echo "<p title='This is in your clipboard now.' style='font-family:courier new;'>" . str_replace(" ", "&nbsp;", str_replace("\t", "&#9;", str_replace("\n","<br>",$res))) . "</p>";
  }
 } else {
  //echo shell_exec($verb . " | pbcopy; pbpaste");
  echo "<html>
<head>
<meta charset='UTF-8'/>
</head>
<body onload=\"if (('' + navigator.platform).toLowerCase().indexOf('mac') != 0) { document.getElementById('blankif').style.visibility='visible'; document.getElementById('blankif').srcdoc='<p>Sorry, no macOS system detected.</p>';  }\">
<h1>Interfacing to macOS pbcopy paste and copy into clipboard</h1>
<h3>RJM Programming - December, 2025</h3>
<h4></h4>
<form target=blankif onsubmit=\"document.getElementById('blankif').style.visibility='visible'; document.getElementById('blankif').style.backgroundColor='#f0f0f0'; document.getElementById('blankif').frameborder='1'; if (document.getElementById('verb').value == '') { document.getElementById('verb').value=document.getElementById('verb').placeholder; } window.open(this.action + '?verb=' + encodeURIComponent(document.getElementById('verb').value) + '&passXthru=y', 'blankif', 'top=50,left=50,height=600,width=600');  return false;\" method=GET action='HTTP://localhost:8888/pbcopy_ideas.php'>
<span>Command to pbcopy paste and copy into clipboard </span><input title='Please be careful with your commands here applied to your own macOS operating system.' style=display:inline-block;width:40%; type=text ondblclick='this.value=this.placeholder;' name=verb id=verb value='' placeholder='ls -clt ~/Documents ~/Desktop ~/Downloads'></input><br><br>
<input type=hidden name=passXthru value=y></input>
<input type=submit value=Process style=background-color:orange;></input>
</form>  
<iframe src='/About_Us.html' id=blankif name=blankif style='font-family:courier new;visibility:hidden;width:100%;height:900px;' frameborder=0></iframe>
</body>
</html>";
 }
?>