0

I'm trying to call a function/handler from a tell block, but AppleScript keeps throwing an error:

Result: error "AppleEvent handler failed." number -10000

The script fails where I'm calling start_varnish():

tell application "System Events"
    set iTermIsRunning to exists (processes where name is "iTerm")
end tell

tell application "iTerm"
    activate

    try
        tell current terminal
            if iTermIsRunning then
                launch session "Default Session"
            end if
            tell the last session
                my start_varnish()
            end tell
        end tell
    on error err
        log "Reopening iTerm2"
        reopen
        tell current terminal
            tell the last session
                my start_varnish()
            end tell
        end tell
    end try

end tell

on start_varnish()
    set name to "Varnish Cache"
end start_varnish

When I move the contents of the handler and place it directly in the code above, it works fine, but I would like to wrap the functionality in a handler for reusability.

josef.van.niekerk
  • 11,941
  • 20
  • 97
  • 157

1 Answers1

0

The closest fix I could find looks as follows:

tell application "System Events"
    set iTermIsRunning to exists (processes where name is "iTerm")
end tell

tell application "iTerm"
    activate

    try
        tell current terminal
            if iTermIsRunning then
                launch session "Default Session"
            end if
            my start_varnish()
        end tell
    on error err
        log "Reopening iTerm2"
        reopen
        my start_varnish()
    end try

end tell

on start_varnish()
    tell application "iTerm"
        tell current terminal
            tell the last session
                set name to "Varnish Cache"
            end tell
        end tell
    end tell
end start_varnish

I'm not sure if the scoping in my original question for cell current terminal and tell the last session is understood inside the context of the start_varnish() handler.

josef.van.niekerk
  • 11,941
  • 20
  • 97
  • 157