Reputation: 186
I am making a startup script to start various applications and set their locations and size. It works for everything except chrome. I need a script that checks if Chrome is open, and if it is open the current window, if it isnt activate it. Then, set its size and position
-- Start terminal
tell application "Terminal"
if (exists window 1) then reopen
activate
do script "screenfetch"
end tell
delay .5
tell application "System Events" to tell process "Terminal"
tell window 1
set size to {960, 600}
set position to {2880, 0}
end tell
end tell
delay .5
-- Start Chrome
tell application "Google Chrome"
if (exists window 1) then
reopen
else
activate
end if
end tell
delay .5
tell application "System Events" to tell process "Google Chrome"
tell front window
set size to {960, 600}
set position to {1920, 0}
end tell
end tell
delay .5
This works for Terminal. It starts a terminal and puts it in the top right of my second monitor. However, with Chrome it will either: say "cannot find Window 1 of Chrome" or, it will bring chrome to the front and won't set its size or position.
Upvotes: 1
Views: 918
Reputation: 22948
Whenever possible, it's best to simply tell the application to do something directly rather than trying to route it through System Events. In this case, Google Chrome supports the basic window
object from AppleScript's standard suite, so you can do something like the following:
tell application "Google Chrome"
activate
if ((count of windows) = 0) then
make new window with properties {bounds:{1920, 22, 960, 622}}
else
set bounds of window 1 to {1920, 22, 960, 622}
end if
end tell
Upvotes: 2