From iterm
Pattern for generating iTerm2 AppleScript with window checks, dual tell blocks, and conditional configuration
npx claudepluginhub manifoldlogic/claude-code-plugins --plugin itermThis skill uses the workspace's default tool permissions.
This skill documents the established pattern for generating AppleScript for iTerm2 pane and tab operations in the iTerm plugin. The pattern includes window existence validation, dual tell block structure for operations and configuration, and conditional sections based on arguments.
Guides Next.js Cache Components and Partial Prerendering (PPR) with cacheComponents enabled. Implements 'use cache', cacheLife(), cacheTag(), revalidateTag(), static/dynamic optimization, and cache debugging.
Guides building MCP servers enabling LLMs to interact with external services via tools. Covers best practices, TypeScript/Node (MCP SDK), Python (FastMCP).
Generates original PNG/PDF visual art via design philosophy manifestos for posters, graphics, and static designs on user request.
This skill documents the established pattern for generating AppleScript for iTerm2 pane and tab operations in the iTerm plugin. The pattern includes window existence validation, dual tell block structure for operations and configuration, and conditional sections based on arguments.
This pattern ensures consistent error handling and operation structure across all iTerm plugin scripts.
Apply this pattern when:
AppleScript generation follows this template:
tell application "iTerm2"
activate
-- VALIDATION BLOCK (optional but recommended)
if (count of windows) is 0 then
return "NO_WINDOWS"
end if
-- OPERATION BLOCK (required)
tell current session of current tab of first window
-- perform operation (split, create tab, etc.)
end tell
-- CONFIGURATION BLOCK (conditional)
tell current session of current tab of first window
-- set name, write text, etc. (if arguments provided)
end tell
end tell
Always start with activate to ensure iTerm2 is frontmost:
tell application "iTerm2"
activate
Why: Operations on background applications can fail or behave unpredictably.
For operations requiring existing windows, check before attempting operation:
if (count of windows) is 0 then
return "NO_WINDOWS"
end if
When to use:
When to skip:
Error handling: Shell script checks for "NO_WINDOWS" return value and exits with appropriate error message.
Single tell block for the primary operation:
tell current session of current tab of first window
split vertically with profile "Devcontainer"
end tell
Targeting convention:
first window for safety (not current window)current session for pane operationscurrent tab for tab-level operationsWhy first window: Multi-window safety. current window may be undefined; first window is always the frontmost window.
Separate tell block for post-operation configuration:
tell current session of current tab of first window
set name to "session title"
write text "command to execute"
end tell
Why separate block: After operations like split, the new session receives focus. A second tell current session correctly targets the newly created pane/tab.
Conditional inclusion: Only include if configuration arguments were provided (-n for name, -c for command).
build_applescript() {
local direction="${1:-vertical}"
local profile="${2:-Devcontainer}"
local command="${3:-}"
local name="${4:-}"
# Escape special characters
local escaped_profile
escaped_profile=$(escape_applescript_string "$profile")
# Start AppleScript
local script
script=$(cat << EOF
tell application "iTerm2"
activate
-- Window count validation
if (count of windows) is 0 then
return "NO_WINDOWS"
end if
-- Operation block
tell current session of current tab of first window
split ${direction} with profile "${escaped_profile}"
end tell
EOF
)
# Conditional configuration block
if [[ -n "$name" || -n "$command" ]]; then
script+=$(cat << EOF
-- Configuration block
tell current session of current tab of first window
EOF
)
if [[ -n "$name" ]]; then
local escaped_name
escaped_name=$(escape_applescript_string "$name")
script+=$(cat << EOF
set name to "${escaped_name}"
EOF
)
fi
if [[ -n "$command" ]]; then
local escaped_command
escaped_command=$(escape_applescript_string "$command")
script+=$(cat << EOF
write text "${escaped_command}"
EOF
)
fi
script+=$(cat << EOF
end tell
EOF
)
fi
# Close AppleScript
script+=$(cat << EOF
end tell
EOF
)
printf '%s' "$script"
}
Always escape user-provided strings before embedding in AppleScript:
escape_applescript_string() {
local input="$1"
# Escape backslashes first, then double quotes
local escaped="${input//\\/\\\\}"
escaped="${escaped//\"/\\\"}"
printf '%s' "$escaped"
}
Critical: Escape backslashes before quotes to avoid double-escaping.
# Input
build_applescript "vertical" "Devcontainer" "npm test" "tests"
# Generated AppleScript
tell application "iTerm2"
activate
if (count of windows) is 0 then
return "NO_WINDOWS"
end if
tell current session of current tab of first window
split vertically with profile "Devcontainer"
end tell
tell current session of current tab of first window
set name to "tests"
write text "npm test"
end tell
end tell
# Input
build_applescript "horizontal" "Development" "" "Monitor Pane"
# Generated AppleScript
tell application "iTerm2"
activate
if (count of windows) is 0 then
return "NO_WINDOWS"
end if
tell current session of current tab of first window
split horizontally with profile "Development"
end tell
tell current session of current tab of first window
set name to "Monitor Pane"
end tell
end tell
# Input
build_applescript "vertical" "Devcontainer" "" ""
# Generated AppleScript
tell application "iTerm2"
activate
if (count of windows) is 0 then
return "NO_WINDOWS"
end if
tell current session of current tab of first window
split vertically with profile "Devcontainer"
end tell
end tell
# In main() function
result=$(run_applescript "$applescript")
if [[ "$result" == "NO_WINDOWS" ]]; then
iterm_error "No iTerm2 windows open. Cannot split pane."
iterm_error "Open an iTerm2 window first, then retry."
return "$EXIT_INVALID_ARGS"
fi
plugins/iterm/skills/pane-management/scripts/iterm-split-pane.sh (build_applescript function, lines 200-260)plugins/iterm/skills/tab-management/scripts/iterm-open-tab.sh (similar pattern for tab creation)archive/tickets/PANE-001_core-split-script/planning/architecture.md (Decision 3: Single AppleScript Block for Split + Configure)