Swift in Applescript/Applescript in Swift?

I have an Applescript that does get some values from web pages and store them in a csv file. It now works very smoothly and I am currently teaching myself how to make that into a Cocoa-Applescript application with an interface for input of other variables. Compared to my previous “solution” of taking timed screen grabs, extract the important parts via PS script and run it through an OCR program, Applescript is godsend.

I also have made a Swift project that does a lot of calculations on the same type of values that I get from the Applescript and do I so fast, at least compared to other options I have tried, first Excel and then R (I mean EXTREMELY fast. I have very little coding experience and the basic calculations haven´t changed that much, except for the most obvious. What took 20 minutes in Excel and five in R take four seconds with a compiled Swift program)

So I have two very good solutions to two part of a problem: Applescript to interface with the world and Swift to do a lot of number crushing. What I really want is to merge the two. Preferably “Swift inside Applescript”: have the Applescript do its first part, then call my compiled Swift program with some values and use the returned calculated values for the last part of the Applescript.

Is that doable at all? Yes I know I am not posting any code but my question so far is not so much “how” but if I am trying what amounts to plugging a Display Port cable into the electric socket and I should find another solution instead.

Model: Macbook Air 2011
Browser: Safari 600.1.25
Operating System: Mac OS X (10.10)

Hi,

here are some suggestions: http://macscripter.net/viewtopic.php?id=43127

WAY over my current level of knowledge.:stuck_out_tongue: But thanks :slight_smile:

I might be forced to educate myself enough to be able to implement it in my project. But it looks like it is “Applescript inside Swift” while the ideal situation would be “Swift inside Applescript”.

What about Swift scripts like described here: http://practicalswift.com/2014/06/07/swift-scripts-how-to-write-small-command-line-scripts-in-swift/? It looks like I should be able to run a script in termnial I can call from Applescript with parameters and use the result as variables in the Applescript.

Again, I am not asking how to do it but if I am on the right track or have misunderstood it completely.

Yes, you can call shell scripts written in Swift with the method described in that article, but omit the -i switch in the first line

Have you considered rewriting the Swift part in Objective-C/C? It might well be the simplest solution.

I am becoming more and more convinced that I ought to go that way, even if I am hugely intimidated by the syntax in Objective C. Compared to that Swift is as intuitive as good old basic and scripting languages like Applescript, R and VBA. It literally took 30 minutes to rewrite my 100 line R script to Swift including some important code optimization. I fear what it will take to rewrite it in Objective C.

But to understand this correctly: In an Applescript Cocoa project I can do all the low CPU intensive interface parts in Applescript and still let compiled Objective C code do the heavy CPU intensive lifting when needed? Most of the time a Applescript loop will simply be checking for updates of a web page and only when that happens will I need it to run a CPU intensive calculation.

And now that I noticed your name :slight_smile: :

Will either of your books on ApplescriptObjC help me “get” AppleScriptObjC enough to come up with the necessary understanding of how to bridge Applescript and Objective C to find a solution ? (in your answer assume normal intelligens :wink: )

To be more specific: The “CPU intensive” parts job is “only” to do some intensive manipulation on some variables given to it by the Applescript part and give the result (a handful integers) back to the applescript part for further processing. It does not in itself interface with anything but the variables givet to it.

Exactly.

Bridging the two is what it’s all about, in a sense. ‘AppleScriptObjC Explored’ covers how to build ASObjC-based apps in Xcode. The way you call your own included Objective-C code is much the same as you call Cocoa’s Objective-C code.

What sort of calculations are you doing?

It is large Monte Carlo simulations. With the use of Applescript and “real” programming languages we will soon be able to do in real time with minimal interaction what we up until two weeks ago had to use hours if not days to achieve and now have much more time for the analysis instead of the data collection.

Thumbs up for “AppleScriptObjC Explored”. You sir is an excellent teacher. I bought it a few hours ago and now just by quickly going through the first example I already have a much firmer grasp of bindings instead of the “trial and error without learning” method I have been using for the last week.

Thanks for the kind words.

You’ll probably be able to find some Monte Carlo simulation code in C on the internet – then it’s just a matter of putting an Objective-C wrapper around it, to wrap the primitive values as Cocoa objects. Good luck!