Moving (and resizing?) the window of another process? (Any language.)

Discussion in 'Mac Programming' started by exscape, Oct 22, 2009.

  1. macrumors member

    Joined:
    Jul 29, 2008
    #1
    Is this possible (without too much effort)? I don't care much about the language used, as I have at least a basic understanding of all the possible candidates here. (I'm assuming Cocoa APIs will be required.)

    The program should, without any GUI if possible, find the Firefox window (if there are multiple, simply exit) and resize it to a pre-defined position (and width, and possibly height). Nothing more, nothing less. No arguments, no options, except those set at compile time.

    Any advice on how to do this?
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    AppleScript seems like a likely candidate.
     
  3. thread starter macrumors member

    Joined:
    Jul 29, 2008
    #3
    Thanks! :)
    That tiny hint pointed me to a guide on how to accomplish exactly this. Very simple, too:
    Code:
    tell application "Firefox"
    	activate
    	set the bounds of the first window to {0, 0, 1440, 600}
    end tell
    
    Edit: I ramped it up a little and made it preserve the window height, too.
    Code:
    tell application "Firefox"
    	set b to the bounds of the first window
    	set height to (item 4 of b) - (item 2 of b)
    	
    	set the bounds of the first window to {0, 0, 1440, height}
    end tell
    
    The item 4 minus item 2 is because, apparently, the list element aren't x, y, width, height as I expected, but rather the coordinates of each side of the window.
     
  4. thread starter macrumors member

    Joined:
    Jul 29, 2008
    #4
    For some reason I don't understand, this doesn't work anymore with Firefox, which seems to report its bounds as taking up the entire screen ((0,0,1440,900) here).
    Here's my new version:

    Code:
    tell application "System Events"
    	-- Set position (The menu bar will be taken care of automatically)
    	set position of window 1 of process "Firefox" to {0, 0}
    	
    	-- Get the size, in the format {width, height}
    	set height to item 2 of (get size of window 1 of process "Firefox")
    	
    	-- Finally, set the size
    	set size of window 1 of process "Firefox" to {1440, height}
    end tell
    
     

Share This Page