PDA

View Full Version : Applescript, dual monitors, and full screen Chrome




drakino
May 7, 2011, 01:13 PM
I'm working on an Applescript to manage a Mac Mini attached to two (soon to be three via a USB video adaptor) TV screens that display various web based stats views.

Chrome was picked for this due to it's full screen mode that removes all the browser UI, along with the Applescript support that was added in a few versions back.

The specific problem I'm having is getting both Chrome windows to go full screen. I can get one to work, but any attempts to bring the second window forward and make it full screen fail. Usually the first window just exits full screen.

Here is one of my iterations at attempting this, if anyone has any ideas, this would be appreciated. This may be hard to test if you don't have two monitors.

(I'm including a slightly cut down version, but including some useful code here if someone else is attempting similar. The code below will open Chrome, loading two tabs in the first window, and one tab in the second. It also attempts to reposition the browsers so one is on the left TV {0,0,1920,1080} and the second is on the right TV {2560, 0, 5120, 1080}. I chose not to auto detect both monitors and position accordingly, as resolution and arrangement won't be changing once this is all set up. The script does assume the monitor on the left has the Mac menu bar to flag it as the primary screen)


tell application "Google Chrome"
tell window 1
set URL of active tab to "http://apple.com"
repeat while loading of active tab
delay 0.1
end repeat
set myTab to make new tab at end of tabs with properties {URL:"http://trailers.apple.com"}
repeat while loading of active tab
delay 0.1
end repeat
end tell
end tell

tell first window of application "Google Chrome" to set bounds to {0, 0, 1920, 1080}

tell application "Google Chrome"
set aWin2 to make new window with properties {mode:"normal"}
tell window 1
set URL of active tab to "http://me.com"
repeat while loading of active tab
delay 0.1
end repeat
end tell
end tell

tell first window of application "Google Chrome" to set bounds to {2560, 0, 5120, 1080}

-- Bring Chrome to the front and full screen both windows
tell application "Google Chrome"
activate
end tell
tell application "System Events" to keystroke "f" using {command down, shift down}
delay 3
tell application "Google Chrome"
set index of window 2 to 1
end tell
tell application "System Events" to keystroke "f" using {command down, shift down}



jiminaus
May 7, 2011, 10:53 PM
I think you've encountered a limitation of Chrome. It does kind of make sense that only one window would be allowed to be "full"-screen at a time.

An idea: Have a single Chrome UI-less window stretch across all the screens. Load a local HTML file that loads the desired pages into frames positioned so that there's one frame per screen.

drakino
May 8, 2011, 12:14 AM
It doesn't appear to be a Chrome limitation, as I can manually make two windows go full screen on different monitors. The way I did it manually was to open two windows, move one fully to the second monitor, type Command-Shift F, then click once on the window not in fullscreen and press the same key combo. Both remain in full screen mode.

If I can't get the Applescript method working in the same way, I'll look into implementing your idea. Hadn't really thought about using frames, hopefully that doesn't break some of the javascript in use on some of the web pages to be displayed.

jiminaus
May 8, 2011, 03:28 AM
Okay. I don't know AppleScript, but I got the following to work.


tell application "Google Chrome"
activate
set aWin1 to make new window with properties {mode:"normal", bounds:{-1000, 0, -500, 500}}
tell aWin1
set URL of first tab to "http://apple.com"
set tab2 to make new tab at end of tabs with properties {URL:"http://trailers.apple.com"}
end tell
end tell
tell application "System Events" to keystroke "f" using {command down, shift down}

delay 1

tell application "Google Chrome"
set aWin2 to make new window with properties {mode:"normal", bounds:{0, 0, 500, 500}}
tell aWin2
set URL of first tab to "http://me.com"
end tell
end tell
tell application "System Events" to keystroke "f" using {command down, shift down}


There's many more edits to your code then were necessary, I took the opportunity to learn some AppleScript.

The idea is to setup one window, make it fullscreen, and then go into setting up the second window and making it fullscreen.

The delay is critical. If I tried to make the second window fullscreen while the first window is still in the process of animating into fullscreen, it would abort the fullscreen mode of the first window.

Also I needed to setup the window on secondary screen before setting up the window on the primary screen. If I did the other way around, the menu bar and dock showed.

(BTW I had to muck around with your windows' bounds because my monitors are in a different configuration to yours).


EDIT: This is also works if you need the pages to be loaded before Chrome is activated. But it will break if the titles of the individual windows is not unique.

tell application "Google Chrome"
set windowOnSecondaryScreen to make new window with properties {mode:"normal", bounds:{-1000, 0, -500, 500}}
tell windowOnSecondaryScreen
set tab1 to first tab
set tab1's URL to "http://apple.com"
set tab2 to make new tab at end of tabs with properties {URL:"http://www.google.com"}
end tell

set windowOnPrimaryScreen to make new window with properties {mode:"normal", bounds:{0, 0, 500, 500}}
tell windowOnPrimaryScreen
set tab3 to first tab
set tab3's URL to "http://me.com"
end tell

repeat while loading of tab1 or loading of tab2 or loading of tab3
delay 0.1
end repeat

set titleOfWindowOnPrimaryScreen to windowOnPrimaryScreen's title
set titleOfWindowOnSecondaryScreen to windowOnSecondaryScreen's title

activate
end tell

tell application "System Events"
set chromeWindowMenu to first menu of menu bar item "Window" of first menu bar of process "Google Chrome"
tell process "Google Chrome" to click menu item titleOfWindowOnSecondaryScreen of chromeWindowMenu
keystroke "f" using {command down, shift down}
delay 1
tell process "Google Chrome" to click menu item titleOfWindowOnPrimaryScreen of chromeWindowMenu
keystroke "f" using {command down, shift down}
end tell

drakino
May 9, 2011, 01:05 PM
Much appreciated jiminaus. I ended up using code from your second sample, and injected a few other delays due to some problems that cropped up from time to time once it was deployed to the Mini.

I'm going to let it run for this week and see how it handles it. Due to some memory leaks in the web pages being displayed, I'm running the overall script in a big loop that quits the browser from time to time. Once I've hardened the script a bit more, I'll post it here, as it is working well for a TV kiosk type setup.

drakino
Sep 16, 2011, 05:48 PM
I've still been making tweaks to this script (and even expanded it to support 3 screens). Some stability issues persist, but I'm hoping a rewrite here soon will fix some outstanding issues. Once I verify this planned rewrite, I'll post it here.

In the interim, anyone interested in using Chrome with applescript like this should star the following enhancement request. http://code.google.com/p/chromium/issues/detail?id=96915

And if the planned system is going to be running Lion, and multiple monitors, staring this issue would be appreciated.
http://code.google.com/p/chromium/issues/detail?id=96913

mikeryan
Nov 7, 2011, 06:49 PM
I'm interested in doing something similar. Glad to find this post.

When I followed the link you gave to Issue 96915, Feature request: Applescript call to enter full screen and presentation mode, I noticed that it indicates the status is Fixed and Closed on Oct 25.

Does that mean it should work now? Or, will it be included in the next release of Chrome? How do you tell?

drakino
Nov 7, 2011, 08:38 PM
When I followed the link you gave to Issue 96915, Feature request: Applescript call to enter full screen and presentation mode, I noticed that it indicates the status is Fixed and Closed on Oct 25.

Does that mean it should work now? Or, will it be included in the next release of Chrome? How do you tell?
It's checked into the trunk, and I believe it's lined up with Chrome 17, possibly 18. Their version tracking is a mess to figure out as an outsider.

The downside for me is that due to them refusing to address bug 96913, I've had to hold back the Chrome release on our internal Lion systems to Chrome 13. Updating to 14 breaks the systems with multiple monitors, and Google Chrome developers believe Apple has to fix it. Stuck between two stubborn dev teams I suppose.

I have done some investigation into swapping over to Firefox, but haven't committed to that path yet. The TVs is a "free time" type project at work, and my department has been busy with other more important things. Once we broke the Chrome autoupdate, we found the TVs to be much more stable, since some random update won't be auto applied and break things.

gregor.hoch
Aug 2, 2012, 02:25 AM
Anything new about this? Mountain Lion did NOT solve the problem... :(
What are your solutions?