3

When my system boots I would like to run applescript that opens files in three different desktops/spaces.

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

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:

run script file "<drive name>:Users:<username>:boot2.scpt"

In boot2 and boot3 you will see a bunch of delay 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:

# 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. 
    # <cntl> 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/<username>/sites/project1/"
end tell

tell application "Terminal"
    activate
    do script "cd /users/<username>/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

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?

2305491 is no longer relevant because space preferences are gone.

Thanks.

Community
  • 1
  • 1
Jay
  • 6,206
  • 11
  • 48
  • 82
  • 1
    I think there's no why to do this with Applescript. You could use the autmator recording function. – idmean Nov 04 '13 at 18:35

1 Answers1

1

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.

There is an option in the Mission Control Preferences called "When switching to an application, switch to a Space with open windows for the application". Uncheck this.

OK... so this mostly works to get desktop 2 in place except for inconsistencies when the delays aren't long enough.

Better solution is always something like this

repeat until something exists
delay 0.1
end repeat
user309603
  • 1,518
  • 11
  • 10