Managing Mac OS Windows from Script - macos

I am trying to find the best way to handle application windows on Mac OS from a script. I am open to any language, but I want it to have the following capabilities
Get list of open applications
Get location of specific application
Change focus, position, and size of specific application
Send key events to only specific application
I have managed to get a list of applications using JNA, Apple's Core Graphics library, and some code from this question, but the capabilities seem to be limited. Is there a way to implement the functionality I am looking for, or is Mac OS too closed down for this? Like I said, I am open to all languages.
Thanks so much!

Related

Counting printed pages in OSX

I'm developing an app for the Mac OS X that provides various system statistics. I have a little problem with the printer because I have never worked with it before. Is there any way to listen to the printer and count pages printed in color and in b&w?
Edit: I remember that I was using a Mac OS X kiosk machine once and it offered color and b&w prints priced per page, so there must be a way to do this. Also, I develop this app for my own needs and for practice so the solution doesn't have to be universal.
I'm pretty sure access to this information is specific to individual printer models. I don't believe there's a system API that will help you here as it would require querying individual printer drivers or utilities.
The Core Printing Reference which is the documentation for the Mac OS X printing API doesn't appear to contain any calls that would allow you to extract this information from a printer.

mac os x Component Object Model equivalent

I'm trying to port an application I've written in Qt from the windows platform to the Mac OS X platform.
The application is relatively simple:
It queries the user for a document (either MS Word, or OOo Writer document). It than launches that that document inside the
respective application, and than replaces various text elements with other data (Think Mailmerge).
It starts up the application and does the text replacing using QAxObject which is wrapper for COM.
Now I'm wanting to port this to the Mac OS X. I've installed Qt Creator on the Mac etc., but obivously COM is a windows technique not available on the Mac OS X.
So I've been looking around for techniques on the Mac OS X that are similiar to COM.
For now, I'm especially interested in using the OOo API http://api.openoffice.org/.
I'd like some pointers which techniques I should be looking at. I'm also willing to accept that this just plainly is not possible at all.
Thanks in advance.
A bit of information about COM on OS X is available in this 2004 article from O'Reilly's MacDevCenter.
From the description of your problem, however, you're looking for something that works with Apple Events. Apple has developed an entire language for working with Apple Events, so most people equate them with the language -- AppleScript. You'll want to start exploring the field of scripting GUI applications with exploration of AppleScript, or Apple Events.
Each directly scriptable app has a "dictionary" of "verbs" and "nouns" you can manipulate. Nouns have properties, and name of the property is often either a string or the name of another noun (or a plural of a noun, which implies a collection - an array).
If the app doesn't have a dictionary (i.e. it isn't scriptable) or doesn't provide what you need via the dictionary, it is possible to send generic UI scripting commands to an "application" called "System Events".
Hmmm -- not a lot of experience in the OOo arena, but have you considered using UNO, the component model that is part of OpenOffice?
Some documentation can be found in the Developer's Guide here.

is there a scripting solution for determining the default application path for a file on the Mac?

For a given extension, for example ".psd", I'd like to be able to determine the default application path for opening this file, for example "/Applications/Adobe Photoshop CS4.app".
I've looked into the Launch Services API, and there are clearly programmatic ways to get this information. Unfortunately for my particular scenario, only a scripting solution (Applescript or shell script) will do.
I've also looked at "lsregister -dump". It seems to be unwise to rely on parsing this information, since there are no guarantees as to the stability of the output format.
I've been solving this problem in the past with Creator Codes, but since Apple seems to be phasing them out since Snow Leopard I'm trying to eliminate dependence on Creator Codes.
thanks
Launch Services is the one and only place to get that information. You can write a scripting addition that will expose its functionality to AppleScript, but then you have to install that on whatever machine you plan to run on.
System Events does give you this in Leopard
alt text http://img.skitch.com/20091222-eessetxeqbai2mnwduygtm1cd5.png

Running a native program in Adobe AIR

I need to run a native program and retrieve the output in an Adobe AIR client side only application. I know that AIR does not allow access to native applications, so what is the best way to achieve this? I came across this solution which works on Windows, but I need something that works on Mac as well. I'd rather not have two separate solutions if possible. Any suggestions?
I believe that Shu might be what you're looking for. It costs money, but its an API and bundler for AIR applications that gives them additional functionality.
To quote their site:
Shu provides you the developer with a
toolkit of commands to extend the
system capabilities of your AIR
application, features include,
controlling external applications,
opening external files, database
connectivity and control, system path
retrieval and screen capture
functionality.
I believe that its cross platform as well.
Also see
http://www.merapiproject.net/
http://aperture.fluorinefx.com/

Change Sound (or other) System Preferences in Mac OS X

I'd like to be able to switch the sound output source in Mac OS X without any GUI interaction.
There are tools to do control the sound output, such as SoundSource and an applescript to open the preferences dialog.
What I am looking for is something that switches the preference instantly, like SoundSource but it has to be scriptable. The goal is to switch between my digital and analog output with one keystroke. I have a helper application that will launch a program or applescript on one keypress. All I need now is the applescript or application that switches the sound source quickly without any user interaction.
I'm willing to write some Objective-C if that is what it takes, but I'm pretty much a newbie at Cocoa development.
Do you have a one-click solution or can point me to a good tutorial on controlling sound system preferences from a Cocoa App or command line?
EDIT: I created a command-line application to do exactly this. You may download it at http://code.google.com/p/switchaudio-osx/downloads. Source code is available on the project site as well.
I created a command-line application to do exactly this.
You may download it at http://code.google.com/p/switchaudio-osx/downloads. Source code is available on the project site as well.
UPDATE (Dec. 2014): the code is now hosted on github -- https://github.com/deweller/switchaudio-osx. And works just fine in Yosemite.
Don’t think of it in terms of preferences; there’s no centralized system preference framework for this sort of thing. I believe what you need to do is use Core Audio to set the kAudioHardwarePropertyDefaultOutputDevice and kAudioHardwarePropertyDefaultSystemOutputDevice properties of the AudioSystemObject (using AudioHardwareSetProperty()).

Resources