Integrate applescript into a Swift app

P.S. wait… now i noticed the error actually said this method should NOT be called on the main thread. So scratch whatever i said before. this error message is still just a warning but it looks like Swift wants you to run this, preferably, on a background thread i.e. via NSOperationQueue etc. you’ll need to read Swift docs for more

Thank you, Leo.
Well, I can’t do it. I’m going to wait until I’m a bit more comfortable with the Swift language, for which I’m too much of a beginner because I don’t understand everything that’s going on and it’s too complicated for me to understand and resolve the errors there. There are a lot more parameters than I thought.

As soon as tasks are involved, you will probably always have the problem that GUI calls have to be made in the main thread (incidentally, this is also the case in C# under Windows). For this there is the function performSelectorOnMainThread which you might even have to call in AS and at the latest then it gets really complicated with “Script-Code in Strings”.
That’s why I found the example with the direct integration of a script file so exciting.

Here is another example of the things you have tried: (446.0 KB)

1 Like

Well that’s exactly what I wrote in my original reply (which I then mostly deleted).

Because I re-read the error message again:

“This method should not be called on the main thread as it may lead to UI unresponsiveness”

The method should NOT be called on the main thread.

I guess it’s a Swift thing or something else is involved that makes it undesirable to run this method on the main thread.

I still think it’s just a warning (that is, this error doesn’t prevent the method from running), not completely sure. But, like I mentioned, the error suggests that ideally he should run this method on a background thread via NSOperationQueue or other methods.

1 Like

You are right. However, in my opinion it should be the complete opposite for some commands in AppleScript, since it is a single thread concept .
Be that as it may. It works with the direct integration of a script file without any problems.

The problem with the main thread in AppleScript relates specifically if you use ASObj-C calls. Probably because in Object-C and Swift changes to GUI elements may only be made by the main thread. The message “This method should not be called on the main thread…” probably appears when calling some methods in Swift (executeAndReturnError etc) as a general warning. This may have been implemented together with Async / Await functionality. Unfortunately, I’m not very familiar with Swift as I rarely use it.

But somehow two different concepts (single thread vs. multi-thread) collide in Swift & AppelScript, so that it makes no sense to really deal with them in detail. It works when you need it, but beautiful is something else.

It’s great! I think it’s just what I’ve been looking for!
And it’s even possible to pass variables to Applescript routines, which is great. I didn’t understand the whole process to do it again myself and understand line by line what’s going on because I’m at the very beginning of learning Swift (harder than Applescript!) but I understand better how to pass from a button action to an Applescript command through an Applescript file.
I’m reassured, for a moment I thought it wasn’t possible…
Very cool! I’ll look into it!
That’ll keep me busy for a while…
Thanks for your help, Dirk