Automating native macOS file dialogs with nativeType()

Last edited on

Introduction

Currently, Squish cannot hook into the native macOS file dialogs. Nonetheless, it is still possible to automate interaction with these dialogs using the nativeType() function.

Example:

def main():
    # ...
    # the file dialog gets opened here

    # Snooze long enough for the dialog to pop up and be ready
    snooze(2)
    # Open path input field:
    nativeType("<Command+Shift+g>")
    # Wait long enough for the sheet to appear
    snooze(1)
    # Enter desired path:
    nativeType("/Users/myuser/temp.txt")
    # Give the dialog a moment's pause
    snooze(1)
    nativeType("<Return>")
    # Give the dialog a moment's pause
    snooze(1)
    # Close the dialog
    nativeType("<Return>")
    # Give the AUT a chance to handle the closing of the file dialog
    snooze(1)

⚠️ The snooze() statements are important to avoid sending the key presses before the dialog appears, or faster than the dialog can process them.

⚠️ The Command+Shift+g key press causes the “Go to the folder” sheet to appear: