
Building OS X Apps with JavaScript - snyp
http://tylergaw.com/articles/building-osx-apps-with-js
======
petercooper
_That pipe is a C bitwise OR operator. I’m not gonna pretend to know what that
means._

I'm honestly not being sarcastic, but I think there's something interesting
about reading an enjoyable programming article by someone who doesn't know
what bitwise OR is. It sorta feels like a huge reminder we've moved on to a
whole new generation/level of abstraction where competent developers no longer
need to understand binary.

I gave a talk the other day about the future of code (think 2024 and on) and
one of the biggest things out of my research was that we'll eventually not
need to think about intermediate data structures in code anymore as AI
profilers, VMs, compilers, etc. will be able to optimize and profile almost
everything away with developers working more with specifications and
constraints. I wonder if in 10 years' time a programmer not knowing what a
linked list or a binary tree is will not be considered unusual either.

~~~
protonfish
I've been a programmer for 15 years (web dev, self-taught) and I don't know
what those are. I think a linked list is like a JSON object, right?

Yes, I am a real developer but the technologies I use don't have those
paradigms. I doubt it this is good or bad, just different.

~~~
deckiedan
Imagine an object:

    
    
        { data: stuff,
          next: more_stuff }
    

`next` is a reference to the next object like this one in the list, and `data`
is a reference to the actual object at this point in the list.

You've now got a close-enough idea about what a linked-list is.

You can also have dual-linked lists, where each object also has a `previous`
pointer to the previous object in the list.

If you want, you could store the data directly in this object, rather than
having a pointer out to the actual data (of course).

This kind of thing can be very useful for certain kinds of data manipulation,
where the built-in lists aren't efficient enough - for whatever reason. Say
the language you're using creates an entirely new list whenever you try to
remove an element from the middle of a list - or when you need to be moving /
removing elements all over the place.

The big downside of such a linked list is that you can't easily jump to a
specific item number in the list. So jumping to item 200 means walking through
the first 199 elements in the list to find it.

I've only had to implement a linked list once in a JS project, I don't
remember the reason why. I think the algorithm just worked a lot more
efficiently that way.

One reason a linked list could be useful in a javascript (or PHP, or whatever)
language would be, for instance, if you had a game where when a player hit a
block with a hammer, that block would bounce up and down, as would the two
blocks on either side. If you store all the blocks in a regular list, then you
need to know the index of the block in order to find the blocks on either
side. In a linked list, you don't.

One of the original big advantages of linked lists is that you can have
objects of variable size, and the list can grow and shrink without adjusting
any of the previous elements. Opposed to this is an `array` (sometimes called
a vector) which is a fixed size chunk of memory which is split up into
identical sized parts. You can then jump to element 200 by looking at the
starting position + 200 * the size of a single element.

This works pretty well, but if you want to add another 20 elements into the
middle of the array, you need to allocate a new chunk of memory and move the
whole lot into it, and deallocate the previous entire array. This is one of
the reasons why strings are usually immutable in many languages. Strings are
stored as arrays, not linked lists of letters.

I think it's worth learning about all this "Computer Science" stuff, even if
you don't need it very often, as it means when you come across a new problem
you have a much bigger mental toolkit from which to figure it out.

~~~
tel
A better reason why linked lists are better than arrays is that they have
obvious, fast immutable APIs. Immutable data is not always a win, but
sometimes it is.

------
warcher
You know, I understand the urge, once one has really gotten deep and mastered
their first language, to try to apply it everywhere. But I really must
strongly encourage the kids on the come-up to continue mastering new tools.
It's not that javascript is a bad language (although that helps), so much as
the industry doesn't allow any of us to rest on our laurels. This year's rock
star is tomorrow's dinosaur.

~~~
marknutter
Great. Now master Java, C#, and Objective-C to build the same app on multiple
platforms. JavaScript is the only language that let's you target every
platform.

~~~
zoomerang
> JavaScript is the only language that let's you target every platform

Ironically, the three languages you listed - Java, C#, and Objective-C are all
available on all major platforms. I could write code in any of those languages
and have it run on

    
    
      - Windows
      - MacOS
      - Linux
      - Android
      - iOS
    

Furthermore, the apps would all be _native_. That closest Javascript can come
is to use the uncanny-valley of HTML5 without native widgets or performance.

Frankly, Given that the burden of learning a new language is tiny, I see no
reason why we should try and cram Javascript into every nook and cranny. It's
a fairly mediocre language at best.

tl;dr The three languages you listed are all capable of _better_ cross
platform support than Javascript.

~~~
taf2
Really? Seems like HTML/JS is the best choice for cross platform application
development. These days with Chrome, Firefox and even Safari you can create
really full featured applications. The new HTML5 API's are incredibly full
featured. Just the other day I created an App for my daughter to helper her
login to her school computers - so I decided to create a login screen and then
I realized with HTML5 audio API, I can also read aloud each key press as well
as an intro text so even though she can't read yet she can use the App. It's
100% JS and HTML/CSS for UI.

At my job we spend thousands of hours building our native App for Android and
IOS for work and it's painful - when I can open the browser and nearly all the
capabilities are in browser and the browser already cleanly abstracts the
process of editing UI via HTML/CSS. I've yet to find another UI framework as
powerful or quick to develop in as HTML/CSS - and the performance is looking
really good in the browser. Native app performance is a myth unless maybe
you're writing a video game?

~~~
vertex-four
> I've yet to find another UI framework as powerful or quick to develop in as
> HTML/CSS - and the performance is looking really good in the browser.

Really? Until a couple of months ago with the advent of flexbox, you couldn't
reliably do _columns_ that fit dynamically to the browser window (frameworks
like Bootstrap simply define three different window sizes and switch between
them, rather than allowing for fluid resizing) as well as you can in every
other UI framework I've used, never mind vertical layout. (See all the hacks
for "sticky footers" over the past decade.)

~~~
woah
It may not be powerful, but it is quick to develop in.

~~~
pjmlp
If all you know is web related stuff.

------
lemming
The really cool thing about this is that you can also use alternative
languages which compile to Javascript. I develop Cursive, an IDE for Clojure
and Clojurescript, and one of my users has a native app written in
Clojurescript published on the App Store. He had to jump through a few hoops
to get the REPL working but once he got it going he could edit code on his
device live. It's really cool to see. I'm hoping to add debugging support
using the Webkit Remote Debugging Protocol, which will make it even nicer.

~~~
lovamova
"…one of my users" Do you have users? What part of your body do they use?

~~~
estefan
Do you need a certain number of points to downvote pedantic dickheads like
this?

~~~
collyw
500

------
nnx
Will Apple allow apps written with this to be distribued on the Mac App Store?

I don't see why they wouldn't, but I couldn't find a confirmation either way.

~~~
thegaw
I haven't seen anything official, but I think you can. Mavericks introduced
Code Signing for applets
[http://macosxautomation.com/mavericks/codesign/02.html](http://macosxautomation.com/mavericks/codesign/02.html)
so you can at the very least distribute JS Apps. The first time someone tries
to push one up to the app store for review will be interesting.

------
rattray
What's great about this isn't just that you can do it with JavaScript, which
is much easier than ObjC, and probably also easier than Swift.

It's that it's also essentially natively supported. Having futzed with PyQt
and PyObjC to build a medium-sized desktop application, I never want to go
back to dealing with bridges to low-level libraries again. But this looks much
more manageable.

Reading this code makes me think I just might write an OSX app again some day.
And if CoffeeScript can compile down to it - no idea why it couldn't - even
better.

~~~
city41
Why is this easier than ObjC or Swift? I'm a huge JS fan, and I'd still choose
those two over this. The API is still very much Cocoa, and now in JS form it's
even more tedious. Not sure how you'd truly debug an app written this way, and
not to mention almost all docs, examples, blog posts, etc that you ever find
will be based on ObjC and Swift.

~~~
rattray
"Easy" and "hard" are always relative terms. If someone doesn't already know
ObjC or Swift, they may be considered harder for that person. Not necessarily
harder in absolute terms. But I'll take that as a serious recommendation!

~~~
city41
ObjC is a very small, simple language. The hard part is Cocoa. The JavaScript
bindings don't address the hard part at all.

~~~
AlexeyBrin
> ObjC is a very small, simple language

Objective-C is a small language only if you already know C. Otherwise you will
have to learn C first.

~~~
DonHopkins
But C combines the power of assembly language with the ease of use of assembly
language!

Or to quote Bill Joy, "C is really just peek and poke with some syntactic
sugar".

------
FreakyT
What I wonder is: can this style of development be used with Interface
Builder?

~~~
sjtrny
Why would you want that? You can programmatically defibe your interface with
Swift/objective-C. JavaScript feels like a step backwards to me.

~~~
Alphasite_
Because defining constraints in code is more of a faff than just doing it
visually? Its also more immediate since (as far as I know) Xcode doesn't
support anyform for edit and continue outside of Playgrounds.

------
benihana
Learning Objective-C was never the limiting factor for me writing native OS X
code. The hard part about it was always figuring out the APIs. It's one of
those things where if you have someone to teach you it's great, but learning
on your own is time consuming and tedious and frustrating.

As much as I like using JS for scripting purposes in the OS, I feel like with
this, I have the same problem: namely that the cocoa APIs are massive and I
have trouble understanding what's going on, even after trying to learn them
three or four times in the past decade.

------
nowayjorge
Nice!

