Open 3 Apps, Position and Resize Windows

Hello. I have searched around, but nothing quite answers exactly what I’m looking for. I’m looking to setup my desktop with a single script that:

  1. Open 3 Terminal windows (opening one window was easy, I’m not sure how to open 3 of the same app with Applescript)
  2. Position and resize each of these windows (I can do this much)
  3. Open Firefox, resize and position
  4. Open TextMate by passing a command into a Terminal window (so that I can ensure Textmate loads the correct project)
  5. Resize and Position TextMate

Here are my basic questions:

  • How can I open 3 Terminal apps?
  • How can I open Firefox (as far as I understand, Firefox doesn’t support Applescript – so this has to be done with System Events?)

Position and resizing I can do. And I found enough reference on how to pass in commands to the Terminal window.

Thank you.

Model: Mac Pro 2.66ghz
Browser: Firefox 1.5.0.6
Operating System: Mac OS X (10.4)

Do you want to open 3 Terminal applications or 3 Terminal windows? To open 3 windows simply repeat the command(s) to open one window three times :wink:

Here an example how to open three Terminal windows and reference them via their ids:

set winIDs to {}

repeat 3 times
	tell application "Terminal"
		do script ""
		set newWIN to (front window)
	end tell
	set winIDs to winIDs & id of newWIN
end repeat

set pos to 0
repeat with theID in winIDs
	tell application "Terminal"
		set bounds of window id theID to {pos, 10, pos + 360, 700}
		set pos to pos + 360
		do script "echo 'I am window " & theID & "'" in window id theID
	end tell
end repeat

The Firefox version I use has a (rudimentary) AppleScript support. So you can open it with:

tell Application "Firefox" to activate

The problem is that your script is actualy opening 4 terminal windows. 3 that do the right thing, and then another… seemingly parent window. Any idea on that?

Probably the 4th window is actually the first one which Terminal opens by default.
Try adding

tell application "Terminal"
	close window 1
end tell

on top of Dominik’s script.

Ciao
Farid

many thanks - Farid is right - I forgot Terminal’s behaviour to open a window at startup.

To avoid closing an existing window when you run the script and Terminal was already in use you could also use this modified script:


tell application "System Events"
	if "Terminal" is not in (name of application processes) then
		tell application "Terminal"
			activate
			close window 1
		end tell
	end if
end tell

set winIDs to {}

repeat 3 times
	tell application "Terminal"
		do script ""
		set newWIN to (front window)
	end tell
	set winIDs to winIDs & id of newWIN
end repeat

set pos to 0
repeat with theID in winIDs
	tell application "Terminal"
		set bounds of window id theID to {pos, 10, pos + 360, 700}
		set pos to pos + 360
		do script "echo 'I am window " & theID & "'" in window id theID
	end tell
end repeat

Ok great. Also, is there a decent way to position Firefox? setting the bounds doesn’t do a thing. I know it’s Applescript cababilities are limited, but maybe you guys know some tricks.

Did this work for you? With my Firefox I could only set the position («class pLcn») - ‘bounds’ were only readable :frowning:
If you don’t succeed with Jacques’ idea you could try to resize the windows with JavaScriptlets - for example like this:

set scriptPath to ((path to me) as string) & "Contents:Resources:"

repeat with n from 1 to 3
	set resizeIt to alias ((scriptPath & n & ".htm") as string)
	tell application "Finder" to open resizeIt using (path to application "Firefox")
end repeat

save this script as ‘Program Bundle’ and add three files in it’s resources folder:

file 1.htm

<SCRIPT LANGUAGE="JavaScript">
	window.resizeTo(350, 650);
	window.moveTo(0, 0);
</script>

file 2.htm

<SCRIPT LANGUAGE="JavaScript">
	window.resizeTo(350, 650);
	window.moveTo(350, 0);
</script>

file 3.htm

<SCRIPT LANGUAGE="JavaScript">
	window.resizeTo(350, 650);
	window.moveTo(700, 0);
</script>

>> Note: Running the script from Script Editor does not work - you have to save it and start it from Finder! <<

Sorry Jacques - I’ve checked it again and seen that I was trying with Firefox 1.0 (where I get an NSReceiverEvaluationScriptError: 4 with your script) … maybe I should update :wink: