Paste from clipboard with a specific variable

Good day
I encountered a problem after updating WhatsApp.

I used your function before:

Enter the URL to open. %@ will be replaced with the currently selected text or the content of your clipboard. You can also use BTT variables by putting them in curly braces e.g. {BTT_OPENED_URL}, {CLIPBOARD_CONTENT}

The command looked like this:
whatsapp://send?phone=%@

For example:
I copied the phone number to the clipboard: +7 123 456-78-90
pressed a certain key combination and a new chat in the WhatsApp application started.

Now WhatsApp has been updated, and the phone number needs to be entered like this: +71234567890
Without "spaces" and "-"

Please tell me how I can change the old command "whatsapp://send?phone=%@" so that my phone numbers are inserted into it without spaces and "-"?

you could use the predefined action "Transform & Copy Selection With Java Script" with this script:

async (clipboardContentString) => {
   // remove whitespace and -
   let phoneNumber = clipboardContentString.replace(/[\s-]/g, '');

   // open whatsapp
   let script = `/usr/bin/open whatsapp://send?phone=${phoneNumber}`
   runShellScript({script: script});
 
   return phoneNumber;
}

This would copy the number and immediately open whatsapp. If you want to copy separately use the predefined action "Transform Clipboard Contents" instead (the script stays the same)

It worked :+1:
Thank you very much :handshake:

Please add an exception to the code
Brackets.
"(" and ")"

Sometimes phone numbers are indicated with brackets, for example:
+7 (123) 456-78-90

async (clipboardContentString) => {
   // remove whitespace, -, (, and )
   let phoneNumber = clipboardContentString.replace(/[\s\-\(\)]/g, '');

   // open whatsapp
   let script = `/usr/bin/open whatsapp://send?phone=${phoneNumber}`
   runShellScript({script: script});
 
   return phoneNumber;
}

Thank you very much, everything works :+1:

Only for some reason it changes the text in the original.
For example:

  1. I select a phone number in a text document,
  2. I copy it to the clipboard
  3. I press a combination of buttons
  4. WhatsApp opens and a dialogue begins

But in the text document the phone number also changes, spaces, brackets and dashes are removed

You can use the "Transform Clipboard Contents" instead to prevent this

And I just noticed that this scheme only works if the text is selected, and not copied to the clipboard.

That is, if you copy the text and deselect the text, the script does not work.
But if you select the text and run the script, it works, but replaces the original text with a version without spaces, dashes and punctuation marks.

I also noticed that for some reason the script does not work in Google Sheets, it removes spaces at the beginning and that's it, it doesn't even open WhatsApp.

Yes, it worked, thanks.
I just don't understand why the script doesn't work if I copy text from Google Sheets...

that I don't know, is the number copied correctly to your clipboard in google sheets?

Yes, and if you copy the same number to the clipboard from another place (not from Google Sheets), then everything works :slight_smile:
Also, if you copy another phone number to the clipboard from Google Sheets - it also works :slight_smile:

Maybe it's just some kind of glitch :slight_smile: ?

Maybe it contains some special characters (maybe invisible ones), then the script would fail

Hi @T-N-T, regarding your question about the script not working in Google Sheets, I have a couple ideas:

Idea #1 – copying the cell versus copying the cell's content may lead to different results when parsing the phone number.

When you copy the phone number, are you copying the cell in Google Sheets or are you copying the text in the cell? This distinction may be causing issues parsing the phone number.

Idea #2 – the raw text in the cell may have special formatting that will cause this line of code to not extract the phone number correctly:

let phoneNumber = clipboardContentString.replace(/[\s\-\(\)]/g, '');

Question: How exactly are the phone numbers stored in Google Sheets?

I ask because depending on how exactly the phone number is entered into the Google Sheets cell, it may cause the JavaScript script Andreas provided to not work correctly.

I'm making this point because, in Google Sheets, it's not possible to store text in a cell that starts with a + character by simply entering the raw text because Google Sheets will automatically interpret the + character as the start of a formula. So, if you enter the example phone number you provided, i.e. +7 (123) 456-78-90, this will result in "Formula parsing error". This leads me to believe that the phone numbers stored in the Google Sheets cells are not exactly formatted like +7 (123) 456-78-90, leading to this line of code to not extract the phone number correctly: let phoneNumber = clipboardContentString.replace(/[\s\-\(\)]/g, '');.

Click the dropdown to see screenshots showing the Formula parsing error

Type +7 (123) 456-78-90 into a Google Sheets cell...

Tap the return key will result in an error in the cell...

So, if my understanding of your situation is correct, either the phone numbers in your Google Sheet...

  1. Do not start with a + character, or;
  2. Do start with a + character, but are being escaped to display the raw text. e.g.:
    i. '+7 (123) 456-78-90
    ii. ="+7 (123) 456-78-90"

In either case, the script Andreas provided you may not work as expected and will need to be modified.

Good day.
When I copy a phone number from a Google spreadsheet, I copy only the "text", not the "cell" itself.
That is, I select the text, press "control + v".
Phones in the Google spreadsheet are written without "+".

Previously, according to this scheme: whatsapp://send?phone=%@
I could not copy the text to the clipboard, but simply select it, this was enough to start the desired process.

Andreas' scheme works, but over the past couple of days I encountered a problem that some phones (for unknown reasons) did not work according to this scheme...

@T-N-T I think I know what might be causing the issue. The ? character in whatsapp://send?phone= needs to be escaped with a \ character. If it's not escaped, then the command won't work correctly.

Here's the full script:

async (clipboardContentString) => {
  // remove whitespace, -, (, and )
  let phoneNumber = clipboardContentString.replace(/[\s\-\(\)]/g, "");

  // open whatsapp and escape the `?` character with `\`
  let script = `/usr/bin/open whatsapp://send\?phone=${phoneNumber}`;

  runShellScript({ script: script });

  return phoneNumber;
};

I've been able to use this Keyboard Shortcut trigger & Transform Clipboard Contents with JavaScript action combination successfully with a verify of different phone number formats in Google Sheets.

[
  {
    "BTTActionCategory" : 0,
    "BTTLastUpdatedAt" : 1730318371.1017189,
    "BTTTriggerType" : 0,
    "BTTTriggerClass" : "BTTTriggerTypeKeyboardShortcut",
    "BTTUUID" : "8CF06DC7-72D3-4BC3-8148-E15B1DE788F1",
    "BTTPredefinedActionType" : 366,
    "BTTPredefinedActionName" : "Empty Placeholder",
    "BTTAdditionalConfiguration" : "262145",
    "BTTKeyboardShortcutKeyboardType" : 2302,
    "BTTTriggerOnDown" : 1,
    "BTTLayoutIndependentChar" : "v",
    "BTTEnabled" : 1,
    "BTTEnabled2" : 1,
    "BTTShortcutKeyCode" : 9,
    "BTTShortcutModifierKeys" : 262144,
    "BTTOrder" : 1,
    "BTTAutoAdaptToKeyboardLayout" : 0,
    "BTTAdditionalActions" : [
      {
        "BTTActionCategory" : 0,
        "BTTLastUpdatedAt" : 1730321104.0419459,
        "BTTTriggerParentUUID" : "8CF06DC7-72D3-4BC3-8148-E15B1DE788F1",
        "BTTIsPureAction" : true,
        "BTTTriggerClass" : "BTTTriggerTypeKeyboardShortcut",
        "BTTUUID" : "B06FB1AF-686C-44D6-B09F-82E1B481BE9E",
        "BTTPredefinedActionType" : 448,
        "BTTPredefinedActionName" : "Transform Clipboard Contents with Java Script",
        "BTTAdditionalActionData" : {
          "BTTClipboardTransformerExampleInput" : "7 123 456-78-90",
          "SelectedTransformerTab" : 1,
          "BTTClipboardTransformerJS" : "async (clipboardContentString) => {\n  \/\/ remove whitespace, -, (, and )\n  let phoneNumber = clipboardContentString.replace(\/[\\s\\-\\(\\)]\/g, \"\");\n\n  \/\/ open whatsapp and escape the `?` character with `\\`\n  let script = `\/usr\/bin\/open whatsapp:\/\/send\\?phone=${phoneNumber}`;\n\n  runShellScript({ script: script });\n\n  return phoneNumber;\n};\n"
        },
        "BTTGenericActionConfig" : "phone_number_to_whatsapp",
        "BTTKeyboardShortcutKeyboardType" : 0,
        "BTTEnabled" : 1,
        "BTTEnabled2" : 1,
        "BTTShortcutKeyCode" : -1,
        "BTTOrder" : 3,
        "BTTAutoAdaptToKeyboardLayout" : 0
      }
    ]
  }
]

I hope this helps.

1 Like

Maybe I'm not inserting the code correctly, but it doesn't work for me from Google Sheets... (

@T-N-T Could you share with me:

  1. Your BTT configuration (both the trigger and the action)? To do this, right click on the Trigger and select the first option, "Copy". Then, paste the content here so I can take a look.
  2. The Google Sheet. Before doing so, make sure you don't share any sensitive information (e.g. real phone numbers).

[
{
"BTTActionCategory" : 0,
"BTTIsPureAction" : true,
"BTTPredefinedActionType" : 448,
"BTTPredefinedActionName" : "Transform Clipboard Contents with Java Script",
"BTTAdditionalActionData" : {
"BTTClipboardTransformerJS" : "async (clipboardContentString) => {\n // remove whitespace, -, (, and )\n let phoneNumber = clipboardContentString.replace(/[\s\-\(\)]/g, "");\n\n // open whatsapp and escape the ? character with \\\n let script = \/usr\/bin\/open whatsapp:\/\/send\\?phone=${phoneNumber};\n\n runShellScript({ script: script });\n\n return phoneNumber;\n};\n\n[\n {\n "BTTActionCategory" : 0,\n "BTTLastUpdatedAt" : 1730318371.1017189,\n "BTTTriggerType" : 0,\n "BTTTriggerClass" : "BTTTriggerTypeKeyboardShortcut",\n "BTTUUID" : "8CF06DC7-72D3-4BC3-8148-E15B1DE788F1",\n "BTTPredefinedActionType" : 366,\n "BTTPredefinedActionName" : "Empty Placeholder",\n "BTTAdditionalConfiguration" : "262145",\n "BTTKeyboardShortcutKeyboardType" : 2302,\n "BTTTriggerOnDown" : 1,\n "BTTLayoutIndependentChar" : "v",\n "BTTEnabled" : 1,\n "BTTEnabled2" : 1,\n "BTTShortcutKeyCode" : 9,\n "BTTShortcutModifierKeys" : 262144,\n "BTTOrder" : 1,\n "BTTAutoAdaptToKeyboardLayout" : 0,\n "BTTAdditionalActions" : [\n {\n "BTTActionCategory" : 0,\n "BTTLastUpdatedAt" : 1730321104.0419459,\n "BTTTriggerParentUUID" : "8CF06DC7-72D3-4BC3-8148-E15B1DE788F1",\n "BTTIsPureAction" : true,\n "BTTTriggerClass" : "BTTTriggerTypeKeyboardShortcut",\n "BTTUUID" : "B06FB1AF-686C-44D6-B09F-82E1B481BE9E",\n "BTTPredefinedActionType" : 448,\n "BTTPredefinedActionName" : "Transform Clipboard Contents with Java Script",\n "BTTAdditionalActionData" : {\n "BTTClipboardTransformerExampleInput" : "7 123 456-78-90",\n "SelectedTransformerTab" : 1,\n "BTTClipboardTransformerJS" : "async (clipboardContentString) => {\n \/\/ remove whitespace, -, (, and )\n let phoneNumber = clipboardContentString.replace(\/[\\s\\-\\(\\)]\/g, \"\");\n\n \/\/ open whatsapp and escape the ? character with \\\\\n let script = \\\/usr\\\/bin\\\/open whatsapp:\\\/\\\/send\\\\?phone=${phoneNumber};\n\n runShellScript({ script: script });\n\n return phoneNumber;\n};\n"\n },\n "BTTGenericActionConfig" : "phone_number_to_whatsapp",\n "BTTKeyboardShortcutKeyboardType" : 0,\n "BTTEnabled" : 1,\n "BTTEnabled2" : 1,\n "BTTShortcutKeyCode" : -1,\n "BTTOrder" : 3,\n "BTTAutoAdaptToKeyboardLayout" : 0\n }\n ]\n }\n]"
},
"BTTEnabled2" : 1,
"BTTOrder" : 18
}
]

@T-N-T

The JSON you shared looks correct... but I can't confirm with 100% confidence because you shared the JSON as plain text instead of in a Markdown code block. Sharing the JSON as plain text makes the back ticks (i.e. the ` character) render so I'm having trouble confirming that it's correct.

To help me help you, could you:

  1. Share the JSON again in this format

     ```json
     paste the JSON right here between ```json and ```
     ```
    
  2. Could you try to describe in more detail what's not working?

  1. I created a modified version of the script to display a popup dialog that will show debugging information of the script. This will help us figure out what's going wrong. Could you please update the script to the one below and share with me the results?
async (clipboardContentString) => {
  // Function to display dialog with input, escaping any double quotes
  function displayDialog(message) {
    let safeMessage = message.replace(/"/g, '\\"'); // Escape double quotes
    let debugScript = `
    display dialog "${safeMessage}"
    `;
    runAppleScript(debugScript);
  }

  // Error handling block
  try {
    // Remove whitespace, dashes, and parentheses
    let phoneNumber = clipboardContentString.replace(/[\s\-\(\)]/g, "");

    // Regex check for valid phone number format (example: international format)
    let isValidPhoneNumber = /^\+?[1-9]\d{1,14}$/.test(phoneNumber);

    // Stop execution if phone number is not valid
    if (!isValidPhoneNumber) {
      displayDialog(`Invalid phone number format: ${phoneNumber}. Execution stopped.`);
      return phoneNumber;
    }

    // Escape any special characters to prevent command injection
    let escapedPhoneNumber = phoneNumber.replace(/[^a-zA-Z0-9+]/g, '');

    // Construct the shell command with the escaped phone number
    let script = `/usr/bin/open whatsapp://send\?phone=${escapedPhoneNumber}`;

    // Use AppleScript with better readability and extra new lines between key-value pairs
    let appleScript = `
    set dialogText to "Clipboard Content:\n" & "${clipboardContentString}" & "\n\n" & \
      "\nFormatted Phone Number:\n" & "${phoneNumber}" & "\n\n" & \
      "\nIs Valid Phone Number:\n" & "${isValidPhoneNumber}" & "\n\n" & \
      "\nShell Script:\n" & "${script}"
    display dialog dialogText
    `;

    // Execute the AppleScript
    runAppleScript(appleScript);

    // Execute the shell command
    runShellScript({ script });

    return phoneNumber;
  } catch (error) {
    // Display dialog with the last variable that caused an error
    displayDialog(`Error encountered: ${error.message}`);
    throw error; // Re-throw the error for any higher-level handling
  }
};

Once you run this updated script, you should see this popup:

I am attaching 2 screenshots.

No. 1 One of the numbers of Google tables, in case the script does not work.
1

No. 2 One of the numbers of the same Google table, in case the script works.
2

I select a phone number in a Google table, copy it to the clipboard, run the script.
Sometimes the script works, sometimes it does not.