Search code examples
user-interfaceapplescript

AppleScript - how to have actions ignore the tell statements they are nested within?


I think that I am using ignoring application responses incorrectly.

What I'd like to do is to have nested actions which do not rely on being scripted by the applications they are nested within.

In the example below, there is a repeat loop that depends on application "System Events" and application process myApp, etc. But whatever actions that are within this loop, I'd like these to ignore application "System Events" and application process myApp, etc. How do I achieve this?

set myApp to "someApp"
set pPath to POSIX file "/Volumes/myDisk/outputPath"

tell application myApp to activate

tell application "System Events"
    tell application process myApp
        tell window myApp

            --some code here

            repeat while progress indicator 1 of sheet 1 exists
                ignoring application responses
                    set newPath to POSIX file pPath as alias
                    set currentDate to current date
                end ignoring
            end repeat

            --some code here

        end tell
    end tell
end tell

The error that is returned:

get POSIX file (file "myDisk:outputPath:") of application process "somApp"
Result:
error "No result was returned from some part of this expression."

Here, I would've expected get POSIX file (file "myDisk:outputPath:") of application process "somApp" to just be get POSIX file (file "myDisk:outputPath:").


Solution

  • It all depends on who owns the command. Knowing who owns the command, you can redirect it to the correct application (or to the frameworks) with a nested tell sentence. For example, here I am trying to redirect the (current date) command to the frameworks of the installed script additions.

    set myApp to "someApp"
    set pPath to POSIX file "/Volumes/myDisk/outputPath"
    set newPath to pPath as alias
    
    tell application myApp to activate
    
    tell application "System Events" to tell process myApp to tell window 1
        
        --some code here
        
        repeat while progress indicator 1 of sheet 1 exists
            tell scripting additions to set currentDate to current date
        end repeat
        
        --some code here
        
    end tell
    

    But that won't always help. The correct and stable solution is to "decouple" nested tell blocks and transform them into separate tell blocks. In your example, I would do it like this:

    set myApp to "someApp"
    set pPath to POSIX file "/Volumes/myDisk/outputPath"
    set newPath to pPath as alias
    
    tell application myApp to activate
    tell application "System Events" to tell process myApp to tell window 1
        --some code here
    end tell
    
    set indicatorExists to true
    repeat while indicatorExists
        tell application "System Events" to tell process myApp to tell window 1
            set indicatorExists to progress indicator 1 of sheet 1 exists
        end tell
        set currentDate to current date -- now this command is not nested
    end repeat
    
    tell application "System Events" to tell process myApp to tell window 1
        --some code here
    end tell