Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Moving Windows with QS and AppleScript

Moving Windows with QS and AppleScript
Thread Tools
kevinway
Fresh-Faced Recruit
Join Date: Aug 2007
Status: Offline
Reply With Quote
Aug 18, 2007, 12:19 PM
 
I wanted some hot-keys to move windows to the far edges of screens on my laptop. As such, I made some AppleScripts, and assigned them to opt-cmd-(arrows).

As an example, here is the the move_left script:

tell application "Finder"
set {disp_x1, disp_y1, disp_x2, disp_y2} to bounds of window of desktop
end tell

tell application "System Events"
set frontmostApplication to name of the first process whose frontmost is true
end tell

tell application frontmostApplication
set {x1, y1, x2, y2} to bounds of window 1

set bounds of window 1 to {disp_x1, y1, disp_x1 + x2 - x1, y2}
end tell


Unfortunately, just picking window 1 doesn't work in all applications. In Adium it results in the menu-bar icon getting moved around the screen. In Firefox, nothing happens at all.

Can somebody help me figure out how to choose the select application window correctly, so that this might work correctly? I'd greatly appreciate it, as I never even looked at AppleScript until a day ago.
     
redJag
Senior User
Join Date: Dec 2002
Status: Offline
Reply With Quote
Aug 19, 2007, 11:47 AM
 
You can reference the selected window with 'front window':

edit: actually this doesn't appear to work in Firefox. It does work in Adium and Safari, though. Firefox might have something goofy with what it considers the active window, I don't know.

Code:
tell application "Finder" set {disp_x1, disp_y1, disp_x2, disp_y2} to bounds of window of desktop end tell tell application "System Events" set frontmostApplication to name of the first process whose frontmost is true end tell tell application frontmostApplication set {x1, y1, x2, y2} to bounds of front window set bounds of front window to {disp_x1, y1, disp_x1 + x2 - x1, y2} end tell
Travis Sanderson
     
numero
Junior Member
Join Date: Mar 2000
Location: Salem, OR, USA
Status: Offline
Reply With Quote
Aug 19, 2007, 03:56 PM
 
You've run into two problems here.

1) Not all applications include an AppleScript dictionary.
2) Just because an application does contain an AppleScript dictionary it doesn't mean all commands have been implemented or that the command you want uses the keywords you expect.

If your process is going to work I see it needing to do two things:
1) Once you have discovered frontmostApplication you will need to have "if" statements for every application you typically have open.

2) Inside each "if" block you have three choices to decide amongst.
a) The application has full "normal" AppleScript support and you will use the method you mentioned in your post.
b) The application has non-normal AppleScript support. You may be able to get the frontmost window, but it isn't known as frontmost. Maybe it will be "window -1".
c) The last resort is UI Scripting. See Apple - Software - AppleScript - GUI Scripting for instructions. Also see my post at http://forums.macnn.com/79/developer...t/#post3374191 for a working example.

For Firefox you would want to do something like

tell application "System Events"
tell process "Firefox"
tell window 1
set position to {5, 50}
set size to {300, 500}
end tell
end tell
end tell

Let me know if you have any questions on this.

-numero
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 04:32 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,