Freelance iOS Developer, Rapidly Aging Punk

Coding for the Spark Core in BBEdit

Feb 05, 2015 at 04:29PM

Call me old-fashioned if you want, but when I write code I want to do it in a Cocoa window, like a dignified human being. So I was pretty disappointed to find out that the primary IDE for the Spark Core is a text box on a web site:

Spark Web IDE

Obviously that's not gonna do. I dug a little deeper and it turns out that there is a Spark desktop app called Spark Dev – but it's a fork of Atom, Github's web-based text editor. No thanks.

There's a light at the end of this JavaScript tunnel: Spark has an excellent command line app – Spark CLI. It can handle everything the web IDE does including compiling code and flashing your Spark over wifi. With that component in place, it's just a matter of writing some Applescript to get BBEdit pounding out Spark code. Here's my (mostly ripped off from the web) Compile for Spark script:

set theFile to missing value

tell application "BBEdit"
    set activeWindow to front window

    if (class of activeWindow is project window) then
        set projectDocument to project document of activeWindow

        if ((count of items of projectDocument) > 0) then
            set firstFileItem to item 1 of projectDocument as alias
        else
            set firstFileItem to file of document of activeWindow as alias
        end if

        if (on disk of projectDocument) then
            set theProjectFile to file of projectDocument as alias

            tell application "Finder"
                set theProjectDir to container of theProjectFile
                set firstFileDir to container of firstFileItem
            end tell

            if (firstFileDir is equal to theProjectDir) then
                -- Use project file
                set theFile to theProjectDir as alias
            else
                -- External project file -> use first item to set context
                set theFile to firstFileItem
            end if
        else
            -- BBEdit doesn't provide direct access to the Instaproject root
            -- Use the first node from the project list
            set theFile to firstFileItem
        end if
    end if
end tell


tell application "Terminal"
    if (the first window whose busy is false) exists then
        set win to the first window whose busy is false
        set frontmost of win to true
        do script "spark compile " & the quoted form of the POSIX path of theFile
    else
        do script "spark compile " & the quoted form of the POSIX path of theFile
    end if
    activate
end tell

And here's my Flash to Spark script:

set theFile to missing value

tell application "BBEdit"
    set activeWindow to front window

    if (class of activeWindow is project window) then
        set projectDocument to project document of activeWindow

        if ((count of items of projectDocument) > 0) then
            set firstFileItem to item 1 of projectDocument as alias
        else
            set firstFileItem to file of document of activeWindow as alias
        end if

        if (on disk of projectDocument) then
            set theProjectFile to file of projectDocument as alias

            tell application "Finder"
                set theProjectDir to container of theProjectFile
                set firstFileDir to container of firstFileItem
            end tell

            if (firstFileDir is equal to theProjectDir) then
                -- Use project file
                set theFile to theProjectDir as alias
            else
                -- External project file -> use first item to set context
                set theFile to firstFileItem
            end if
        else
            -- BBEdit doesn't provide direct access to the Instaproject root
            -- Use the first node from the project list
            set theFile to firstFileItem
        end if
    end if
end tell


tell application "Terminal"
    if (the first window whose busy is false) exists then
        set win to the first window whose busy is false
        set frontmost of win to true
        do script "spark flash THE_NAME_OF_YOUR_CORE " & the quoted form of the POSIX path of theFile
    else
        do script "spark flash THE_NAME_OF_YOUR_CORE " & the quoted form of the POSIX path of theFile
    end if
    activate
end tell