Scriptable Pasteboard API

Hi, fellow automators!

I’m having problems using the iOS clipboard (Pasteboard) in Scriptable.

I have a JavaScript that performs FIND and REPLACE on the specified text using a series of lists of regular expressions. Here’s a link to a .txt file of the script: http://share.llmmd.me/SnacDv

I know it’s long, but the first 1636 lines are nothing but regular expressions. The meat of the script is in the 15 lines that start at line 1638. I have pasted those 15 lines of code below.

Although the script runs perfectly on the Online JavaScript Editor https://js.do, it doesn’t run in Scriptable.

HERE ARE MY PROBLEMS:
(1) I think there’s something wrong with how I have the HTML headers, because Scriptable returns the error message SyntaxError: Unexpected token ‘<’
(2) In the example script I have defined the variable textInput as a specific string, but what I need is for textInput to be whatever text string is in the iOS clipboard (pasteboard).
(3) In the example script I have output the variable textOutput using the document.write function, but what I need is for textOutput to be pasted to the pasteboard as a string for subsequent use in a Siri Shortcut.

I have read the Scriptable documentation on the Pasteboard, but I can’t seem to get the syntax correct. I’m a scripting noob, so I really appreciate the help any of you can give me.

Thanks to the Automators community!

Lee

var textInput = "Onset reported as yesterday. Severity reported as severe yesterday. Severity reported as moderate today. Location is left knee. Hypertension is good. Diabetes is fair. GERD is good. Depression is good.";

function findAndReplace(myString, ...lists) {
    var list = null;
    for (var j = 0; j < lists.length; j++) {
        list = lists[j];
        for (var i = 0; i < list.length; i++) {
            myString = myString.replace(list[i].pattern, list[i].replacement);
        }
    }
    return myString;
}

var textOutput = (findAndReplace(textInput, list1, list2, list3, list4, list5));

document.write(textOutput);
1 Like

Very clever idea, looks like it will save you a lot of typing time! Can you also include the Scriptable pasteboard code you’re using that isn’t working?

Edit – I tried running your script (with fewer regex matches) using Pasteboard and it worked fine. If you are using Pasteboard as I do below and it’s not working, then there’s probably an issue with one of your regex. That will not be fun to debug, sorry :frowning: I would try a tool like https://codebeautify.org/jsvalidate or https://validatejavascript.com/

var list1 = [
  {
      pattern: /\b34 days ago/i,
      replacement: '3-4 days ago'
  }
];

var list2 = [
  {
      pattern: /\bnext problem[\.:]? ?/i,
      replacement: '<br><br>PROBLEM/CONDITION: '
  }
];

var textInput = "next problem 34 days ago";
var clipboardContents = Pasteboard.paste();
console.log(`Clipboard contents: ${clipboardContents}`);

function findAndReplace(myString, ...lists) {
  var list = null;
  for (var j = 0; j < lists.length; j++) {
      list = lists[j];
      for (var i = 0; i < list.length; i++) {
          myString = myString.replace(list[i].pattern, list[i].replacement);
      }
  }
  Pasteboard.copy(myString);
  // return myString;
}

findAndReplace(clipboardContents, list1, list2);
console.log('Running script');

console.log(`Contents of clipboard: ${Pasteboard.paste()}`);
2 Likes

Are your trying to run the text file which is a web page inside of Scriptable? If so you can’t do that. Scriptable only runs JavaScript. Also Scriptable isn’t a web page so it doesnt have access to the document object.

If you follow the pattern that @jsloat showed up I bet all your regexes will work

2 Likes

Yes good point, I assumed the HTML markup was just for sharing the file, I stripped all that out and ran exactly what I included above in Scriptable.