Move current window to specific size/location (of any application)

I've been working at this for quite a while now, and all I've manged to get is a rather large headache.

I often have a small chat program running, I keep it in the bottom right corner of my screen. It's kind of a stupid application, so it never pops up above active windows for an update, so I keep it uncovered - this means all my windows have to be shrunk to fit, and to not hide it.

Now, because I'm such an OCD person, I like to have a perfect, 1px border between applications, and I like things to be neat and orderly, etc. That means there's a VERY specific place all my windows go. When I open a new one, however, they can be in any shape, size, etc.

Using applescript, I've had some success - if I target a specific application, I.E., firefox, it works. Just for firefox, this is the script:

tell application "Firefox"
activate
set the bounds of the first window to {0, 0, 1173, 1178}
end tell

However, I want the script to work with ANY application. I've tried a few variations, with stuff like setting "frontapp" to the "process whose foremost is true" or something like that, and then telling frontapp to adjust it's window's bounds, but it never worked correctly, and I threw it all away.

Are there any script that are already set up to do this, or how would I go about writing a script (application) to do it? I can set up a hotkey for it, which is no problem (I already got around the 'control in the hotkey' problem, that was a pain) but the script itself is not fun. Any help?

MDD G4, Mac OS X (10.4.11)

Posted on Jun 1, 2010 4:05 PM

Reply
10 replies

Jun 2, 2010 9:16 AM in response to Snipeye

A couple things you might try...

*tell application "System Events"*
*set the_app to name of the first process whose frontmost is true*
*set visible of process the_app to false*
*set the_app to name of the first process whose frontmost is true*
*end tell*
*tell application the_app*
try
*set bounds of window 1 to {0, 0, 1173, 1178}*
*end try*
*end tell*

For the above script to work, it must be saved as an application. The saved script applet would need to be launched from the dock or from the script menu (double-clicking on its desktop icon would switch focus to the Finder, throwing what you'd expect to be the proper order of running applications out of whack).

The script's "try" statement is included to allow the script to quit silently in case your target application is not scriptable.

The action can actually be sped up considerably by assigning a hotkey to run a modified version of the above script. I tested this using Spark. A keyboard shortcut was created to execute the following AppleScript:

*tell application "System Events"*
*set the_app to name of the first process whose frontmost is true*
*end tell*
*tell application the_app*
try
*set bounds of window 1 to {0, 0, 1173, 1178}*
*end try*
*end tell*

Note that the modified script executed by Spark omits the second and third lines of the original version, allowing the action to run much faster.

Hope this helps.

Jun 1, 2010 11:44 PM in response to Snipeye

To use AppleScript, you will need to target a particular application (the tell application "whatever" bit). Sometimes you can get away with using a variable for the name of the target application, and lot of applications use the bounds property, but there is no guarantee that a particular application will use that terminology (or is even scriptable).

Jun 2, 2010 2:13 PM in response to Andrew99

That's working perfectly, thanks! I had to save it as an application and launch it from spark from there, because it wasn't working otherwise, (I've never been able to make spark run applescripts, just applications, etc) but it's working find for that! Thanks!

Of course, there's always one more thing, and that's this: Sometimes, I use x-11 applications, which means x11 stays as the active (frontmost) application. The window that I'm actually working with is a different application, so when I try to do that, it fails, because it's not the frontmost application.

This seems like a tougher nut to crack, but if anybody has any idea, that's be great. Andrew, I marked your response as helpful, thanks a ton!

Jun 2, 2010 4:24 PM in response to Snipeye

Snipeye,

You're welcome; I'm glad you had success.

You wrote:
I had to save it as an application and launch it from spark from there, because it wasn't working otherwise, (I've never been able to make spark run applescripts, just applications, etc)


Running an AppleScript with Spark is relatively easy. You might want to give it another try -- you should notice a definite speed increase when Spark runs AppleScript source code, or when it opens a compiled script, as opposed to when Spark opens a saved AppleScript application.

Try this:

Open Spark and select File > New HotKey > AppleScript (or press ⌘1). An AppleScript Action sheet should drop down. From here you have two choices: run AppleScript source code, or select a previously created compiled .scpt file to run.

1) To run AppleScript source code, make sure that the Source button is selected in the drop-down sheet. Paste the code below into the text area provided, located underneath the Source and File buttons. Give the action a name, enter a keyboard shortcut, and press Create.

2) To run a compiled .scpt file, the script would first need to be saved +as a script+ in the AppleScript Script Editor. In the drop-down sheet, select the File button (instead of Source) and then click the Choose button. Navigate to the location where the compiled .scpt file was saved and select it. Press Open, and then press Create.

Whichever method you choose, here's the code to use:

*tell application "System Events"*
*set the_app to name of the first process whose frontmost is true*
*end tell*
*tell application the_app*
try
*set bounds of window 1 to {0, 0, 1173, 1178}*
*end try*
*end tell*

Good luck!

Jun 3, 2010 3:04 PM in response to Snipeye

Snipeye,

You wrote:
Sometimes, I use x-11 applications, which means x11 stays as the active (frontmost) application. The window that I'm actually working with is a different application, so when I try to do that, it fails, because it's not the frontmost application.


You might consider a "choose from list" approach:

*tell application "System Events"*
*set all_apps to name of every process whose visible is true and frontmost is false or background only is false and visible is false*
*end tell*
*tell me to activate*
*set my_app to choose from list all_apps with prompt "Select from list of running apps. For multiple selections, depress the Command key; then select..." with multiple selections allowed*
*if my_app is not false then*
*repeat with i from 1 to the count of my_app*
*set this_app to item i of my_app*
try
*tell application this_app*
activate
*delay 0.5*
*set the bounds of window 1 to {0, 0, 1173, 1178}*
*end tell*
*end try*
*end repeat*
*end if*

If saved as an application, the script can be launched from the dock, from the script menu, from the desktop or launched using Spark. It has tested well for me using Mac OS 10.4.11.

When launched, a "choose from list" window presents a list of current running applications, excluding the frontmost script applet itself. Make a selection, or multiple selections, and press OK.

For multiple selections, hold down your Command key while selecting. The script will cycle through selected applications and resize the front window of each in its turn, skipping applications with unopened windows or any unscriptable applications which may have been selected inadvertently.

Just another idea for you. Good luck.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Move current window to specific size/location (of any application)

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