<?php
// osascript_it.php
// RJM Programming
// December, 2025

$retis='';

if (isset($_GET['pasXte']) || isset($_POST['pasXte'])) {
  $retis=shell_exec("osascript -e 'tell application \"System Events\"
	tell process (name of first application process whose frontmost is true)
		keystroke \"v\" using command down
	end tell
end tell
  '");
  
  exit;
} else if (isset($_GET['paste']) || isset($_POST['paste'])) {
  //sleep(1);
  
  // Thanks to https://www.google.com/search?q=macos+osascript+paste+from+clipboard+where+the+cursor+is+in+whatever+window&rlz=1C5OZZY_en&oq=macos+osascript+paste+from+clipboard+where+the+cursor+is+in+whatever+window&gs_lcrp=EgZjaHJvbWUyBggAEEUYOdIBCTQ3NDg4ajBqN6gCALACAA&sourceid=chrome&ie=UTF-8
  $retis=shell_exec("osascript -e 'tell application \"System Events\"
    # Get the name of the current application (which is likely the Script Editor or Automator)
    set currentApp to name of current application
    
    # Check if the current app is one of the script runners and hide it to allow the target app to become frontmost
    if currentApp is in {\"Script Editor\", \"Automator\", \"Script Debugger\"} then
        set visible of process currentApp to false
        # Wait a moment for the target app to become active
        delay 0.1
    end if
    
    # Simulate the Command+V keystroke in the now frontmost application
    #keystroke \"v\" using command down
    
    set theText to (the clipboard as text)
    # Insert the text into the front document at the current insertion point
    make new paragraph at end of front document with data theText & (ASCII character 10)
    
    
    # (Optional) Unhide the script application afterward
    if currentApp is in {\"Script Editor\", \"Automator\", \"Script Debugger\"} then
        set visible of process currentApp to true
    end if
end tell
  '");
  
  exit;
} else if (isset($_GET['copy']) || isset($_POST['copy'])) {
  //sleep(1);
  
  // Thanks to https://www.google.com/search?q=macos+osascript+paste+from+clipboard+where+the+cursor+is+in+whatever+window&rlz=1C5OZZY_en&oq=macos+osascript+paste+from+clipboard+where+the+cursor+is+in+whatever+window&gs_lcrp=EgZjaHJvbWUyBggAEEUYOdIBCTQ3NDg4ajBqN6gCALACAA&sourceid=chrome&ie=UTF-8
  $retis=shell_exec("osascript -e 'tell application \"System Events\"
    # Get the name of the current application (which is likely the Script Editor or Automator)
    set currentApp to name of current application
    
    # Check if the current app is one of the script runners and hide it to allow the target app to become frontmost
    if currentApp is in {\"Script Editor\", \"Automator\", \"Script Debugger\"} then
        set visible of process currentApp to false
        # Wait a moment for the target app to become active
        delay 0.1
    end if
    
    # Simulate the Command+V keystroke in the now frontmost application
    keystroke \"c\" using command down
    
    # (Optional) Unhide the script application afterward
    if currentApp is in {\"Script Editor\", \"Automator\", \"Script Debugger\"} then
        set visible of process currentApp to true
    end if
end tell
  '");
  
  exit;
} else if (isset($_GET['selectcopy']) || isset($_POST['selectcopy'])) {
  //sleep(1);
  
  // Thanks to https://www.google.com/search?q=macos+osascript+paste+from+clipboard+where+the+cursor+is+in+whatever+window&rlz=1C5OZZY_en&oq=macos+osascript+paste+from+clipboard+where+the+cursor+is+in+whatever+window&gs_lcrp=EgZjaHJvbWUyBggAEEUYOdIBCTQ3NDg4ajBqN6gCALACAA&sourceid=chrome&ie=UTF-8
  $retis=shell_exec("osascript -e 'tell application \"System Events\"
    # Get the name of the current application (which is likely the Script Editor or Automator)
    set currentApp to name of current application
    
    # Check if the current app is one of the script runners and hide it to allow the target app to become frontmost
    if currentApp is in {\"Script Editor\", \"Automator\", \"Script Debugger\"} then
        set visible of process currentApp to false
        # Wait a moment for the target app to become active
        delay 0.1
    end if
    
    # Simulate the Command+V keystroke in the now frontmost application
    keystroke \"a\" using command down
        delay 0.1
    
    # Simulate the Command+V keystroke in the now frontmost application
    keystroke \"c\" using command down
        delay 0.1
    
    # Simulate the Command+V keystroke in the now frontmost application
    keystroke \"v\" using command down
        delay 0.1
    
    # (Optional) Unhide the script application afterward
    if currentApp is in {\"Script Editor\", \"Automator\", \"Script Debugger\"} then
        set visible of process currentApp to true
    end if
end tell
  '");
  
  exit;
} else if (isset($_GET['selectall']) || isset($_POST['selectall'])) {
  //sleep(1);
  
  // Thanks to https://www.google.com/search?q=macos+osascript+paste+from+clipboard+where+the+cursor+is+in+whatever+window&rlz=1C5OZZY_en&oq=macos+osascript+paste+from+clipboard+where+the+cursor+is+in+whatever+window&gs_lcrp=EgZjaHJvbWUyBggAEEUYOdIBCTQ3NDg4ajBqN6gCALACAA&sourceid=chrome&ie=UTF-8
  $retis=shell_exec("osascript -e 'tell application \"System Events\"
    # Get the name of the current application (which is likely the Script Editor or Automator)
    set currentApp to name of current application
    
    # Check if the current app is one of the script runners and hide it to allow the target app to become frontmost
    if currentApp is in {\"Script Editor\", \"Automator\", \"Script Debugger\"} then
        set visible of process currentApp to false
        # Wait a moment for the target app to become active
        delay 0.1
    end if
    
    # Simulate the Command+V keystroke in the now frontmost application
    keystroke \"a\" using command down
    
    # (Optional) Unhide the script application afterward
    if currentApp is in {\"Script Editor\", \"Automator\", \"Script Debugger\"} then
        set visible of process currentApp to true
    end if
end tell
  '");
  
  exit;
}
?>
