Integrate applescript into a Swift app

Hello everyone

I’m starting to master Applescript a little for my own needs and I’m starting to learn Swift (which I don’t yet master). I would like to create a small Mac application that integrates Applescript code. I’m having a hard time finding information on this. Is it possible?
For example, how to integrate a small applescript program into a swift function?
Example of a small basic applescript program to integrate into a button so that the finder opens a folder :

tell app “Finder”
set dossier to folder “nomDossier” of desktop
open dossier
end tell

I tried everything, it doesn’t work. Do you have a tip?
Thank you for your attention
(this is a translation, I am French ;-))

Basically the question is : is it possible to create a desktop application with the swift language on Xcode by integrating Applescript code for actions?

Did you try NSAppleScript?

My Swift knowledge is only superficial but as far as I understand you should be able to use Cocoa classes.

Hi Leo_r
I don’t know. Maybe. How to use NSApplescript?
I would really like to find an example of integration on the internet but I can’t find it. I’m currently learning to program in Swift to create simple applications for macOS and be able to use Applescript by pressing the buttons on the interface. But I think it’s hard to find :wink:

Here’s search results for “NSApplescript examples” (the first result is actually exactly what you ask):

Developer docs:

If after exploring all this and trying some basic implementation you have any specific questions, feel free to ask.

(It’s possible that other forum members will offer more ready-to-use solutions).

Thank you Leo for your help!
Some links seem interesting.
It seems it’s not so easy as I felt.
I’m going to look into it this weekend

Here is an Xcode example application: GitHub - hhas/Swift-AppleScriptObjC: How to call AppleScript handlers from Swift via AppleScript-ObjC bridge.

Tip: Close the music app bevor you runnig the example.

Edit: The example was created in an older macOS version. Under Sonoma you should set the type for “isRunning” to Bool in the MusicBridge.swift:

var isRunning: Bool { get }

and comment out this line:

//var isRunning: Bool {return self.isRunning == 0 ? true :false }
1 Like

Thank you so much, Dirk! This is a good example! but it’s quite difficult for me to understand exactly what it does. I thought I could simply create a MacOs interface, write a few lines of swift code and press a button to launch an Applescript but it doesn’t seem that simple. I’m looking for a tutorial but apparently not many people do it.

Dirk posted a great example. But I also think that it’s indeed too complex to understand for a beginner. Plus, once again, for your simple task of running a few lines of AppleScript code, NSAppleScript is all you need.

In my earlier post, I already posted a search result with this hit as the first one:

As far as I understand, it already includes all you need to run AppleScript from Swift via NSAppleScript.

The question is: do you already know how to create a UI with a button in Swift and assign a Swift function to this button?

If you already do know how to achieve this task, then the aforementioned code sample should provide all you need - you basically only need to replace the AppleScript code with your own.

If you’re still at a learning stage where you don’t know how to assign a basic Swift function to a button, then you need to concentrate on this task first. Once you master this, you’ll be able to easily assign any Swift code to your button - including the one that contains NSAppleScript functions.

Leo is right. To “just” execute a script, this is perfectly sufficient.

That’s also true. Once you’ve started programming in Swift, there’s not much reason to fall back on AppleScript. The only one I can think of is the control or interaction with other programs. That’s why I found the example exciting and had experimented with the bridge. Maybe this little example will help you to understand a few things: (updated) (433.4 KB)

It’s show:

  • How to integrate a script file into the app (AppDelegate.swift)
  • How to declare an interface to be able to call the Applescript handlers directly in Swift (ScriptBridge.swift)
  • How to build set/get handlers and handle lists, records and passing multiple parameters (ViewController.swift / ScriptBridge.applescript)

However, after I found out everything, I didn’t continue and never used it.

1 Like

Interesting. When I open Swift-Storyboard-Script in Xcode and try to execute I get a build failed message.

What is the error? Everything works for me:

You may have to adjust the team settings:

I have set it to “none” and uploaded again.

It works for me! (changing signature) thank you very much for your help.
I haven’t had too much time to integrate and analyze it yet but I’ll let you know very soon if I can do anything with it.

If I absolutely want to integrate applescript into a program it is because my needs are, as you say, exactly the interaction between programs and I know how to do it very well with Applescript.
I can do it! :innocent:

That was exactly my approach. You could probably implement this directly in Swift with the Accessibility interface, but AppleScript is (for me) easier. That’s why I experimented with it years ago, but ultimately never used it.
At first I had problems understanding that you have to put an underscore “_” after the name for each parameter in the AppleScript handler name:

to doFunction_(param)…
to doFunctionMultipleArgs___(args1, args2, args3)…

and had implemented the JSON serialization in order to be able to handle the transfer via one parameter if required. This site has helped me a lot:

Objective-C to AppleScript Quick Translation Guide

With the example project as a template, however, you should be able to implement everything you have in mind, since you are integrating a “real” AppleScript with which you can then also interact.

1 Like

Hello Dirk & Leo
thank you for the help you give me. However I tried several methods with a simple test in Applescript to open a folder on the desktop for example (I would obviously want to create something more complex afterwards). It’s true that I don’t yet master Swift very well but I know how to assign a function to a button. However, none of the solutions on the links you offered me seem to work. Here is a screenshot with a different suggestion per button… Have I forgotten something?

Ideally, you’d want to check for errors and evaluate them:

This script will never compile because of undefined variable message:

If you check for errors, you’d catch it right away.

Also, compileAndReturnError: only compiles the script; you need executeAndReturnError: to run the script.

I replaced “compileAndReturnError” with “executeAndReturnError(nil)” but it doesn’t work.
Maybe it would be better to wait until I master Swift a little more to understand everything that’s going on.

I know how to automate my entire Mac very well with very complex Applescript codes but it’s impossible to create a small graphical interface with a button that launches all that… I’m a little discouraged :crazy_face: :neutral_face:

unless you fix the AppleScript script itself as i described in my previous post, it won’t compile and won’t run.

Yes, you were absolutely right. I actually modified it with a simple: display dialog “ok”
But the error message is still: “This method should not be called on the main thread as it may lead to UI unresponsiveness”

This particular message is, I think, just a warning but regardless: yes, ideally you need to make sure everything UI-related is done on the main thread (this includes any dialogs/alerts).

[see below for updated answer]