Trigger Mac shortcut on button click [Processing]

Hi All :slight_smile:

My name is Alwin, and I used Processing some years ago at college. And now, I want to use Processing again to prototype some UX hobby projects.

I was curious if someone could maybe help me with a line of code to make something work :slight_smile:

I have made some email text templates in an application called TextExpander. And at the moment, it works like this:

In a new email, I type, for example, “”
(the shortcut word can be anything, so there is no need to use the < >)

Then TextExpander launches a popup box.

In this popup box, I can fill in some information, like, for example, the name of the person.

Then I press OK, and the template text will automatically be added to the email.

But I would love to design a simple interface around the templates in Processing. Does anyone know what code I should use to trigger a shortcut on macOS?

I thought maybe it’s possible to trigger a Mac Sudo or another “external.” command to the macOS?

Alwin

 trigger a shortcut on macOS

What’s the ‘shortcut’?

Hello @svan, the shortcut is to launch a TextExpander template snippet. If you type, for example, the word: “template-a” in an email, then the template snippet will automatically launch. TextExpander has access to macOS and will launch with every shortcut word you set up per template snippet in TextExpander.

I’m excited to tell you that I fixed it with the Java Robot Class. If you send a standard command word via Processing, like “template-a”, it will not work. But if you simulate the keyboard and let the Robot Class type the word "“template-a” it will work!

I’m excited to tell you that I fixed it with the Java Robot Class

I’ve seen Java apps that can do this when run from Terminal, eg ‘Typer’ (syntax: java Typer --delay 5000 triggerStr). Have you done something similar with a Processing app?

Taking your suggestion the following code will type ‘welcome to processing’ in a specified editor of your choosing (must be selected with cursor blinking) using the Robot class. Will also enter text into Processing IDE (or any other text editor) if you re-select it after hitting ‘Sketch/Run’. Reference:https://www.javatpoint.com/java-robot

import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.*;

static Robot robot = null;
static Process proc = null;

static void doKeyPresses() {

  try {
    robot = new Robot();
    Thread.sleep(5000);
    robot.keyPress(KeyEvent.VK_W);    
    Thread.sleep(500); //delay of 5 milliseconds
    robot.keyPress(KeyEvent.VK_E);
    Thread.sleep(500);
    robot.keyPress(KeyEvent.VK_L);
    Thread.sleep(500);
    robot.keyPress(KeyEvent.VK_C);
    Thread.sleep(500);
    robot.keyPress(KeyEvent.VK_O);
    Thread.sleep(500);
    robot.keyPress(KeyEvent.VK_M);
    Thread.sleep(500);
    robot.keyPress(KeyEvent.VK_E);
    Thread.sleep(500);
    robot.keyPress(KeyEvent.VK_SPACE);
    Thread.sleep(500);
    robot.keyPress(KeyEvent.VK_T);
    Thread.sleep(500);
    robot.keyPress(KeyEvent.VK_O);
    Thread.sleep(500);
    robot.keyPress(KeyEvent.VK_SPACE);
    Thread.sleep(500);
    robot.keyPress(KeyEvent.VK_P);
    Thread.sleep(500);
    robot.keyPress(KeyEvent.VK_R);
    Thread.sleep(500);
    robot.keyPress(KeyEvent.VK_O);
    Thread.sleep(500);
    robot.keyPress(KeyEvent.VK_C);
    Thread.sleep(500);
    robot.keyPress(KeyEvent.VK_E);
    Thread.sleep(500);
    robot.keyPress(KeyEvent.VK_S);
    Thread.sleep(500);
    robot.keyPress(KeyEvent.VK_S);
    Thread.sleep(500);
    robot.keyPress(KeyEvent.VK_I);
    Thread.sleep(500);
    robot.keyPress(KeyEvent.VK_N);
    Thread.sleep(500);
    robot.keyPress(KeyEvent.VK_G);
    Thread.sleep(500);
  }
  catch(Exception e) {
    println("Error: " + e);
  }
}

public static void main(String args[]) {

  try {
    proc = exec("open", "/Applications/Sublime.app");
    BufferedReader out = new BufferedReader(new InputStreamReader(proc.getInputStream()));
    BufferedReader err = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
    // Read the output from the command
    System.out.println("Here is the standard output of the command:\n");
    String s = null;
    while ((s = out.readLine()) != null) {
      println(s);
    }
    // Read any errors from the attempted command
    System.out.println("Here is the standard error of the command (if any):\n");
    while ((s = err.readLine()) != null) {
      println(s);
    }
  }
  catch (Exception ex) {
    ex.printStackTrace();
  }

  doKeyPresses();
}