Applescript to Objective-C translation tool?

I don’t need it right now but does it exist? A tool to translate AppleScript into Objective-C?

Hmmm… I don’t believe this to be possible, just based on the very fundamental differences in both languages. If a tool were to be devised, I think the potential to do it for a substantial potential clientele is very, very low. Plus, AppleScript is mostly there to drive other apps, as objective c can do so much more. I think it would be faster and less time consuming to convert the code manually, IMHO.

Don’t think there is, nor will ever be one. Sorry!

Browser: Safari 6533.18.5
Operating System: Mac OS X (10.6)

It is possible but it is a lot of work. The tool doesn’t have to translate everything, it could skip the untranslateable parts like scripting additions.

Yes, but it would help if a tool does the boring stuff like translating “set x to y” into “x = y;” and “on applicationWillFinishLaunching_(aNotification)” into “- (void)applicationWillFinishLaunching:(NSNotification *)aNotification”.

Ok, I’ll create my own private tool. It is easier if it only has to do what I need. I see two uses: ASOC classes and Scripting Bridge. Scripting Bridge is probably never going to work, but I like a challenge.

I too am also extremely pessimistic it would work.
For example in applescript you can: “set aNumber to 3.1415” and later “set aNumber to 3” and that’s OK.
But in Obj-C, you have to know what sort of number you are dealing with. Float, int, long, etc. So that simple Applescript statement COULD potentially be:

int aNumber = x;
float aNumber = x.xx;
long aNumber = xxxxxxxxxxxxxx;

How would you know which one, if your function was getting a value from some other input?
Or would you try to use NSNumber for everything? Even using that there are all these methods for which you can make an NSNumber object from a different value:

  • numberWithBool:
  • numberWithChar:
  • numberWithDouble:
  • numberWithFloat:
  • numberWithInt:
  • numberWithInteger:
  • numberWithLong:
  • numberWithLongLong:
  • numberWithShort:
  • numberWithUnsignedChar:
  • numberWithUnsignedInt:
  • numberWithUnsignedInteger:
  • numberWithUnsignedLong:
  • numberWithUnsignedLongLong:
  • numberWithUnsignedShort:

Here’s another one: consider strings and text in ASOC and then consider NSString and NSMutableString

I don’t wanna dissaude you from trying, but I think that Applescript is SOOO forgiving, that it’s simplicity of data typing can’t be easily translated to Obj-C without strongly typing (declaring) what any particular variables can be. And that’s just one aspect of difficult challenges.
It’s still an exercise for a trained mind to do this.

I can keep it simple: If the number has a decimal point, the type is CGFloat. Without a point the type is NSInteger. If a variable is used for both, the type is CGFloat. If a variable is used for all kinds of data, the type is id.
In theory: if I can translate it myself, I can make a program that does the same. If the AppleScript compiler can understand it, it is translatable by a program.
I’ll see how far I get, just for fun.

I think that GREP could be useful in this case. If you succeed, the one thing I fear is that you’re gonna end up with a mess of Applescript and Cocoa, it may be confusing to look at…

I still think that redoing the code from scratch is the best way, since I would do code optimization/cleanup while translating from AS.

Anyways, good luck!

You should…


set x to 1.5 --real
set y to 10 --integer
set z to x + y

can indeed be translated to:

what if our AppleScript above was:

set x to "Spam" --let's pretend this is importand ;).
set x to 1.5 --real
set y to 10 --integer
set z to x + y

It won’t work because id’s only works for objects (structs) and doesn’t work for (C’s) data types.

True, but an AppleScript compiler is an interpreter and not a translator. That also works in his own virtual machine in a world following his own rules. Objective-C is a C utility running with his own run-time to get a scripting-like feeling. Those worlds are so different that you can’t talk about a translation but more like code analyzer and creating new C/Objective-C code.

Good luck!

Hi Chocoholic,
Dd you progress on your translation tool?
If you need help debugging etc I am available and interested.

Hello Chocoholic,

The idea of a “language translator” is an old idea. For example for natural languages, linguists and programmers have been working on it since the '70: see the performance of Google Translator to have an idea of the state of the art in the 21st century. :wink:

For programming languages with a stricter grammar and a defined set of keywords, it would seem easier, but in fact every language has its own particularity, for example in variable types, type-casting, dynamic memory management, object-orientation and so on. You could understand what this could mean:

but you would have to re-formulate it into Objective-C for example.

I have made some program “translations” so far:

FORTRAN → Pascal → ASOC → Objective-C. Restarted from scratch each time, but the ASOC → Objective-C step is the most easy part (the closest from your idea, assuming there is no other application involved).

Applescript → ASOC. This is OK, but this is a big change, and from ASOC only parts can be translated into Objective-C. There is nothing like the “current selection of Finder” in Obj-C.

Back to the '80, we had a “home-made” pre-processor which generated code sources for different machines (DEC, CDC, and so on) because an INTEGER was treated differently on them. It was a big work, and for the same language (FORTRAN).

So you have a lot of predecessors for your idea, I am just wondering, as other did, if the time you will spend to make it work would not be longer as rewriting (and of course optimizing) your original code.

Anyway, I wish you good luck and a lot of patience. Keep us informed, and above all, remember that caffein does NOT replace sleep.:wink:

This may be of interest to you:
http://appscript.sourceforge.net/tools.html#astranslate

ASTranslate.
A simple exploratory tool for translating application commands from AppleScript to Python, Ruby and ObjC appscript syntax.

http://downloads.sourceforge.net/project/appscript/ASTranslate-0.5.2.zip

hope that help
:o)
Carlos