English 中文(简体)
Controlling OSX windows
原标题:

I m trying to control windows of a foreign OSX applications from my application. I d like to 1. move the windows on the screen 2. resize the windows on the screen 3. change the currently active window of the application 4. get the currently active window.

(And I d like to do this either through ObjC/C/C++ apis).

What are the API calls that I should be looking for, considering that I have the CGWindowIDs of the windows that I want to control? That is, I d expect to find functions with signatures of something like: MoveWindow(CGWindowID winId, int x, int y), ResizeWindow(CGWindowID winId, int width, int height), Activatewindow(CGWindowID winId), CGWindowID GetCurrentlyActivatedWindow().

For 3, I m already using SetFrontProcess to pull a process to be up front, but this doesn t let me select the specific window of a process if it has multiple.

最佳回答

One way of doing this is indeed to use the accessibility APIs.

An application I m developing is doing just this to get the front window, the document path of the front window and many other attributes.

The way I m doing this is through AppleScript. It can be clumsy at times, but it seems to be fairly reliable. I use AppScript to send AppleScript from within my Cocoa app. It s thread safe and more stable than the alternatives - either Scripting Bridge or NSAppleScript.

The difficult bit will be identifying a window using it s window ID in AppleScript - AppleScript doesn t seem to have a window ID property that matches up to CGWindowID. However, you can get any window you want using AppleScript.

  1. Move frontmost window to 100, 100

    tell application "System Events"
     set theprocess to the first process whose frontmost is true
     set thewindow to the value of attribute "AXFocusedWindow" of theprocess
        set position of thewindow to {100, 100}
    end tell
    
  2. Resize frontmost window to 200, 300

    tell application "System Events"
      set theprocess to the first process whose frontmost is true
      set thewindow to the value of attribute "AXFocusedWindow" of theprocess
         set size of thewindow to {200, 300}
    end tell
    
  3. Change current window of the frontmost application

    tell application "System Events"
      set theprocess to the first process whose frontmost is true
      set thewindow to the value of attribute "AXFocusedWindow" of theprocess
         set size of thewindow to {200, 300}
         windows of theprocess
         -- Code to get ID of window you want to activate
         tell window 2 of theprocess -- assuming it s window 2
               perform action "AXRaise"
         end tell
    end tell
    
  4. Window that s active

    tell application "System Events"
     set theprocess to the first process whose frontmost is true
     set thewindow to the value of attribute "AXFocusedWindow" of theprocess
    end tell
    

There s an application available for AppScript called ASTranslate that will turn this AppleScript into the Objective C code that calls the relevant commands in AppScript.

For more information on how to get the size and bounds of windows (these are read only as far as I m aware) see the Son of Grab sample application.

问题回答

Use CGWindowListCopyWindowInfo to grab the kCGWindowOwnerPID of each window. Then you can use "distributed objects" if you want access to ObjectiveC stuff, or Mach RPC for other stuff. All of this is documented at http://developer.apple.com

There are many good reasons for wanting to message other applications - for instance when developing novel user interfaces that are neither mice nor keyboards.

Based on your comment, you can do this with the OS X accessibility API, but I believe "access for assistive devices" must be turned on in the user s Accessibility preferences.

There s a third-party shareware app whose name escapes me at the moment that lets you move any window around (and I think resize it) with keyboard commands.

Accessibility needs to be enabled in System Preferences for this to work. It s applescript, but could be used in objective-c with the scripting bridge.

-- Moves safari window by deltaposition
tell application "System Events"
    tell application "Safari"
        set win to first window
        set b to bounds of win
        set deltaposition to {50, 0}
        set bounds of first window to {(item 1 of b) + (item 1 of deltaposition), (item 2 of b) + (item 2 of deltaposition), (item 3 of b) + (item 1 of deltaposition), (item 4 of b) + (item 2 of deltaposition)}
    end tell
end tell

I think you should explain why you want to do this. The only utilities I know of that move all other apps windows are Spaces and Expose, which are both supplied by Apple. If you want to take over an entire screen, there s public API for that but moving another apps windows sounds suspicious.

I m new here, so this has to be submitted as an answer instead of a comment. I wrote a .NET application (PlaceMint) does exactly this in Windows and isn t suspicious at all. PlaceMint helps you organize your windows in a structured way defined by the user. All it does is do things like move or resize windows based on the rules defined by the user. Some one asked me if I could do a port to OSX, but I never got very far because I could not find an API definition like the one provided in Windows (marshaling dll calls to user32.dll like SetWindowPos()).





相关问题
2 mysql instances in MAC

i recently switched to mac. first and foremost i installed xampp. then for django-python-mysql connectivity, i "somehow" ended up installing a seperate MySQL. now the seperate mysql installation is ...

Iterating over string/strlen with umlauted characters

This is a follow-up to my previous question . I succeeded in implementing the algorithm for checking umlauted characters. The next problem comes from iterating over all characters in a string. I do ...

Controlling OSX windows

I m trying to control windows of a foreign OSX applications from my application. I d like to 1. move the windows on the screen 2. resize the windows on the screen 3. change the currently active window ...

Switching J2SE versions on Mac OS (SnowLeopard)

My current JDK on Mac OS (10.6) is set to 1.6 and I d like to switch to 1.5. A listing of /System/Library/Frameworks/JavaVM.framework/Versions/ shows: lrwxr-xr-x 1 root wheel 10 Nov 3 18:34 ...

Scrolling inside Vim in Mac s Terminal

I ve been googling around trying to figure out if it s possible to use my mouse wheel to scroll while inside Vim in Mac s Terminal, with no luck. It seems as if only X11 or iTerm support this. Before ...

export to MP3 from quicktime API

A question for Apple,QT programmers. Would like to know if it s possible to export a Movie object to MP3 using the QuickTime API. Preferably the ConvertMovieToFile function. I ve looked at ...

热门标签