JavaScript Snippets: Programmable Text Expansion
Write snippets that compute values, transform text, and generate dynamic content using JavaScript.
Sometimes a template isn’t enough. You need to calculate a value, transform some text, or generate something programmatically. TypeSnap lets you write JavaScript directly in your snippets.
There are two ways to use JavaScript: the JavaScript content type (the entire snippet is a script) and the {{js:...}} macro (inline JavaScript within a regular snippet).
The JavaScript Content Type
When you set a snippet’s content type to JavaScript, the entire content is executed as a script. The return value becomes the expanded text.
- Create a new snippet
- Change the Type picker from Plain Text to JavaScript
- Write your script. The last expression’s value (or an explicit
return) is the output.
Example: Random Password
Abbreviation: ;pass
Type: JavaScript
const chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefghjkmnpqrstwxyz23456789!@#$%';
let password = '';
for (let i = 0; i < 16; i++) {
password += chars[Math.floor(Math.random() * chars.length)];
}
password;
Type ;pass and you get a random 16-character password. A new one every time.
Example: Unix Timestamp
Abbreviation: ;unix
Type: JavaScript
Math.floor(Date.now() / 1000).toString();
Example: JSON Formatter
Abbreviation: ;jsonpretty
Type: JavaScript
const raw = getClipboard();
try {
JSON.stringify(JSON.parse(raw), null, 2);
} catch (e) {
'Invalid JSON on clipboard';
}
Copy some minified JSON, type ;jsonpretty, and get the formatted version. The getClipboard() function reads the current clipboard contents.
The Inline Script Macro
For snippets where you need one computed value inside a larger template, use the {{js:...}} or {{javascript:...}} macro:
Today is day {{js:Math.ceil((new Date() - new Date(new Date().getFullYear(),0,1)) / 86400000)}} of the year.
The script runs inline, and its result replaces the macro.
Example: Week Number
Abbreviation: ;week
Week {{js:Math.ceil((((new Date() - new Date(new Date().getFullYear(),0,1)) / 86400000) + new Date(new Date().getFullYear(),0,1).getDay() + 1) / 7)}} of {{date:yyyy}}
Example: Countdown
Abbreviation: ;xmas
{{js:Math.ceil((new Date(new Date().getFullYear(), 11, 25) - new Date()) / 86400000)}} days until Christmas
Available Built-in Functions
TypeSnap’s JavaScript sandbox includes helper functions:
| Function | Description |
|---|---|
getDate() |
Current date as a string |
getTime() |
Current time as a string |
getClipboard() |
Current clipboard contents |
uppercase(str) |
Convert to uppercase |
lowercase(str) |
Convert to lowercase |
capitalize(str) |
Capitalize first letter of each word |
trim(str) |
Remove leading/trailing whitespace |
uuid() |
Generate a UUID v4 |
getEnv(key) |
Read allowed environment variables |
formatDate(format) |
Format current date with a custom format string |
randomInt(min, max) |
Generate a random integer in a range |
join(array, sep) |
Join an array with a separator |
The sandbox also exposes date/time variables you can use directly: now (a Date object), year, month, day, hour, minute, second, and weekday.
Example: Title Case from Clipboard
Abbreviation: ;titlecase
Type: JavaScript
capitalize(getClipboard());
Copy “the quick brown fox”, type ;titlecase, get “The Quick Brown Fox”.
Example: Word Count
Abbreviation: ;wc
Type: JavaScript
const text = getClipboard();
const words = text.trim().split(/\s+/).length;
const chars = text.length;
words + ' words, ' + chars + ' characters';
Example: Slug Generator
Abbreviation: ;slug
Type: JavaScript
lowercase(getClipboard())
.replace(/[^a-z0-9\s-]/g, '')
.replace(/\s+/g, '-')
.replace(/-+/g, '-')
.replace(/^-|-$/g, '');
Copy “My Blog Post Title!”, type ;slug, get my-blog-post-title.
Combining JavaScript with Fill-Ins
JavaScript snippets can coexist with fill-in macros. The fill-in dialog appears first; then the script runs with the filled-in values available.
Abbreviation: ;calc
{{input:Expression}} = {{js:eval('{{input:Expression}}')}}
Type ;calc, enter 24 * 365 in the dialog, get 24 * 365 = 8760.
Security
TypeSnap runs JavaScript in a sandboxed environment. Scripts:
- Cannot access the filesystem
- Cannot make network requests
- Cannot access other applications
- Cannot run shell commands
- Have limited environment variable access
The sandbox keeps your system safe even if you import snippets from an external source.
When to Use JavaScript vs. Macros
Use macros ({{date}}, {{random}}, etc.) when a built-in macro covers your need. They’re simpler, faster, and easier to read.
Use JavaScript when you need:
- String transformation (case conversion, formatting, encoding)
- Math calculations
- Conditional logic
- Processing clipboard content
- Generating structured data
- Anything a macro can’t do
JavaScript snippets are the escape hatch. When the built-in macros aren’t enough, you have a full programming language available.
Stop typing the same things over and over
TypeSnap expands your snippets instantly. One-time purchase, no subscription.