Who You Gonna Protocol

While building a map-to-rooms generator for my next game I wanted to be able to open some Tiled JSON files from the browser. Just linking directly to the file with the file: protocol would open the JSON in the browser. And (as far as I know) Tiled doesn’t provide its own custom protocol for opening its files. (I’m not prepared to declare all JSON files on my system Tiled files so setting Tiled to the default application for JSON files so I can double-click them in the Finder isn’t an option.)

Here’s how I defined my own custom protocol to open files in Tiled from the browser using a simple AppleScript app. (I know right!)

Open up AppleScript Editor and paste the following into the editor window:

on get_config()
    return {"Tiled", "tiled"} -- app name, protocol
end get_config

on open location url_
    set config_ to get_config()
    set app_ to item 1 of config_
    set protocol_ to item 2 of config_

    set start_ to (get length of protocol_) + 2 -- : then next character
    set end_ to length of url_
    set file_ to (get characters start_ thru end_ of url_ as string)

    try
        ignoring application responses -- prevents hangs
            tell application app_
                activate
                open file_
            end tell
        end ignoring
    on error errStr
        display alert "Failed to open " & file_ & ": " & errStr
    end try
end open location

Then save it as an Application (it doesn’t matter where since you never need to open it manually). Back in the editor window click the Bundle Contents icon and then select “Reveal in Finder” from the gear contextual menu.

You should see your new app’s Contents folder. Open up Info.plist in a plain text editor. Add the following:

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLName</key>
        <string>Tiled</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>tiled</string>
        </array>
    </dict>
</array>

To the end of the file, right before the final:

</dict>
</plist>

Save Info.plist. That’s it. Now clicking a link in the browser with the tiled: protocol will open the desired file in Tiled.

eg. <a href="tiled:/path/from/root/to/room.json">room</a>

Changing the app and protocol are easy. Just change the app name and protocol strings in both the get_config() function in the AppleScript and in the <string> nodes in Info.plist.

Previous
Everlong
Next
And Game Boy Makes Three
Author
Shaun Inman
Posted
September 30th, 2014 at 12:03 pm
Categories
Apple