Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Applescript, Safari (or Firefox) and Spaces

Hello,

I am currently attending online classes using my new iMac and it works great 🙂 Here is my question. I am using the iMac screen and also a second monitor. I have 5 classes that require 2 windows per class.

I have opened up Safari and placed each class on its own "Space". It works awesome, except when something crashes (typically due to flash). When I reopen Safari or Firefox, all of the Windows are back on Space 1.

What I want to do, is create an applescript to move the windows to their proper spaces. I have figured out that I can get each windows name in Applescript and perform some action afterwards. My test was displaying the name of the Window, but I am not sure how to tell Applescript something like the following pseudo-code (Mind you I have excluded a lot of the earlier part of the script that gets the window name of each window of the application)

if theWindowName = "Class 1" then
move theWindowName to Space 2"
end if

Any help would be appreciated 🙂

Thanks

Greg Evans

21.5" i5 iMac, G4 Mac Mini, iPhone 3g, Mac OS X (10.6.4), Anyone wanna donate me a iPhone4? <grin>

Posted on Oct 26, 2010 9:52 PM

Reply
6 replies

Oct 28, 2010 7:13 PM in response to whyismynamewrong

I'm not really sure to understand what you are trying to do, but maybe the following handler could help, if you don't mind using [GUI Scripting|http://www.macosxautomation.com/applescript/uiscripting/index.html]. The handler should be able to move a Safari window from space 1 to another space.

IMPORTANT - The handler assumes the option “When switching to an application, switch to a space with open windows for the application” is not checked in the Exposé & Spaces Preferences

*on moveWindow(theWindowName, theSpaceNumber)*
*tell application "Safari"*
activate
*tell application "System Events" to tell process "Safari"*
*click menu item theWindowName of menu 1 of menu bar item 8 of menu bar 1*
*delay 0.1*
*keystroke "l" using command down* -- select the URL
*keystroke "c" using command down* -- copy the URL
*keystroke "w" using command down* -- close the window
*keystroke (theSpaceNumber as text) using control down* -- go to the specified space
*delay 0.5* -- adjust if necessary
*end tell*
activate
*tell application "System Events"*
*keystroke "n" using command down* -- new window
*delay 1*
*keystroke "l" using command down* -- select the URL
*keystroke "v" using command down* -- paste the URL
*keystroke return*
*delay 1* -- adjust if necessary
*keystroke "1" using control down* -- come back to space 1
*delay 0.5* -- adjust if necessary
*end tell*
*end tell*
*end moveWindow*

The following examples assume that windows “Apple” and “Google” are already open in space 1:

*moveWindow("Apple", 2)* -- http://www.apple.com/
*moveWindow("Google", 3)* -- http://www.google.com/

Hope it can help.

Message was edited by: Pierre L.

Oct 29, 2010 1:08 PM in response to Pierre L.

Here's a much better version of the handler (assuming Safari is the default browser):

*on moveWindow(theWindowName, N)* -- N is the space number
*tell application "Safari"*
activate
*set theURL to URL of document of window theWindowName*
*close window theWindowName*
*tell application "System Events" to keystroke (N as text) using control down*
*delay 0.5* -- adjust if necessary
activate
*open location theURL*
*delay 1* -- adjust if necessary
*tell application "System Events" to keystroke "1" using control down*
*delay 0.5* -- adjust if necessary
*end tell*
*end moveWindow*

As before, the above handler assumes the option “When switching to an application, switch to a space with open windows for the application” is not checked in the Exposé & Spaces Preferences.

Oct 29, 2010 8:37 PM in response to Pierre L.

And here's another improvement to the handler:

*on moveWindow(theWindowName, N)* -- N is the space number
*tell application "Safari"*
activate
*close window theWindowName*
*tell application "System Events" to keystroke (N as text) using control down*
*delay 0.5* -- adjust if necessary
activate
*tell application "System Events" to tell process "Safari" to click menu item "Reopen Last Closed Window" of menu 1 of menu bar item "History" of menu bar 1*
*delay 1* -- adjust if necessary
*tell application "System Events" to keystroke "1" using control down*
*delay 0.5* -- adjust if necessary
*end tell*
*end moveWindow*

Applescript, Safari (or Firefox) and Spaces

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.