There is a version of this script for Mavericks further down the thread
-- MacScripter / Sizing an apps window, then bringing only that window to front
-- http://macscripter.net/viewtopic.php?id=39849
script genUI
-- © McUsr 2012 and put in public domain, you may not post this as a stand-alone thing
-- anywhere else, nor put into a public repository.
-- Please refer to this link: http://macscripter.net/edit.php?id=157665
on posFrontWinForAppAndDisplay(appname, x1, y1, W, H)
-- Width is the actual width, Height the actual height for how apps treats those parameters differs slightly.
-- you'll have to figure out the formulaes outside the handler.
-- most often the formula for the width is width - x1, and height respective is height - y1.
local success
set success to my getFrontWinForAppAndDisplay(appname)
if success then
tell application id "sevs"
tell application process appname
try
set position of its front window to {x1, y1}
set size of its front window to {W, H}
end try
end tell
end tell
end if
end posFrontWinForAppAndDisplay
on getFrontWinForAppAndDisplay(appname)
-- gets the front window to front for an app
-- returns true if it manages, false otherwise
local bid, success, failure, pass, curAppName, curBid
set {success, failure, pass} to {false, false, 1}
repeat while success = false
tell application id "sevs"
if pass = 1 then
if UI elements enabled = false then set UI elements enabled to true
set curAppName to name of first application process whose frontmost is true
set curBid to bundle identifier of application process curAppName
if {appname} is not in name of application processes then
set failure to true # it wasn't running so we're exiting
else
if {appname} is not in name of (application processes whose visible is true) then
set visible of application process appname to true
set frontmost of application process curAppName to true
end if
end if
end if
if not failure then tell application process appname
local wnCount
set wnCount to count (its windows whose role is "AXWindow")
if wnCount = 0 then
# no regular windows open, there may be, however, in other spaces.
if pass = 2 then set failure to true
# there are no open windows in any space and we fail.
else
set bid to bundle identifier of it
try
local dummy
set dummy to position of its front window
# if this doesn't fail, then we do have a window in the current space
set success to true
end try
end if
end tell
end tell
if failure then
do shell script "open -b \"" & curBid & "\""
return false
else if not success then
do shell script "open -a \"" & appname & "\""
# some apps open an empty window if there was none.
set pass to pass + 1
end if
end repeat
do shell script "open -b \"" & bid & "\""
# no use in this if we have made the app visible!
# then we activate the previous app, and the bring the window forward.
return true
end getFrontWinForAppAndDisplay
end script
-- Example calls
-- genUI's posFrontWinForAppAndDisplay("FreeMind", 0, 22, 800, 800)
-- genUI's getFrontWinForAppAndDisplay("TextEdit")
-- Xcode uses the "non-flipped" "cocoa model" of window addressing, where the left lower corner is the start point
-- for x1, y1 You'll have to trial and error a little!
-- Terminal is even worse, haven't figured it out, but VLC, FreeMind, and such apps works!
-- When you pass the width parameter, be sure to add up for x1, and the same goes for height where you should
-- add in y1.
New Stuff
This is the second handler for generic UI that is residing in the GenUI library.
It has the dedicated purpose of just bringing an app’s window to front. It is intended to be very good at that, as long as the app are running:
You can check the return value to see if the handler succeeded or not, before you start changing the window size and such. It leaves the previous current app current, if the previous app didn’t have any window open or such.
Purpose:
First and foremost to be enabled to make small automator services, which each is used for activating a specific app, by a specific hotkey. To make it easy to switch betweeen the apps you use the most. I use the keys cmd-opt 2 thru 0 and beyond, on that row, as the key combination is easy to type on a Macbook Pro keyboard.
Why not just use the 1 or 2 together with command-tab?
That may be a slow operation, if you have many windows open. And it requires more keypresses/chances to do wrong.
Usage:
A working Automator Service look like this:
set hfsScriptName to "Hfs Path:To:Where:You:Saved:GenUI.scpt"
set genUI to genUI of (load script (hfsScriptName as alias))
genUI's getFrontWinForAppAndDisplay("BBedit")
The Service should take no input from any app, you add an AppleScript Procedure and add the code above with your changes. When that is done, you enter the keyboard preferences: use the snippet below for quick access, and assign a keyboard shortcut to it, then try it!
script displayKeyboardPrefPane
tell application "System Preferences"
tell anchor 2 of pane "com.apple.preference.keyboard" to reveal
activate
end tell
end script
tell displayKeyboardPrefPane to run
What it does:
It bring a specified App’s first window to front, whether that window is hidden or minimized. If the App has no windows open, the App may create one. This pair of handlers are designed to just one thing each, and do them well, as robustly as possible.
Caveats:
-
The first one is that, if we are active in one space, and the App has windows open in another space, and those windows are hidden, then all the windows of that App is brought forward. This is just how it is, as we can’t run around in different spaces, register the current App, and test if the invisible app has windows open there… err we can, but it won’t look good.
-
The second caveat is that an App can be associated with a space, so that you will be teleported to that space, and a new window made there, if the app is running but has no open windows. But this you’ll figure out when it happens, if it happens and can deal with it, the obvious thing, is either to accept it as something you want, or disassociate the App with the space, (unassign it).
Enjoy!
Edit
It doesn’t work with HelpView, so helpview you would have to switch from, one way or another.