Controlling other applications' windows from my JUCE app


#1

I want my application to be able to show/hide iTunes’ window when the user moves the mouse in a certain region of the screen. On Windows, I managed to do this using the Win32 functions:

FindWindow() // used to find the iTunes window
MoveWindow() // to set the position of the iTunes window
ShowWindow() // to hide/show the iTunes window

So, not to hard on Windows.

But how does this work on OS X? Is is even possible?


#2

http://www.apple.com/applescript/firsttutorial/18.html

Maybe something like…

tell application “iTunes” to get the bounds of window 1

And if performance isn’t really an issue you can compile the script on the fly…

NSString *scriptString=@"tell application \"Finder\"\r to get the bounds of window 1"; // This can't be more than 1012 characters
			NSAppleScript *iTunesScript=[[NSAppleScript alloc]initWithSource:scriptString];
			NSDictionary *errorInfo;
			[iTunesScript executeAndReturnError:&errorInfo];
			[iTunesScript release];

#3

cool, thanks. is there no way to do this without actually using applescript? just using WindowRef / Carbon code?


#4

I just tried, and using Carbon’s Window Manager functions only list Carbon windows - so I’ll have to try to use Cocoa.


#5

AppleScript worked, thanks. Just seems to be a bit slow the first time.


#6

Yea, “compiling” it is what’s taking its time. You might want to look into the raw AppleEvent that get’s sent if you really want to speed things up.

http://www.macosxhints.com/article.php?story=20030318214645101
http://www.red-sweater.com/blog/227/scripting-the-hard-way

Or you might want to look into a framework like EyeTunes which wraps all that nasty AppleEvent stuff into a nicer API

http://www.liquidx.net/eyetunes/

Good luck!


#7

Is it possible to “precompile” the scripts once for speedup and then just run them? Does the compilation occur in the executeAndReturnError method? Sorry for such stupid questions, but I don’t know anything about Cocoa nor Obj-C.


#8

Plagiarized below

[quote]you can save the save the script you provided as a compiled
.scpt made in Script Editor, and include it in the bundle of your
application. You can initialize an NSAppleScript object with the URL file
path of the script - rather than the uncompiled source code - and just run
it. Use initWithContentsOfURL:error and executeAndReturnError: . You can do
so with an already-compiled script and it won’t re-compile.[/quote]


#9

Cool, thanks. Anyway, seems like the compilation time ain’t that important either (it’s actually something else that took a bit more time), so I’ll stick to the code-only version. Cheers!