Skip to content

Creating Scripts ​

Create AppleScripts to automate Bike and connect it with other apps. To run scripts someone else wrote, see Using Scripts. For bigger features, consider Bike's more powerful extension system.

Overview ​

When scripting Bike you work with documents, windows, and rows. Documents and windows are standard scripting objects with a few Bike additions. Rows are Bike's own.

A row's name is its text. Rows also have an id, attributes, and child rows. Moving or deleting a row moves or deletes its children too.

You get rows in three ways:

  1. From document properties such as root row, selection row, and focused row.
  2. From a document's rows: every row except the root. Good for finding rows.
  3. From a row's rows: just its children. Good for making new rows, or as a place to move rows to.

Dictionary ​

Bike's scripting dictionary lists everything you can script.

To open Bike's scripting dictionary ​

  • Drag Bike onto Script Editor's icon.
  • Or in Script Editor, choose File > Open Dictionary… and pick Bike.

Learning AppleScript ​

Apple's AppleScript Language Guide is the official starting point. For lesser people, such as myself, the starting point is to find example scripts and change them until they do what you want. Here are some.

Example Scripts ​

Script Debugger helps a lot with these. It steps through a script line by line, so you can see the effect of each command on your document.

Kitchen Sink ​

A nonsense script that shows many of Bike's scripting abilities, such as making and moving rows.

applescript
tell application "Bike"
  
  -- Make a new demo document, so your open documents aren't touched.
  set demo to make document with properties {name:"Demo"}
  
  tell demo
    -- New documents start with welcome text. Delete it.
    delete every row
    
    -- Create a new "Hello World" row
    set helloWorld to make row with properties {name:"Hello World"}
    
    -- Add some child rows to "Hello World"
    tell helloWorld
      make row with properties {name:"one"}
      make row with properties {name:"two"}
      make row with properties {name:"rich text"}
    end tell
    
    tell row named "rich text"
      set bold of first word of text content to true
      set italic of second word of text content to true
    end tell
    
    -- Insert a new child into the middle
    tell helloWorld
      make row at (after row named "one") with properties {name:"middle child"}
      
      -- Making a row "at" another row adds it as a child.
      make row at row 2 with properties {name:"middle child child"}
    end tell
    
    -- You can also just make an empty row
    make row
    
    -- Or make a row with a persistent id, so it's easy to find later, even after its name changes.
    -- A row's `id` is a read-only number that isn't stable across saves, so use the writable `persistent id` for this.
    make row with properties {persistent id:"boom", name:"My id is boom"}
    
    -- Change the name of an existing row. Look it up by persistent id using the `get row` command.
    set name of (get row persistent id "boom") to "You've been renamed"
    
    -- Check to see if a row exists
    set boomExists to (get row persistent id "boom") is not missing value -- true
    
    tell (get row persistent id "boom")
      -- Read/Write row level attributes
      exists (attribute named "test") -- false
      make attribute with properties {name:"test", value:"value"}
      value of attribute named "test" -- value
      set value of attribute named "test" to "new value"
      value of attribute named "test" -- new value
      delete attribute named "test"
    end tell
    
    -- When "Hello World" moves it brings all containing rows with it.
    move row named "Hello World" to (get row persistent id "boom")
    move row named "one" to before row named "two"
    
    collapse row named "Hello World" with all
    expand row named "Hello World"
    select at (get row persistent id "boom")
    
    -- Now just show "Hello World" and contained rows
    set focused row to row named "Hello World"
  end tell
  
  -- Make another document
  tell (make document with properties {name:"Move to"})
    -- delete welcome text again
    delete every row
    
    -- Move row from our original document to this new document
    move row named "middle child" of demo to first row
    
    -- Can also just copy rows to new document
    duplicate row named "one" of demo to end of rows
  end tell
  
end tell

Query Script ​

Find rows with an outline path.

applescript
tell front document of application "Bike"
  set headings to query outline path "//heading"
  set names to {}
  repeat with each in headings
    set end of names to name of each
  end repeat
  names
end tell

Home Script ​

Resets your view to "Home".

applescript
tell front document of application "Bike"
  set focused row to root row
  if exists first row then
    select at first row
  end if
end tell

Cleanup Script ​

Collapses all rows, then restores your selection, expanding only what's needed to show it. Use it when too many rows are expanded but you want to keep working where you are.

applescript
tell front document of application "Bike"
  set saved to selection row
  collapse root row with all
  select at saved
end tell

Timestamp Script ​

Adds a row with the current time under today. It finds or makes the same year, month, and day rows that Bike's Calendar uses, by persistent id, so it works wherever those rows are in your outline.

applescript
set yearName to do shell script "date +'%Y'"
set yearId to do shell script "date +'%Y'" & "/00/00"
set monthName to do shell script "date +'%B, %Y'"
set monthId to do shell script "date +'%Y/%m'" & "/00"
set dayName to do shell script "date +'%B %d, %Y'"
set dayId to do shell script "date +'%Y/%m/%d'"
set timeName to do shell script "date +'%l:%M %p' | sed 's/^ //'"

tell application "Bike"
  tell front document
    set y to my getOrMake(yearId, yearName, root row)
    set m to my getOrMake(monthId, monthName, y)
    set d to my getOrMake(dayId, dayName, m)
    select at make row at front of rows of d with properties {name:timeName}
  end tell
end tell

to getOrMake(getId, getName, rowContainer)
  using terms from application "Bike"
    tell container document of rowContainer
      set existing to get row persistent id getId
      if existing is not missing value then
        return existing
      else
        tell rowContainer
          return make row at front with properties {persistent id:getId, name:getName}
        end tell
      end if
    end tell
  end using terms from
end getOrMake

Run App Context Script ​

The evaluate command calls from AppleScript into Bike's app extension API, which is faster and more powerful than AppleScript. Jump into extension land and back again.

Bike evaluates script in its app extension context. If the result is a function, Bike calls it with input. Either way, you get the result back as a string.

TIP

Pass plain JavaScript, not the TypeScript used in most of the extension API documentation.

Script Editor accepts either AppleScript or JavaScript (JXA):

applescript
tell application "Bike"
	evaluate script "bike.version"
end tell

tell application "Bike"
	evaluate input "hello" script "(input) => { return input + \" world\" }"
end tell
javascript
Application("Bike").evaluate({ script: "bike.version" })

Application("Bike").evaluate({ input: "hello", script: "(input) => { return input + \" world\" }" })

See also ​