Skip to content

jo-tools/applescript-automation

Repository files navigation

AppleScript-Automation

Xojo example project

License: MIT

Description

There used to be a time where any application just could execute AppleScripts to Automate other applications such as: tell application X to do Y.
That's no longer the case starting with macOS 10.14. It's become a bit trickier to do that with a Xojo built application, since not everything is built in.

This example project shows:

  • How to use AppleScript for Automation in a Xojo built application by providing an Info.plist with NSAppleEventsUsageDescription.
    Without that, nothing will work at all.
  • How to determine if the application has permission for AppleEvents / Automation (AEDeterminePermissionToAutomateTarget).
  • Explains how to deal with the permission results.
  • In case you determine the application has no permission:
    How to open System Preferences at Privacy - Automation (so that the user can allow the app).

Additionally:

  • Check if an Application is running (find .app by BundleIdentifier )
    and if not: launch it.

ScreenShots

Automate Terminal.app by executing a Shell command in Terminal.app
ScreenShot: AppleScript-Automation - Automate Terminal.app

Determine Permission to automate Terminal.app
ScreenShot: AppleScript-Automation - Determine Permission to automate Terminal.app

Xojo

Requirements

Xojo is a rapid application development for Desktop, Web, Mobile & Raspberry Pi.

The Desktop application Xojo example project applescript-automation.xojo_project is using:

  • Xojo 2023r3
  • API 2

How to use in your own Xojo project?

  1. Drag the example Info.plist into your project.
    Edit it and change the NSAppleEventsUsageDescription to fit your app's purpose.

  2. To determine Automation-Permission and opening System Preferences at Privacy - Automation

    • copy-and-paste the Module modAppleScriptUtils into your project.

    If you want to check for an .app running (and launching it if required) then additionally

    • copy-and-paste the Module modMacOsApplications into your project.
  3. Use the provided Methods in the Modules in a similar way to this example project.

  4. If you are CodeSigning (with hardened Runtime) and/or Notarizing your application:
    Make sure to have the Entitlement enabled: com.apple.security.automation.apple-events

About

Juerg Otter is a long term user of Xojo and working for CM Informatik AG. Their Application CMI LehrerOffice is a Xojo Design Award Winner 2018. In his leisure time Juerg provides some bits and pieces for Xojo Developers.

Contact

E-Mail    Follow on Facebook    Follow on Twitter

Donation

Do you like this project? Does it help you? Has it saved you time and money?
You're welcome - it's free... If you want to say thanks I'd appreciate a message or a small donation via PayPal.

PayPal Dontation to jotools