title: In applescript, is there any way to specify which desktop a file is opened in?
body: <p>When my system boots I would like to run applescript that opens files in three different desktops/spaces. </p>
  
  <pre><code>First Space: Mail and Things (my to do list program)
  Second Space: Textmate and a Safari for my first project
  Third Space: Textmate and a Safari for my second project
  </code></pre>
  
  <p>First, in Mission Control I created two more desktops which will remain there the next time my system boots unless they are manually removed. Instead of creating one long script, I chained three applescripts (boot1, boot2 and boot3) to break it up into simpler blocks of code. At the end of boot1 you will see:</p>
  
  <pre><code>run script file "&lt;drive name&gt;:Users:&lt;username&gt;:boot2.scpt"
  </code></pre>
  
  <p>In boot2 and boot3 you will see a bunch of <code>delay</code> lines. One thing I dislike about applescript is that it often starts processing the next command before the OS finishes responding to the prior one. This causes inconsistencies and errors. Delays are a hack to force things to slow down. They help, but even when you use them things are still a bit dicey. In boot2.script:</p>
  
  <pre><code># this emulates the keyboard shortcut to move to desktop 2
  # there doesn't seem to be any way to modify an `open` command to open a file on desktop 2
  tell application "System Events"
      delay 2
      # key code 19 is the key code for the number 2. 
      # &lt;cntl&gt; 2 is the shortcut to get to desktop 2
      key code 19 using control down
  end tell
  
  tell application "TextMate"
      activate
      # 'sites' is the name of the directory my projects are in
      open "/users/&lt;username&gt;/sites/project1/"
  end tell
  
  tell application "Terminal"
      activate
      do script "cd /users/&lt;username&gt;/sites/project1/"
  
      delay 2
      do script "rails s" in front window
  
      delay 2
      tell application "System Events" to tell process "Terminal" to keystroke "t" using command down
      tell application "System Events" to tell process "Terminal" to keystroke return
  
      delay 2
      do shell script "open -a Safari http://localhost:3000"
  end tell
  </code></pre>
  
  <p>OK... so this mostly works to get desktop 2 in place except for inconsistencies when the delays aren't long enough. Boot3.script is almost the same as boot2 but when trying to open an application on desktop 3, because there is a window on desktop 2 the system jumps back to that desktop. This is the next problem. How do I overcome that?</p>
  
  <p><a href="https://stackoverflow.com/questions/2305491/applescript-opening-an-app-in-space-number-n">2305491</a> is no longer relevant because space preferences are gone.</p>
  
  <p>Thanks.</p>