incorporating other languages into Applescript

Generally, most of the stuff you can do with programming languages like Java, C++, or Perl can be done with applescript. Some of the stuff takes a lot more time to write, though.
For instance, Java has a int.toBinaryString() to convert an integer to binary. To write a binary converter in applescript would be time consuming and confusing. If anyone knows how to take the result of another language’s command and use it in your applescript, I would be thankful.

Hi scriptWizard,

Integer to binary is not that hard. One thing you need to look out for is limits on integers. There are a lot of scripts already made. Search this bbs for a subroutine.

gl,

I’ll take your advice. However, there’s still countless things that you could do more easily if you use another language’s command. If you know of any way to do that, please let me know.

  • Most common approach is to invoke other languages via the Unix command line using the ‘do shell script’ command, though shifting large or complex data in and out that way is a PITA. There’s no shortage of documentation, discussions and examples for this.

  • Another option is to write a C-based scripting addition. Don’t have any links to hand, but if that’s your interest I’m sure some can be provided.

  • A third option is to write a scriptable agent (a.k.a. faceless background application, or FBA). You can do that in ObjC, Java, or any other language with suitable Apple event handling infrastructure. For example, the TextCommands and ParserTools FBAs on osaxen.com are written in Python using the sfba (‘scriptable FBA’) module included in the appscript package (link in my sig), and are mostly just simple wrappers around existing Python functions and modules. e.g. TextCommands’ ‘unicode numbers’ command is implemented as:

def unicodeNumbers(text):
    return [ord(c) for c in text]

installeventhandler(
        unicodeNumbers,
        'TeCoUnum',
        ('----', 'text', kAE.typeUnicodeText)
        )

sfba provides automatic unpacking and packing of parameters and results. The only other thing needed is to define the corresponding terminology in an aete and/or sdef:

<command name="unicode numbers" code="TeCoUnum" description="Convert Unicode text to a list of integers.">
    <direct-parameter type="string" description="The Unicode text."/>
    <result type="list of integer" description="A list of integers in range 0-65535."/>
</command>

HTH

Oh, and don’t forget that other languages can do application scripting too (Perl and Python have the most advanced bridges, see Mac::Glue and appscript respectively). Depending on what you want to do, you might find it easier to do everything there instead.

The only area other languages really fall short in is OSA language component support, needed for attachability (e.g. iCal alarm scripts), though you can get components for several languages with basic load/store/compile/execute functionality here. (There’s also JavaScriptOSA which has extensive OSA support, but its application scripting support was pretty flawed and only semi-usable last time I looked and I don’t know if it’s improved at all since then.)

It depends what you think is easy. For instance your integer to binary string is kind of easy if you know the algorithm. Here’s the raw script:

set i to 255
set n to i
set b to {}
repeat until n = 0
set r to n mod 2
set beginning of b to r
set n to n div 2
end repeat
return {b as string}

It’s the same thing in c or Pascal or any other language. You can wirte the program or use built in functions.

With AppleScript you have built-in functions (or add on functions) with scripting additons or load libraries of subroutines. One good scipting addition for math is Santimage osax. These are all AppleScript stuff, but as hhas said you can also use unix commands with ‘do shell script’ and access other scripting languages. Using ‘do shell script’ adds an overhead of accessing the shell. Simple subroutines like this, I think I’d rather use AppleScript.

gl,

It depends what you think is easy. For instance your integer to binary string is kind of easy if you know the algorithm. Here’s the raw script:

set i to 255
set n to i
set b to {}
repeat until n = 0
set r to n mod 2
set beginning of b to r
set n to n div 2
end repeat
return {b as string}

It’s the same thing in c or Pascal or any other language. You can wirte the program or use built in functions.

With AppleScript you have built-in functions (or add on functions) with scripting additons or load libraries of subroutines. One good scipting addition for math is Santimage osax. These are all AppleScript stuff, but as hhas said you can also use unix commands with ‘do shell script’ and access other scripting languages. Using ‘do shell script’ adds an overhead of accessing the shell. Simple subroutines like this, I think I’d rather use AppleScript.

Editted: I almost forgot. Here’s an article on writing to file:

http://www.satimage.fr/software/en/as_data_files.html

gl,

Easy is a simple call to a robust, efficient, exhaustively-tested function which already exists. Anything else is by definition harder and more complex, and therefore more time consuming and another potential source of bugs in your program (e.g. your example had two; it’s very easily done:). And while I’d agree an intToBinaryStr() isn’t that hard to write, even once you add paranoid parameter checking code and error reporting, I’m guessing the OP has lots of other functions in mind too, and it all adds up in the end. Plus there’s a lot of things AS simply can’t do, or not do quickly enough to be practical (most string processing operations). So the OP’s desire to relentlessly reuse what’s already been done is not an unreasonable one. The art of programming is doing as little work yourself as is humanly possible. :slight_smile:

I suspect the OP was really hoping for a foreign language interface that’d allow them to invoke Java/C/etc. functions directly, but AS doesn’t provide one itself, alas. Though if you’re writing a Studio-based app, Studio does provide a ‘call method’ command that lets you invoke ObjC class and instance methods, and it’s easy to create a faceless background Studio app allowing you to use its ‘call method’ command from regular scripts. So that’s one more for the list of options.

Yep. It’s well worth checking out what’s already available; while AS’s standard and third-party extension support is a mere blip next to that of Java/C/Perl/etc., which spoil you rotten, there’s still some good stuff around.

Cheers

Thanks so much for your suggest, people. I’ve got one more question though.

If you were to use the “do shell script” command, how would you access another language through UNIX? I’m familiar with UNIX a fair amount. I even wrote a program using Applescript Studio that is a terminal, except you can have a multi-line unix script. But, I don’t know how to access another language through unix. If you know, please let me know:)

Well, basicly the solution is that using these other languages, you write a program/script that reads its input from command line arguments. The way this is done varies from one language to another, but should be fairly simple. Then you just call that program with a do shell script using some arguments.

For example, you have written a program called calculateSum that takes takes two arguments, adds them together, and returns the sum. You can run it in the Terminal by typing:
calculateSum 4 7
And would get 11 as a result.

Then you can use this program also from an AppleScript with something like this:
set theSum to do shell script “calculateSum 4 7”
In this case, theSum gets the value 11.

This is the version of Kel’s script that I use. Does it have serious problems I don’t see?

[Edit] - I’ve cast it in his terms, same algorithm

set i to 299.23 as integer
set n to i
set b to ""
repeat until n = 0
	set r to n mod 2
	set b to (r as text) & b
	set n to n div 2
end repeat
b as integer --> 100101011

Salli:
You said that if you had a program blah and it takes parameter a, from terminal you could do

blah 3

and it would give the result, for any application blah?