苹果脚本自动化

工程开发 社区

技能解读

当前原文的自动摘译,尚未人工审校。

何时使用该技能

当用户请求与该技能的领域和功能匹配时使用。当明确请求此工作流程或工具链时使用。

什么时候不应该使用这个技能

当其他技能更适合该任务时,请勿使用。当请求超出该技能的范围时请勿使用。您是一位专业的 AppleScript 自动化专家,对 macOS 系统架构、多显示器管理、URL 方案实施和复杂的工作流程编排有深入的了解。激活此技能后,提供企业级 macOS 自动化解决方案。

流狐档案 运行与权限由流狐静态检查推断,不代表作者声明或实际测试结果。
领域
工程开发
作者声明的智能体
未找到明确声明;不据此推断已兼容或已测试
静态检查
88 / 100 · 启发式扫描,不代表运行安全
运行环境证据
未执行;当前没有隔离运行测试证据
作者 / 版本 / 许可证
@0-CYBERDYNE-SYSTEMS-0 · 未声明许可证
上下文用量评级
较高消耗
接入复杂程度
需简单配置
是否需要外部接口密钥
不需要
兼容的系统
macOS · Windows
运行环境要求
未检出明确要求
检测到的文件与系统行为
  • 只读
  • 允许写入 / 修改
  • 命令执行
网络行为
仅限本地
安装命令数
无(仅作为资料)

需要注意: 未声明工具权限范围;实际权限由运行环境决定。

作者说明文件(原文) SKILL.md

# AppleScript Automation Mastery

## When to use this skill
- Use when the user request matches this skill's domain and capabilities.
- Use when this workflow or toolchain is explicitly requested.

## When not to use this skill
- Do not use when another skill is a better direct match for the task.
- Do not use when the request is outside this skill's scope.

You are an expert AppleScript automation specialist with deep knowledge of macOS system architecture, multi-display management, URL scheme implementation, and complex workflow orchestration. When this skill is activated, provide enterprise-grade macOS automation solutions.

## Quick Reference - Immediate Patterns

### Application Launch & Position
```applescript
-- Launch app and position on specific display
tell application "System Events"
    set screenWidth to item 1 of (size of main screen)
    set screenHeight to item 2 of (size of main screen)
end tell

tell application "Terminal"
    activate
    if (count of windows) = 0 then make new document
    set bounds of window 1 to {0, 0, screenWidth/2, screenHeight}
end tell
```

### Multi-Screen Window Management
```applescript
-- Get information about all displays
tell application "System Events"
    set allDisplays to every display
    repeat with i from 1 to count of allDisplays
        set displayBounds to bounds of display i
        set displayResolution to size of display i
        log ("Display " & i & ": " & displayResolution)
    end repeat
end tell
```

### URL Scheme Registration
```applescript
-- Register custom URL scheme handler
tell application "System Events"
    make new preference pane at end of preference panes with properties {name:"URL Handlers"}
    tell preference pane "URL Handlers"
        make new URL handler with properties {scheme:"ff-terminal", handler:"/Applications/FF-Terminal.app"}
    end tell
end tell
```

## Multi-Screen Mastery

### Display Detection & Analysis
```applescript
-- Comprehensive multi-display information
on getDisplayInfo()
    tell application "System Events"
        set displayList to {}
        set allDisplays to every display

        repeat with i from 1 to count of allDisplays
            set currentDisplay to display i
            set displayBounds to bounds of currentDisplay
            set displayResolution to size of currentDisplay
            set displayPosition to position of currentDisplay

            set displayInfo to {
                id:i,
                bounds:displayBounds,
                resolution:displayResolution,
                position:displayPosition,
                isMain:(i = 1)
            }

            set end of displayList to displayInfo
        end repeat
    end tell

    return displayList
end getDisplayInfo
```

### Cross-Display Window Positioning
```applescript
-- Position window across multiple displays
on positionWindowAcrossDisplays(appName, windowIndex, displayLayout)
    tell application "System Events"
        set displayInfo to getDisplayInfo()
        set targetDisplay to item displayLayout of displayInfo
        set displayBounds to bounds of targetDisplay

        tell application appName
            activate
            if (count of windows) ≥ windowIndex then
                set windowBounds to {
                    item 1 of displayBounds,
                    item 2 of displayBounds,
                    item 1 of displayBounds + (item 3 of displayBounds - item 1 of displayBounds),
                    item 2 of displayBounds + (item 4 of displayBounds - item 2 of displayBounds)
                }
                set bounds of window windowIndex to windowBounds
            end if
        end tell
    end tell
end positionWindowAcrossDisplays
```

### Layout Templates
```applescript
-- Predefined layout templates for different workflows
on applyLayoutTemplate(templateName)
    tell application "System Events"
        set displayInfo to getDisplayInfo()

        if templateName = "coding" then
            -- Terminal left 50%, Browser right 50% on main display
            positionWindowAcrossDisplays("Terminal", 1, 1)
            delay 1
            positionWindowAcrossDisplays("Google Chrome", 1, 1)

            -- Resize Chrome to right half
            tell application "Google Chrome"
                set mainBounds to bounds of display 1
                set chromeBounds to {
                    (item 1 of mainBounds) + ((item 3 of mainBounds - item 1 of mainBounds) / 2),
                    item 2 of mainBounds,
                    item 3 of mainBounds,
                    item 4 of mainBounds
                }
                set bounds of window 1 to chromeBounds
            end tell

        else if templateName = "research" then
            -- Browser center 60%, Notes right 40%, Terminal bottom strip
            positionWindowAcrossDisplays("Google Chrome", 1, 1)
            positionWindowAcrossDisplays("Notes", 1, 1)
            positionWindowAcrossDisplays("Terminal", 1, 1)

        else if templateName = "data_analysis" then
            -- Terminal left 33%, Data tools right 67%
            positionWindowAcrossDisplays("Terminal", 1, 1)
            positionWindowAcrossDisplays("Numbers", 1, 1)

        end if
    end tell
end applyLayoutTemplate
```

## Application Control & Orchestration

### Advanced App Launching
```applescript
-- Intelligent application launching with state management
on launchAppWithConfiguration(appName, config)
    tell application "System Events"
        -- Check if app is already running
        set appProcess to (first process whose name is appName)

        if appProcess exists then
            -- App is running, bring to front
            tell application appName to activate
        else
            -- App not running, launch it
            tell application appName to launch
            delay 3 -- Wait for app to fully load
        end if

        -- Apply configuration if provided
        if config contains "position" then
            set targetPosition to position of config
            tell application appName
                if (count of windows) > 0 then
                    set position of window 1 to targetPosition
                end if
            end tell
        end if

        if config contains "size" then
            set targetSize to size of config
            tell application appName
                if (count of windows) > 0 then
                    set size of window 1 to targetSize
                end if
            end tell
        end if

        if config contains "fullscreen" then
            tell application appName
                if (count of windows) > 0 then
                    set fullscreen of window 1 to true
                end if
            end tell
        end if
    end tell
end launchAppWithConfiguration
```

### Multi-App Workflow Orchestration
```applescript
-- Coordinate multiple applications in a workflow
on orchestrateWorkflow(workflowSteps)
    repeat with step in workflowSteps
        set stepType to type of step
        set stepApp to application of step
        set stepConfig to configuration of step

        if stepType = "launch" then
            launchAppWithConfiguration(stepApp, stepConfig)
        else if stepType = "configure" then
            configureApplication(stepApp, stepConfig)
        else if stepType = "communicate" then
            set sourceApp to source of step
            set targetApp to target of step
            set dataTransfer to data of step

            transferDataBetweenApps(sourceApp, targetApp, dataTransfer)
        end if

        -- Add delay between steps for stability
        delay 1
    end repeat
end orchestrateWorkflow
```

## URL Schemes & Deep Linking

### Custom URL Scheme Registration
```applescript
-- Register custom URL schemes for deep linking
on registerURLScheme(schemeName, handlerApp, handlerScript)
    tell application "System Events"
        -- Create URL scheme registration
        set schemeInfo to {
            scheme:schemeName,
            handler:handlerApp,
            script:handlerScript
        }

        -- Write to Launch Services database
        do shell script "defaults write com.apple.LaunchServices/com.apple.launchservices.secure LSHandlers -array-add '{\"LSHandlerURLScheme\":\"" & schemeName & "\",\"LSHandlerRoleAll\":\"" & handlerApp & "\"}'"

        -- Refresh Launch Services
        do shell script "/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -kill -r -domain local -domain system -domain user"
    end tell
end registerURLScheme
```

### URL Handler Implementation
```applescript
-- Handle incoming URL scheme requests
on handleURLRequest(scheme, data)
    if scheme = "ff-terminal" then
        -- Parse URL data
        set urlComponents to splitString(data, "/")
        set command to item 1 of urlComponents

        if command = "automate" then
            set automationTask to item 2 of urlComponents
            executeAutomationTask(automationTask)
        else if command = "workflow" then
            set workflowName to item 2 of urlComponents
            runWorkflow(workflowName)
        else if command = "layout" then
            set layoutName to item 2 of urlComponents
            applyLayoutTemplate(layoutName)
        end if

    else if scheme = "automation" then
        -- Handle automation-specific URLs
        processAutomationURL(data)
    end if
end handleURLRequest
```

### Cross-Application Communication
```applescript
-- Enable apps to communicate via URL schemes
on enableAppCommunication(sourceApp, targetApp, messageFormat)
    -- Register URL scheme in target app
    registerURLScheme(targetApp & "://", sourceApp, "")

    -- Configure source app to generate appropriate URLs
    tell application sourceApp
        -- Implementation depends on source app capabilities
        -- Example for browser-based automation:
        set communicationURL to targetApp & "://message/" & urlEncode(messageFormat)
        open location communicationURL
    end tell
end enableAppCommunication
```

## System Integration & Control

### System Preferences Automation
```applescript
-- Automated system preferences configuration
on configureSystemPreference(pane, setting, value)
    tell application "System Preferences"
        activate
        set current pane to pane pane

        delay 1 -- Wait for pane to load

        tell application "System Events"
            tell process "System Preferences"
                -- Navigate to specific setting
                click UI element setting of group 1 of window pane

                -- Set the value
                set value of UI element setting of group 1 of window pane to value
            end tell
        end tell

        quit
    end tell
end configureSystemPreference
```

### Notification Center Integration
```applescript
-- System notifications with advanced options
on showNotification(title, subtitle, message, soundName)
    tell application "System Events"
        -- Create notification
        set notification to {
            title:title,
            subtitle:subtitle,
            informativeText:message,
            soundName:soundName,
            hasActionButton:true,
            actionButtonTitle:"Open"
        }

        -- Display notification
        display notification message with title title subtitle subtitle sound name soundName
    end tell
end showNotification
```

### Finder Operations
```applescript
-- Advanced Finder automation
on automateFinderOperation(operation, source, destination)
    tell application "Finder"
        if operation = "move" then
            move source to destination
        else if operation = "copy" then
            duplicate source to destination
        else if operation = "organize" then
            -- Organize files by type
            set sourceFolder to folder source
            set filesInFolder to every file of sourceFolder

            repeat with fileItem in filesInFolder
                set fileType to file type of fileItem
                set targetFolder to folder fileType of destination

                if not (exists targetFolder) then
                    make new folder at destination with properties {name:fileType}
                    set targetFolder to folder fileType of destination
                end if

                move fileItem to targetFolder
            end repeat
        end if
    end tell
end automateFinderOperation
```

## Error Handling & Recovery

### Robust Error Management
```applescript
-- Comprehensive error handling with recovery
on executeWithErrorRecovery(scriptBlock, recoveryBlock)
    try
        -- Execute main script block
        execute script scriptBlock

    on error errMsg number errNum
        -- Log error details
        log "AppleScript Error: " & errMsg & " (Error " & errNum & ")"

        -- Attempt recovery
        if recoveryBlock is not missing then
            try
                execute script recoveryBlock
            on error recoveryErr
                log "Recovery failed: " & recoveryErr
            end try
        end if

        -- Return error information
        return {
            success:false,
            error:errMsg,
            errorNumber:errNum
        }
    end try

    return {success:true}
end executeWithErrorRecovery
```

### Application State Validation
```applescript
-- Validate application state before operations
on validateApplicationState(appName, requiredState)
    tell application "System Events"
        set appProcess to first process whose name is appName

        if not (appProcess exists) then
            return {valid:false, reason:"Application not running"}
        end if

        tell application appName
            if requiredState contains "window" then
                if (count of windows) = 0 then
                    return {valid:false, reason:"No windows open"}
                end if
            end if

            if requiredState contains "frontmost" then
                if not frontmost then
                    return {valid:false, reason:"Application not frontmost"}
                end if
            end if
        end tell
    end tell

    return {valid:true}
end validateApplicationState
```

## Performance Optimization

### Template Caching System
```applescript
-- Cache frequently used AppleScript templates
property scriptCache : {}

on getCachedScript(templateName)
    if scriptCache contains templateName then
        return scriptCache's item templateName
    else
        -- Load script from file
        set scriptPath to "/path/to/templates/" & templateName & ".applescript"
        set scriptContent to read file scriptPath

        -- Cache for future use
        set scriptCache's item templateName to scriptContent
        return scriptContent
    end if
end getCachedScript
```

### Batch Operations
```applescript
-- Execute multiple operations efficiently
on executeBatchOperation(operations)
    tell application "System Events"
        -- Group similar operations
        set appOperations to {}
        set systemOperations to {}

        repeat with operation in operations
            set operationType to type of operation

            if operationType = "application" then
                set end of appOperations to operation
            else if operationType = "system" then
                set end of systemOperations to operation
            end if
        end repeat

        -- Execute application operations
        repeat with appOp in appOperations
            executeApplicationOperation(appOp)
        end repeat

        -- Execute system operations
        repeat with sysOp in systemOperations
            executeSystemOperation(sysOp)
        end repeat
    end tell
end executeBatchOperation
```

## Best Practices for Agents

### 1. **Always Validate Before Execution**
- Check application availability and state
- Verify screen configurations
- Test URL scheme registration

### 2. **Use Progressive Disclosure**
- Start with simple operations
- Build complexity gradually
- Provide clear feedback at each step

### 3. **Implement Robust Error Handling**
- Always include try-catch blocks
- Provide meaningful error messages
- Include recovery mechanisms

### 4. **Optimize for Performance**
- Cache frequently used scripts
- Batch operations when possible
- Minimize delays between operations

### 5. **Maintain Security**
- Request permissions before sensitive operations
- Validate user input and parameters
- Use secure communication methods

## Integration with FF-Terminal Tools

This skill integrates seamlessly with existing FF-Terminal tools:

- **macos_control**: Use for direct AppleScript execution
- **workflow_automation**: Combine with workflow orchestration
- **bash**: Use for system-level operations alongside AppleScript

When implementing AppleScript solutions, always consider how they can be enhanced through FF-Terminal's broader automation ecosystem.

讨论

基于 GitHub Discussions。登录 GitHub 即可参与讨论、点赞、订阅更新。