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
activate
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 ;-))
Jérôme
Basically the question is : is it possible to create a desktop application with the swift language on Xcode by integrating Applescript code for actions?
THANKS
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
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:
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!
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:
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.
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?
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
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).