Re-wrote using applescript