Examples

Ready-to-run code snippets and examples for common apple-js use cases. Copy, paste, and run these examples to see apple-js in action.

😄

Siri Joke Timer

A simple timer that shows an alert, waits, tells a joke, and shows a completion message. Perfect for learning the basics of sequential command execution.

javascript
const { Osascript } = require("apple-js-stable");
const script = new Osascript();

async function siriJokeTimer() {
    // Show initial alert
    await script.executeSingleCommand(
        script.appleCommands.display("Siri Joke Timer", "Get ready for some laughs!")
    );
    
    // Wait 3 seconds
    await script.executeSingleCommand(
        script.appleCommands.delay(3)
    );
    
    // Tell a joke
    await script.executeSingleCommand(
        script.appleCommands.ai.tellJoke()
    );
    
    // Show completion alert
    await script.executeSingleCommand(
        script.appleCommands.display("Hope you enjoyed that!", "Joke complete!")
    );
}

siriJokeTimer();
📸

Screenshot + Sosumi

Takes a screenshot and plays the classic Mac "Sosumi" sound. Great for productivity workflows and system automation.

javascript
const { Osascript } = require("apple-js-stable");
const script = new Osascript();

async function screenshotAndSosumi() {
    // Take a screenshot
    await script.executeSingleCommand(
        script.appleCommands.systemControl.screenshotToDesktop()
    );
    
    // Play the classic Sosumi sound
    await script.executeSingleCommand(
        script.appleCommands.fun.playSosumi()
    );
    
    // Notify user
    await script.executeSingleCommand(
        script.appleCommands.display("Screenshot taken!", "Check your desktop for the new screenshot.")
    );
}

screenshotAndSosumi();
⌨️

Dramatic Typing Intro

Creates a dramatic introduction with typing effects, sounds, and speech. Perfect for presentations and demos.

javascript
const { Osascript } = require("apple-js-stable");
const script = new Osascript();

async function dramaticTypingIntro() {
    // Start with a dramatic announcement
    await script.executeSingleCommand(
        script.appleCommands.fun.dramaticAnnouncement("Welcome to the future of automation!")
    );
    
    // Wait for effect
    await script.executeSingleCommand(
        script.appleCommands.delay(2)
    );
    
    // Type some text with delay
    await script.executeSingleCommand(
        script.appleCommands.ui.typeText("Hello, World! This is apple-js in action.", 0.1)
    );
    
    // Play a sound
    await script.executeSingleCommand(
        script.appleCommands.fun.playSosumi()
    );
    
    // Speak the typed text
    await script.executeSingleCommand(
        script.appleCommands.speak("Hello, World! This is apple-js in action.", "Zarvox")
    );
}

dramaticTypingIntro();
🌐

Open URL in Chrome + Fullscreen

Opens Chrome with a specific URL and provides user feedback. Useful for web automation and browser control workflows.

javascript
const { Osascript } = require("apple-js-stable");
const script = new Osascript();

async function openChromeFullscreen() {
    // Open Chrome with a specific URL
    await script.executeSingleCommand(
        script.appleCommands.browser.openChrome("https://github.com")
    );
    
    // Wait for Chrome to load
    await script.executeSingleCommand(
        script.appleCommands.delay(3)
    );
    
    // Notify user
    await script.executeSingleCommand(
        script.appleCommands.display("Chrome opened!", "Chrome should now be open with GitHub loaded.")
    );
    
    // Play success sound
    await script.executeSingleCommand(
        script.appleCommands.fun.playSosumi()
    );
}

openChromeFullscreen();
🎤

Custom Voice Announcement

Demonstrates different voice options available in macOS text-to-speech. Shows how to use Zarvox, Alex, and other system voices.

javascript
const { Osascript } = require("apple-js-stable");
const script = new Osascript();

async function customVoiceAnnouncement() {
    // Welcome message
    await script.executeSingleCommand(
        script.appleCommands.display("Voice Demo", "Get ready for a custom voice announcement!")
    );
    
    // Wait a moment
    await script.executeSingleCommand(
        script.appleCommands.delay(2)
    );
    
    // Speak with Zarvox voice
    await script.executeSingleCommand(
        script.appleCommands.speak("Hello! I am Zarvox, your digital assistant.", "Zarvox")
    );
    
    // Wait
    await script.executeSingleCommand(
        script.appleCommands.delay(1)
    );
    
    // Speak with Alex voice
    await script.executeSingleCommand(
        script.appleCommands.speak("And I am Alex, here to help you with apple-js!", "Alex")
    );
    
    // Play a sound
    await script.executeSingleCommand(
        script.appleCommands.fun.playSosumi()
    );
    
    // Final message
    await script.executeSingleCommand(
        script.appleCommands.display("Voice demo complete!", "You've heard both Zarvox and Alex voices.")
    );
}

customVoiceAnnouncement();

Productivity Workflow

A comprehensive workflow that combines multiple apple-js features for productivity tasks. Includes screenshots, browser automation, and notifications.

javascript
const { Osascript } = require("apple-js-stable");
const script = new Osascript();

async function productivityWorkflow() {
    // Start the workflow
    await script.executeScript([
        script.appleCommands.display("Productivity Workflow", "Starting your productivity session..."),
        script.appleCommands.speak("Starting your productivity session", "Alex"),
        script.appleCommands.delay(2)
    ]);
    
    // Take a screenshot for documentation
    await script.executeSingleCommand(
        script.appleCommands.systemControl.screenshotToDesktop()
    );
    
    // Open Chrome for research
    await script.executeSingleCommand(
        script.appleCommands.browser.openChrome("https://github.com")
    );
    
    // Wait and notify
    await script.executeScript([
        script.appleCommands.delay(3),
        script.appleCommands.display("Workflow in progress", "Chrome opened and screenshot taken!"),
        script.appleCommands.fun.playSosumi()
    ]);
    
    // Type some notes
    await script.executeSingleCommand(
        script.appleCommands.ui.typeText("Productivity session started at: " + new Date().toLocaleTimeString(), 0.05)
    );
    
    // Final notification
    await script.executeSingleCommand(
        script.appleCommands.display("Workflow complete!", "Your productivity session is ready to go!")
    );
}

productivityWorkflow();
🎭

Interactive Demo

A complete interactive demonstration showcasing most apple-js features. Perfect for impressing friends or demonstrating the library's capabilities.

javascript
const { Osascript } = require("apple-js-stable");
const script = new Osascript();

async function interactiveDemo() {
    // Welcome sequence
    await script.executeScript([
        script.appleCommands.display("Interactive Demo", "Welcome to apple-js interactive demo!"),
        script.appleCommands.speak("Welcome to apple-js interactive demo!", "Zarvox"),
        script.appleCommands.delay(2)
    ]);
    
    // Joke sequence
    await script.executeScript([
        script.appleCommands.display("Joke Time", "Get ready for a joke!"),
        script.appleCommands.delay(3),
        script.appleCommands.ai.tellJoke(),
        script.appleCommands.delay(2)
    ]);
    
    // Typing demo
    await script.executeScript([
        script.appleCommands.display("Typing Demo", "Watch the magic happen!"),
        script.appleCommands.demoTypeHelloWorld(),
        script.appleCommands.delay(1)
    ]);
    
    // Sound effects
    await script.executeScript([
        script.appleCommands.fun.playSosumi(),
        script.appleCommands.delay(1),
        script.appleCommands.fun.dramaticAnnouncement("Demo complete!")
    ]);
    
    // Final message
    await script.executeSingleCommand(
        script.appleCommands.display("Demo finished!", "Thanks for trying apple-js!")
    );
}

interactiveDemo();

💡 Tips & Best Practices

Use executeScript for Multiple Commands

When running multiple commands, use executeScript()instead of multiple executeSingleCommand() calls for better performance.

Add Delays for Better UX

Use delay() between commands to give users time to see what's happening and create a more natural flow.

Handle Errors Gracefully

Wrap your apple-js calls in try-catch blocks to handle any permission or execution errors that might occur.

Test Permissions First

Always test that your app has the necessary permissions before running complex automation workflows.