OSX: Using System Events to control Menu Items via AppleScript

The following are some trivial snippits to use AppleScript to control Menu Items in applications that support System Events.

—Tell Safari to open a new window
tell application “Safari” to activate
tell application “System Events” tell process “Safari” click menu item “New Window” of menu “File” of menu bar 1 end tell
end tell
—Tell Finder to sort by size
tell application “Finder” activate
end tell
tell application “System Events” tell process “Finder” tell menu bar 1 tell menu bar item “View” tell menu “View” tell menu item “Arrange” tell menu “Arrange” click menu item “by Size” end tell end tell end tell end tell end tell end tell
end tell

Another little useful snippit:

—convert the file reference to UNIX style
set this_path to the POSIX path of this_item