If a TeX project contains a bibliography, a sequence of typesetting commands must be run to update the bibliography. Latex is run first to create an .aux file. Bibtex is run to create .bbl and .blg files from this .aux file. Latex is run again to add the bibliography to the document. Latex is run a final time to update the references to the bibliography in the document.

Your projects may contain similar sequences of typesetting commands. It is possible to use applescript to automate these sequences.

To see how to do that we'll examine the OtherScripts->Bibliography command which comes with TeXShop. Here is the body of that command

  --Applescript

  set fileName to #FILEPATH#
  if fileName is equal to ""
  activate
  display dialog "Please save the file first" buttons {"OK"} default button "OK"
  return
  end if

  set frontName to #DOCUMENTNAME#
  tell application "TeXShop"
  save document frontName
  end tell

  tell document frontName of application "TeXShop"

    latexinteractive

    repeat
      delay 2
      if taskdone
      exit repeat
      end if
    end repeat

    bibtex

    repeat
      delay 2
      if taskdone
      exit repeat
      end if
    end repeat

    latex

    repeat
      delay 2
      if taskdone
      exit repeat
      end if
    end repeat

    latex

  end tell

The first line of this command indicates that this is an AppleScript macro. The next lines check #FILEPATH#, a parameter which gives the full path to the tex source. This parameter is an empty string when a new document has been created and not yet saved. In that case the user is asked to save the document and the script ends.

The next line tells TeXShop to save the document. Notice that we use #DOCUMENTNAME# to refer to the document in question.

The remaining commands run latexinteractive, bibtex, latex, and latex. Recall that control returns to applescript immediately after calling a typesetting command before the typesetting job is over. The repeat loop tells the script to check whether the job is complete before running another typesetting task. The line "delay 2" causes applescript to pause rather than continually asking if the task is done and thereby slowing the entire computer down.

Macros Help
Writing Scripts with TeXShop Typesetting Commands