AppleScript subroutines: how to create a subroutine

AppleScript subroutine FAQ: How do I create an AppleScript subroutine (or AppleScript function)?

Creating a subroutine in AppleScript is pretty easy. Jumping right in, here's an example of an AppleScript subroutine that adds one to whatever you pass into it:

-- a super-simple subroutine
on addOne(a)
   return a + 1
end addOne

-- test the subroutine
set answer to addOne(50)
display dialog answer

I create lots of little AppleScript subroutines to modularize my code, and I encourage you to do the same. Every subroutine should do one (and only one) thing, and do it well.

Add new comment

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.