app.timer - Timer API

Provides delayed execution and repeated execution functionality.

Methods

app.timer.after(seconds, callback)

Creates a one-shot delayed timer.

Parameters:

  • seconds (number) - Delay time in seconds, minimum 0.01
  • callback (function) - Callback function to execute after the delay

Returns: string, error - Timer ID

-- Execute after 3 seconds
local id, err = app.timer.after(3, function()
    app.notification.show("Reminder", "3 seconds are up!")
end)

-- Execute after 0.5 seconds
app.timer.after(0.5, function()
    app.log.info("Executed after a short delay")
end)

app.timer.start(seconds, callback)

Creates a repeating timer.

Parameters:

  • seconds (number) - Execution interval in seconds, minimum 0.1
  • callback (function) - Callback function to execute on each trigger

Returns: string, error - Timer ID

-- Execute every 5 seconds
local id = app.timer.start(5, function()
    app.log.info("Heartbeat: " .. os.date())
end)

-- Stop when needed
app.timer.stop(id)

app.timer.stop(id)

Cancels the specified timer.

Parameters:

  • id (string) - Timer ID

Returns: boolean, error

local ok, err = app.timer.stop(id)

app.timer.stopAll()

Cancels all timers for the current plugin.

Returns: boolean

app.timer.stopAll()

app.timer.list()

Lists all active timers for the current plugin.

Returns: array<table> - Each item contains:

  • id (string) - Timer ID
  • type (string) - Type: "once" or "repeating"
  • interval (number) - Interval/delay in seconds
  • repeats (boolean) - Whether it repeats
  • remaining (number, one-shot only) - Remaining time in seconds
local timers = app.timer.list()
for _, tmr in ipairs(timers) do
    app.log.info(tmr.id .. ": " .. tmr.type .. " interval=" .. tmr.interval)
end

Description

  • Each plugin can have up to 100 timers
  • One-shot timers are automatically removed after execution
  • Timers are automatically cancelled when the plugin is unloaded
  • Callbacks are invoked on the plugin execution queue (thread-safe)
  • after() minimum delay is 0.01 seconds, every() minimum interval is 0.1 seconds

Examples

Periodically check for file changes

local checkTimerId = nil

function MyPlugin:handleStartMonitor(context)
    local dir = context.currentDirectory
    local lastCount = 0

    checkTimerId = app.timer.start(10, function()
        local files = app.file.list(dir)
        if files and #files ~= lastCount then
            local diff = #files - lastCount
            if diff > 0 then
                app.notification.show("New Files", "Found " .. diff .. " new file(s)")
            end
            lastCount = #files
        end
    end)

    app.notification.show("Monitor", "Started monitoring: " .. dir)
end

function MyPlugin:handleStopMonitor(context)
    if checkTimerId then
        app.timer.stop(checkTimerId)
        checkTimerId = nil
        app.notification.show("Monitor", "Monitoring stopped")
    end
end

Auto-close progress bar after delay

function MyPlugin:handleProcess(context)
    app.progress.show("Processing", {message = "Processing..."})

    -- Simulate processing
    for i = 1, 100 do
        app.progress.update(i, "Step " .. i .. "/100")
        app.thread.sleep(0.05)
    end

    app.progress.update(100, "Done!")

    -- Auto-close progress bar after 1 second
    app.timer.after(1, function()
        app.progress.hide()
    end)
end
Developer Documentation
User Guide
Getting Started Script Menus FAQ
Script Development
Development Guide
Plugin Development
Quick Start Development Guide Example Plugins
API Reference
Overview API Query Plugin Info Logging Finder Context Plugin Settings Internationalization
UI & Interaction
Dialog Progress Notification Chooser WebView Status Bar Dock
Files & Paths
File Operations Path Utilities Finder Actions Trash Extended Attributes Metadata File Watcher
Data Formats
JSON Plist CSV XML PDF Image
Text & Encoding
String Regex Date & Time Color Crypto
System
Shell Commands Process Application System Info AppleScript Shortcuts
System Info
Network Power/Battery Screen/Appearance Audio Bluetooth Location
Network
HTTP WebSocket URL
Input & Clipboard
Keyboard Mouse Hotkey Clipboard Window
Storage
SQLite Keychain UserDefaults
Media
OCR QR Code
Utilities
Archive UTI Share Timer Wake Lock Thread