Macs in Chemistry

Insanely great science

 

Scripting Script Editor

One of the beauties of Applescript is that you don't have to install anything, the standard Mac OS X install includes everything you might need to get started. If you look in the Applications folder you will see a folder called Applescript, in there is the Script Editor application. Doubler click on it to open it and you should see a window like this.

se1

If you now control-click (or right click) on the script editor window you get a menu listing all the script editor scripts that are avaiable. Many of these are part of the standard install whilst others might be installed by specific applications. They are all in a folder in Macintosh HD/Library/Scripts/Script Editor Scripts and you can add scripts or folders here and they will appear in the menu. Whilst the standard install includes a variety of useful and frequently used routines if you are writing scripts that interface with UNIX applications then having access to the POSIX paths to some of the standard folders is a regular need.

setemp

The following script is simply a modification of one of the scripts provided by Apple. First we get the path to the desktop and then convert it to a POSIX path. The text can then be inserted into the script or used to replace the highlighted text in the script. Save the script as Posix_path_to_desktop and put it in the Macintosh HD/Library/Scripts/Script Editor Scripts folder (I actually created a folder called Paths and put it in there).

set the target_string to "X-X-X"
set the script_text to "set path_to_desktop to path to desktop
set posix_path to POSIX path of path_to_desktop as text" & return

tell current application
  activate

    tell the front document
      set the selected_text to contents of selection

        if the selected_text is not "" then
          if my display_message() is false then return "user cancelled"
      end if

        set the contents of selection to the script_text

        try
          check syntax
      end try

        my replace_and_select(target_string, "")

        try
          check syntax
      end try
  end tell
end tell

on display_message()
  display dialog "This script will delete the selected text." & return & return & "Do you want to continue?" buttons {"Help", "Continue", "Stop"} default button 3 with icon 2
    set the user_choice to the button returned of the result
    if the user_choice is "Help" then
      my script_help("ScriptEditor001")
        return false
  else if the user_choice is "Stop" then
      return false
  else
      return true
  end if
end display_message

on script_help(this_anchor)
  ignoring application responses
      tell application "HelpViewer"
          activate
            try
              lookup anchor this_anchor in book "Script Editor Help"
          end try
      end tell
  end ignoring
end script_help

on replace_and_select(target_string, replacement_string)
  tell current application
      tell the front document
          set this_text to the contents
            set this_offset to the offset of the target_string in this_text
            if this_offset is not 0 then
              set selection to characters this_offset thru (this_offset + (length of the target_string) - 1)
                set the contents of the selection to the replacement_string
          end if
      end tell
  end tell
end replace_and_select

The script will now be available from contextual menu within script editor. You can of course use a variety of other folders for example:
set path_to_public to path to home folder
set path_to_public to path to public folder
set path_to_public to path to documents folder