Features Videos Help About Updates
Menu Buy Now

Controlling CodeKit With AppleScript

First Steps:
Getting Started
Live-Reload Browsers
Browser Sync
Set Language Options
Set Output Paths & Action
Second Steps:
Defaults For New Projects
Build Your Project
Set Target Browsers
Stuff To Know:
CodeKit + Git
Troubleshooting
License Recovery
PostCSS Tools:
Autoprefixer
PurgeCSS
Custom PostCSS Plugins
Other Tools:
npm
Babel — (JS Transpiler)
Terser — (JS Minifier)
Rollup — (JS Bundler)
Cache-Buster
HTML-Minifier
LightningCSS
Bless
Languages:
Sass
Less
Stylus
JavaScript
CoffeeScript
TypeScript
Pug
Haml
Slim
Kit
Markdown
JSON
Image Optimizers:
WebP
PNG
JPEG
SVG
GIF
Frameworks:
CodeKit Frameworks
Tailwind
Bootstrap
Bourbon
Bitters
Zurb Foundation
Susy
Nib
Jeet
Syntax Checkers:
ESLint
Advanced:
Hooks
Environment Variables
Adding Custom Languages
Team Workflows
Scripting CodeKit
Editor Plugins:
Nova
Atom
Sublime Text
Coda 2
More
Read-Only Mode
Upgrading From 2.0
FAQ

CodeKit can be manipulated with AppleScript. To see available commands, launch the AppleScript Editor app, then choose File > Open Dictionary. In the list that pops up, select CodeKit.

Examples

The following snippets show how to use the AppleScript API. See the CodeKit Dictionary in AppleScript Editor for each method's return value and other detailed information.

Add a Project Or Framework
tell application "CodeKit" to add project at path "~/clients/someProjectFolder"
tell application "CodeKit" to add framework at path "~/someFrameworkFolder"

If the specified folder is already in CodeKit, these methods do nothing. Otherwise, the folder is added and becomes the active project. If the item at the specified path is not a folder, these methods abort and log an error message to Console.app. Unless an error occurs, the CodeKit window will not come to the front. Inspect the return value of each method to determine what happened.


Select a Project Or Framework
tell application "CodeKit" to select project containing path "~/clients/aProject/app.scss"
tell application "CodeKit" to select framework containing path "~/someFrameworkFolder"

The path can be the root project folder or any item that is a descendent thereof. The descendent item need not be in CodeKit or even exist on disk. This makes the API really flexible because you don't need to figure out the path to the project folder if you know a certain file is part of that project and you have that file's path handy.

Note: If you call this right after adding a new project or framework, you must wait for CodeKit to finish adding the new project. Otherwise, this method will tell you that no matching project was found.


Refresh Projects Or Frameworks
tell application "CodeKit" to refresh project containing path "~/clients/aProject"
tell application "CodeKit" to refresh framework containing path "~/clients/aFramework"
tell application "CodeKit" to refresh all projects and frameworks

You can refresh a single project/framework using the top command with a path to the root project folder or any item that is a descendant thereof. The descendant item need not be in CodeKit or even exist on disk.

Use the bottom command to refresh everything currently in CodeKit.

Note: the refresh will not take place if CodeKit is busy when it receives your request. Inspect the return code to determine if a refresh actually fired.

Toggle File-Watching
tell application "CodeKit" to pause file watching
tell application "CodeKit" to unpause file watching

If file-watching is already paused when you call the 'pause' command, nothing happens. This is the same for the 'unpause' command.


Refresh Browsers
tell application "CodeKit" to refresh browsers

The command above reloads the full page of every browser that's viewing CodeKit's preview URL. To reload only the stylesheets and inject changes, use this:

tell application "CodeKit" to refresh browsers by reloading just stylesheets

You can tell CodeKit to wait up to 10 seconds before dispatching the refresh. Here's an example using multi-line AppleScript syntax:

tell application "CodeKit"
  refresh browsers by reloading the whole page after delay 3
end tell

Preview In Browser
tell application "CodeKit" to preview in browser

The above command opens CodeKit's preview server in the system-default browser. You can also specify popular browsers by name (for the full list, see the dictionary for CodeKit in AppleScript Editor):

tell application "CodeKit" to preview in browser Firefox

If the browser you want is not in the named list, use its bundle identifier:

tell application "CodeKit" to preview in browser bundle named "com.apple.Safari"

Finally, you can tell CodeKit which preview address to open. There are three:

tell application "CodeKit"
  preview in browser Firefox using bonjour address
end tell

The other two options are using LAN address and using local feedback address. For details, see Browser Refreshing.


Get Preview Server Status
tell application "CodeKit" to get preview server status 

This command returns '1' if the Preview Server is running normally, '0' if it's not running, '2' or '3' if the Server is starting up or updating, and '-1' if an error occurs.


Get Preview Server Address
tell application "CodeKit" to get preview server url 

This command returns the current address of the Preview Server as a string. You can add "using" and one of "bonjour address", "LAN address", or "local feedback address" to get the type of address you want. If you omit that optional parameter, you'll get whichever address you've picked as the default in CodeKit. Returns an empty string if an error occurs or the Preview Server isn't running.


Add An Entry To The Log
tell application "CodeKit"
  log message "Hello World!" with title "My Log Entry"
end tell

The message string and title are required. By default, messages have a success type (a green dot in the log). You can optionally specify a warning (yellow dot) or problem type (red dot). You may also provide an optional Project Name associated with this message:

tell application "CodeKit"
  log message "Hi!" with title "My Entry" and type warning for project named "myProject"
end tell

Process A File
tell application "CodeKit"
  process file at path "~/path/to/file.scss"
end tell

The file must be part of a Project currently in CodeKit and must not be in a Skipped Folder. Note: CodeKit will process the file even if its Output Action is set to "ignore", because you're explicitly telling the app to do so.


Build A Project
tell application "CodeKit"
  build project containing path "~/some/path/to/a/file.js"
end tell

You can build a Project the same way you select one—by providing the path to the root project folder or to any item that is a descendant thereof. You do not need to know the Project's root folder; CodeKit will figure that out for you.


Request An API

To request a new command, please open an issue on this GitHub page. If you build a workflow or plugin using these methods, I'd like to hear about it! Email me at [email protected]

Made in San Diego, CA

Follow the Developer on Twitter

Email Support

Report a Bug

Privacy Policy