<?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 if (PHP_OS =='WINNT' || PHP_OS =='WIN32' || PHP_OS =='Windows') {
  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 . " | clip & " . $verb);
    }
    } else {
    $res=shell_exec("dir " . $verb . " | clip & dir " . $verb);
    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 . " | clip & " . $verb);
    }
  } else {
    $res=shell_exec($verb . " | clip & " . $verb);
    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'/>
<title>Interfacing to macOS pbcopy paste and copy or Windows clip into clipboard - RJM Programming - December, 2025</title>
<scri" . "pt type=text/javascript>
  var wothree='top=50,left=50,height=600,width=600';
  var rectthree=null, wotest=null;
  var isSafari = /Safari/.test(navigator.userAgent) && /Apple Computer/.test(navigator.vendor);
  var wolab=(isSafari ? '_blank' : 'blankif');
 
  function onl() {
    document.getElementById('verb').placeholder='ls -clt ~/Documents ~/Desktop ~/Downloads';
    if (('' + navigator.platform).toLowerCase().indexOf('win') == 0) {
      document.getElementById('myform').action=document.getElementById('myform').action.replace(':8888','');
      document.getElementById('myspan').title=document.getElementById('myspan').title.replace(' macOS ',' Windows ');
      document.getElementById('verb').placeholder='dir /c/l/t Documents Desktop Downloads';
    }
  }
</scri" . "pt>
</head>
<body onload=\" onl(); rectthree=document.getElementById('blankif').getBoundingClientRect();  wothree='top=340,left=' + rectthree.left + ',height=' + eval(-160 + eval('' + screen.height) - eval('' + rectthree.top)) + ',width=' + rectthree.width; if (('' + navigator.platform).toLowerCase().indexOf('mac') != 0 && ('' + navigator.platform).toLowerCase().indexOf('win') != 0) { document.getElementById('blankif').style.visibility='visible'; document.getElementById('blankif').srcdoc='<p>Sorry, no macOS nor Windows system detected.</p>';  }\">
<h1>Interfacing to macOS pbcopy paste and copy or Windows clip 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; } wotest=window.open(this.action + '?verb=' + encodeURIComponent(document.getElementById('verb').value) + '&passXthru=y', wolab, wothree); if (!wotest && isSafari) { this.target='_blank'; return true; } else {  return false; }\" method=GET id=myform action='HTTP://localhost:8888/pbcopy_ideas.php'>
<span id=myspan>Command to pbcopy paste and copy or Windows clip 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=''></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>";
 }
?>
