8

I wonder if it is possible in applescript to create a script for which we give as input the application name and a number N, so this app gets opened in the Space's space number N.

I would like with this to create a meta-applescript, so when the computer boots and after login, on each space I get different apps, and important, I can change this in the script file, and not through mac os x Space's preferences

Thanks

Open the way
  • 26,225
  • 51
  • 142
  • 196

3 Answers3

7

In OS X 10.5 or 10.6, Spaces assignments can be accessed and changed via the scriptable interface to System Events.app:

tell application "System Events"
    set x to application bindings of spaces preferences of expose preferences
    set x to {|com.apple.textedit|:4} & x  -- Have TextEdit appear in space 4
    set application bindings of spaces preferences of expose preferences to x
end tell

If you don't already know it, you can get the bundle id of an application from the Finder:

tell application "Finder"
    get id of application file "TextEdit" of folder "Applications" of startup disk
end tell
Ned Deily
  • 83,389
  • 16
  • 128
  • 151
0

This works to switch to Space 2 and then back to Space 1:

tell application "System Events"
    key code 19 using {control down} -- control+2 is switch to Display Space 2
end tell
delay 1.0

tell application "System Events"
    key code 18 using {control down} -- control+1 is switch to Display Space 1
end tell
delay 1.0
Haytem BrB
  • 1,528
  • 3
  • 16
  • 23
0

Answer

Although it's useful to assign applications to workspaces this doesn't address the question properly. Because, for example, you may want to launch multiple chrome windows in different spaces hence the application-to-space binding wouldn't work.

I found a workaround made of two steps to make this happen.

  1. Change space location
tell application "System Events"
    # comment: 18 to 21 somehow refer to workspaces 1 to 4, therefore here we are going to space number 1
    tell application "System Events" to key code 18 using {control down}
end tell
delay 1 # comment: add some delay before launching app. this is 1 second delay
  1. Launch the application that you want: either through another applescript or by using Launch Application

  2. repeat the process to go to another space, and launch another app.

some notes:
  • Unfortunately, I have not found the corresponding key code to place an app on space number 5, if you do please let me know.

  • Also, this only works on the assumption that you already have the 4 spaces available (otherwise it will open things in the same space).

  • If things start to chain with previous scripts output and not work properly, remember to tick on each script/task of the automation Options > Ignore this action's input.

  • As personal observation once or twice when the computer seems busy to do something else, the prioritization of the automation seems low and the delay may be a few seconds longer.

Full example (with 4 apple scripts)

The following opens 2 google chrome windows in 2 different spaces. Code is below for copy and paste.

enter image description here

Script 1

tell application "System Events"
    tell application "System Events" to key code 18 using {control down}
end tell
delay 1

Script 2

tell application "Google Chrome"
    make new window
    open location "https://www.google.com"
    open location "https://www.apple.com"
end tell
delay 1

Script 3

tell application "System Events"
    tell application "System Events" to key code 19 using {control down}
end tell
delay 1

Script 4

tell application "Google Chrome"
    make new window
    open location "https://www.bbc.co.uk"
end tell
delay 1
Fed
  • 1,696
  • 22
  • 29