Action
Format Phone Number
I noticed that phone numbers formatted as “+1 (408) 555-1212” are not clickable links when a draft is in Link Mode, so I made a script that takes an input, strips out all non-numeric characters, and then converts it to “nnn.nnn.nnnn”. So for the input above, it would be converted to “408.555.1212”.
The script is easily modifiable to other phone-number formats, but the dot format is my personal favorite. Edge cases: The script errors out if there are no numbers in the input. And if your input is longer than 10 numbers (11 with a leading 1), the output will be “nnn.nnn.nnnnnnn…”.
This is my first script, so let me know if you liked it! I’m @andybates on Twitter, or @andybates17 elsewhere.
Steps
-
script
let inputNumber = editor.getSelectedText(); // Remove non-numeric characters const strippedInput = inputNumber.replace(/[^\d]/g, ''); // Check if the input is empty if (strippedInput === '') { console.log('Input must contain numerical characters'); context.cancel(); } else { // Remove leading '1' if present let formattedNumber = strippedInput; if (formattedNumber.startsWith('1')) { formattedNumber = formattedNumber.slice(1); } // Format the number const regex = /^(\d{3})(\d{3})(\d+)$/; formattedNumber = formattedNumber.replace(regex, '$1.$2.$3'); editor.setSelectedText(formattedNumber); }
Options
-
After Success Default Notification Error Log Level Error
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.