---
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.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.

```applescript
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#)

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

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

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

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

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

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

### Looking for more help?

Have questions or need more information? Reach out via email or Slack.

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