---
title: Using AppleScript with Selenium WebDriver
description: AppleScript or osascript can be used in combination with Selenium WebDriver
  to automate on macOS.
source_url:
  html: https://testingbot.com/support/web-automate/selenium/osascript
  md: https://testingbot.com/support/web-automate/selenium/osascript/index.md
---
# AppleScript

AppleScript (or osascript) is a scripting language which comes by default on macOS.   
 It allows for automating various (native) macOS applications and can be used in combination with Selenium WebDriver  
 via TestingBot's custom `tb:osascript` command.

In the example below, you'll find a simple AppleScript example which will open Safari and enable automation.

    tell application "Safari"
    launch
    activate
    tell application "System Events"
      set v to (value of attribute "AXMenuItemMarkChar" of
      menu item "Allow Remote Automation" of menu "Develop" of
      menu bar item "Develop" of menu bar 1 of application process "Safari")
      if v is missing value then
            click menu item "Allow Remote Automation" of
            menu "Develop" of menu bar item "Develop" of menu bar 1 of application process "Safari"
          else
            log "OK"
      end if
    end tell
    quit
    end tell

## Running AppleScript during a Selenium WebDriver test

TestingBot has created a custom command, `tb:osascript`, which you can use to pass in your specific AppleScript at any point during your (macOS) WebDriver test.

For example, you can use AppleScript to automate a native dialog appearing during your test, such as a file upload dialog from the browser.

You can specify multiple different AppleScripts at any point during your test. The session will wait until the script has ended, before proceeding to the other WebDriver commands.

[Java](https://testingbot.com#)[Python](https://testingbot.com#)[NodeJS](https://testingbot.com#)[Ruby](https://testingbot.com#)[PHP](https://testingbot.com#)[C#](https://testingbot.com#)

    String appleScriptContent = "display alert \"Success!\"";
    Map<String, Object> osaMap = new HashMap<>();
    osaMap.put("script", appleScriptContent);
    ((JavascriptExecutor) driver).executeScript("tb:osascript", osaMap);

    osa_script_content = "display alert \"Success!\""
    driver.execute_script("tb:osascript", {
      "script": osa_script_content
    })

    await driver.executeScript('tb:osascript', {
      "script": 'display alert "Success!"'
    });

    applescript_content = <<-EOF
      display alert "Success!"
    EOF
    driver.execute_script("tb:osascript", { script: applescript_content })

    $osaScript = 'display alert "Success!"';
    $driver->executeScript("tb:osascript", ["script" => $osaScript]);

    var scriptArgs = new Dictionary<string, object>
    {
        ["script"] = "display alert \"Success!\""
    };
    ((IJavaScriptExecutor)driver).ExecuteScript("tb:osascript", scriptArgs);

Was this page helpful? Yes No 

## Looking for More Help?

Have questions or need more information?   
 You can reach us via the following channels:

- [Email us](https://testingbot.com/contact/new)
- [Join our Slack Channel](https://join.slack.com/t/testingb0t/shared_invite/zt-3bcw9xch-jk19~6XPs_xBrsAgAedkCw)
