Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 176 lines (99 sloc) 7.682 kb
9b4b3c75 »
2009-04-24 Reformatted README in Markdown so it looks nicer on github.com
1 What is JSTalk?
2 ===============
3
4 JSTalk is how I imagine AppleScript would be done today, if it was written by a Cocoa programmer who was smitten by Cocoa and Objective-C.
5
6 JSTalk's goal is not to kill off or replace AppleScript, but to provide a compelling alternative by blending together existing technologies.
7
8 JSTalk comes with a command line tool (jstalk), an editor (JSTalk Editor), and a framework that programmers can include in their application to add scripting support with a single line of code.
9
10 And finally, JSTalk is open source. So if there is something wrong, or it's not running the way you expect it, you get the chance to peek under the covers and understand what's going on.
11
12 Here's the git repository: <http://github.com/ccgus/jstalk/tree/master>
13
08f3bfc6 »
2009-09-11 Changed the URL download location, and changed some "NewApplication" …
14 You can also download JSTalk binaries: <http://www.flyingmeat.com/download/latest/JSTalkPreview.zip>
9b4b3c75 »
2009-04-24 Reformatted README in Markdown so it looks nicer on github.com
15
16
17
18 A sane object model for programmers, using Distributed Objects.
19 ---------------------------------------------------------------
20
21 Adding AppleScript support to your Cocoa application is a PITA, to put it bluntly. Sure, it's easy to do simple tasks, but once you move away from sample code territory, you are on your own and in many cases, in a world of hurt.
22
23 Why not do something more modern? Why not add scripting support the Cocoa way? Here's how to make your app scriptable via JSTalk:
24
25 1. Come up with an object model for your application, using standard Cocoa classes. In many cases this is already done, by virtue of writing a maintainable application.
26
27 2. Expose a "root" object to JSTalk, via Distributed Objects. In most cases, this will just be NSApplication. If you choose to use the JSTalk framework, it's just one line of code: [JSTalk listen];
28
29 3. Document what methods, properties, and objects you support. There's no magic xml files to fill out!
30
31 Exposing your model this way lets your app be scripted from Cocoa, Python, Ruby, and JavaScript- any language that can reach over to Cocoa. And no magic. Did I mention that part yet?
32
33
34
35 How does JSTalk (the language) work?
36 ------------------------------------
37
38 JSTalk is built on top of Apple's JavaScriptCore, the same JavaScript engine that powers Safari. So when you write in JSTalk, you are really writing JavaScript.
39
40 JSTalk also includes a bridge which lets you access Apple's Cocoa frameworks from JavaScript. This means you have a ton wonderful classes and functions you can use in addition to the standard JavaScript library.
41
42 JSTalk also adds a preprocessor to make using the Cocoa frameworks friendlier. Since Cocoa is written in Objective-C, you get a different syntax than what you'd normally encounter in JavaScript for calling methods. For example, here's some typical Cocoa code for writing a string to a file:
43
44 NSString *someContent = @"Hello World!";
45 NSString *path = @"/tmp/foo.txt";
46 [[someContent dataUsingEncoding:NSUTF8StringEncoding] writeToFile:path atomically:YES];
47
48 And here is how it would normally look in a bridged scripting language:
49
50 var someContent = NSString.stringWithString_("Hello World!")
51 var path = "/tmp/foo.txt"
52 someContent.dataUsingEncoding_(NSUTF8StringEncoding).writeToFile_atomically_(path, true)
53
54 This is a valid script in JSTalk, but it doesn't look very nice. For instance, there are lots of underscores in the method names, and you don't get the nested message passing like you do in Objective-C. To fix this quandary, JSTalk adds a light preprocessor which will allow you to use Objective-C message syntax like so:
55
56 var someContent = @"Hello World!"
57 var path = @"/tmp/foo.txt"
58 [[someContent dataUsingEncoding:NSUTF8StringEncoding] writeToFile:path atomically:true]
59
60
61
62 Give me an example
63 ------------------
64
65 Here's an AppleScript example, for adding a new rectangle object to a sketch document:
66
67 tell application "Sketch"
68 tell document 1
69 set o to make new box
70 set width of o to 100
71 set height of o to 100
72 set stroke thickness of o to 10
73 end tell
74 end tell
75
76 And here's how you do it with JSTalk, using a doctored version of Sketch(6 lines of code, + the JSTalk framework):
77
78 var sketch = [JSTalk application:"Sketch"];
79 var doc = [sketch orderedDocuments][0]
80 var rectangle = [doc makeNewBox];
81
82 [rectangle setWidth:100];
83 [rectangle setHeight:100];
84 [rectangle setXPosition:100];
85 [rectangle setYPosition:100];
86
87
88 If you aren't a fan of the optional bracket syntax, you can also write the script this way:
89
90 var sketch = JSTalk.application_("Sketch");
91 var doc = sketch.orderedDocuments()[0]
92 var rectangle = doc.makeNewBox();
93
94 rectangle.setWidth_(100);
95 rectangle.setHeight_(100);
96 rectangle.setXPosition_(100);
97 rectangle.setYPosition_(100);
98
99
100
101 But no apps out there currently support JSTalk!
102 -----------------------------------------------
103
104 Applications can also be scripted using Cocoa's Script Bridge class, SBApplication. Here's an example:
105
106 [[SBApplication application:"iChat"] setStatusMessage:"Happy (funball)"];
107
108 Although this is great to have, it's not the same as an application natively support JSTalk over DO. Anything more than simple tasks using SBApplication tends to be a little more than difficult.
109
110
111
112 JSTalk Plugins
113 --------------
114
115 Aka, loadable bundles which add functionality to JSTalk, via helper classes, wrappers, and categories.
116
117 JSTalk comes with some standard helper categories (which you can currently find in JSTalkExtras.m), but it will also look in your ~/Library/Application Support/JSTalk/Plug-ins/ folder, and load any .jstplugin bundles it sees. You can turn this off in your application if you don't like that idea, via [JSTalk setShouldLoadJSTPlugins:NO];
118
119 There are two examples with JSTalk, one that just adds a category cocoa's string class: - [NSString reversedString]. The other example is "FMDB.jstplugin", which loads the FMDB SQLite classes, for use in JSTalk. This allows you to use sqlite to create, insert, update, etc, sql tables from JSTalk.
120
121
122
123 But it doesn't do X:
124 --------------------
125
126 Let us know by sending an email to <gus@flyingmeat.com>
127
128
129
130 Mailing list and bug reporting:
131 -------------------------------
132
133 - Developer mailing list: <http://groups.google.com/group/jstalk-dev>
134 - Bug reporting: <http://jstalk.lighthouseapp.com/projects/26692-jstalk/>
135
136 A user mailing list will pop up at some point, but we're not there yet.
137
138
139
140 Here's what is currently being worked on, what's broken:
141 --------------------------------------------------------
142
143 - No PPC support. The main reason is I don't have a testable PPC box lying around, but in theory it should work.
144 - The preprocessor isn't perfect, and it screws up on some code. For instance:
145
146 [NSFullUserName() lowercaseString];
147
148 Doesn't preprocess correctly. To find out if your script is being preprocessed incorrectly, just use the Script->Preprocess menu item to see what it spits out.
149
150
151
13701812 »
2010-07-03 [DOC] Instruct folks how to cope with git submodules since we now use…
152 Checking out the code:
153 ----------------------
154 $ git clone git://github.com/ccgus/jstalk.git
155
156
9b4b3c75 »
2009-04-24 Reformatted README in Markdown so it looks nicer on github.com
157 TODO:
158 -----
159
160 - Nicer editing features.
161 - A debugger would be killer.
162
163
164
165 Credits:
166 --------
167
168 As said earlier, JSTalk is a blend of existing technologies, and has very little original code in it. Here's what it uses:
169
170 - JSTalk Icon, from Brad Ellis.
171 - JavaScriptCore, from Apple and the WebKit team.
d20aebdc »
2012-07-22 More Mocha transitional stuff.
172 - [Mocha](https://github.com/logancollins/Mocha), from Logan Collins.
9b4b3c75 »
2009-04-24 Reformatted README in Markdown so it looks nicer on github.com
173 - [TDParseKit](http://ditchnet.org/tdparsekit/), from Todd Ditchendorf.
174 - [NoodleLineNumberView](http://www.noodlesoft.com/blog/2008/10/05/displaying-line-numbers-with-nstextview/), from Paul Kim / Noodlesoft.
175 - [TextExtras](http://www.lorax.com/FreeStuff/TextExtras.html), from Mike Ferris.
Something went wrong with that request. Please try again.