AppleScript tip: script a program that isn't scriptable

If you ever want/need to use AppleScript to drive another program that doesn’t have AppleScript support, there are a few things you can do. Here’s a snippet of code where I open new tabs in Safari, and typ text into its location field:

tell application "Safari"
  activate
  -- create all the tabs that are needed
  tell application "System Events"
    -- enter the url in the open window
    keystroke (item 1 of urlList)
    key code 36
    repeat with i from 2 to (numURLs)
      -- for each additional url, first create a tab
      tell process "Safari"
        click menu item "New Tab" of menu "File" of menu bar 1
      end tell
      -- now enter the url
      keystroke (item i of urlList)
      key code 36
    end repeat
  end tell
end tell

If Safari had a little more support for tabs, and opening URLs in tabs, I wouldn't have to have code like keystroke, key code 36, or click menu item "New Tab" of menu "File" of menu bar 1.

This shows the typical way of getting around an application that isn't scriptable: you use the "System Events" to talk to the application in question, and select menu items, click buttons, and in this case, enter text.

Add new comment

Anonymous format

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