Hi there,
I’d like my Xcode AppleScriptObjC application to respond to an action in another external app. Like in the example: my app opens a Quicktime movie (in Quicktime Player 7) and then I close that movie (document 1), then I want my app to do something (let’s say display a dialog) when I close that Quicktime document 1.
Thanks!
The short answer is not really. There are some events you can watch for, like an app quitting or becoming active/inactive, and in some cases you might be able to do so indirectly by tracking opened files, but generally each app minds its own business.
In this case, we know that quicktime is being opened, and a movie being played. So, we could use:
set QTWindowOpen to true
set QTWindowName to "qtTest.mov" -- or whatever the opened QT movie title is
repeat while QTWindowOpen
tell application "System Events" to tell process "QuickTime Player"
try
set currentwindowname to name of window 1
on error
display dialog "Window has closed"
set QTWindiwOpen to false
end try
end tell
end repeat
to test: change QTWindowName to name of suitable quicktime movie
run script
close quicktime window. You may also wish to put a try around
tell application "System Events" to tell process "QuickTime Player"
in my test script, I was also checking to see if the actually opened window was the requested window, such as
set QTWindowOpen to true
set QTWindowName to "qtTest.mov" -- or whatever the opened QT movie title is
repeat while QTWindowOpen
tell application "System Events" to tell process "QuickTime Player"
try
set currentwindowname to name of window 1
if currentwindowname does not contain QTWindowName then
set QTWindowOpen to false
end if
on error
display dialog "Window has closed"
set QTWindowOpen to false
end try
end tell
end repeat
First of all Shane is talking about the notification center where some notifications will come by when any application becomes active for instance. Basically they’re not application notifications but system notifications and not notify you when an application changes it’s window.
When programming AppleScriptObjC you don’t want to make use of an AppleScript helper Application like System Events but you want to make use of the same objects and classes like System Events do. Therefore I don’t recommend Woggledog’s solutions.Take a look at cocoa’s accessibility api, the same api as system events uses. So your code is faster, more Cocoa, more reliable (won’t eat up CPU and memory) and more efficient.
At last, the more professional solution, when you know C and Objective-C you could use NSAccessablityPostNotification which is aC function. You can’t call C functions directly with AppleScriptObjC because they’re not registered in the ObjC runtime. Wrap it in an Objective-C class and use that class to notify your AppleScript code. It is completely idle and gets notified when something happens in your target application. This solution is only for 5% AppleScript and the rest is C or it’s superset, I think that’s why Shane answered with ‘not really’.
Shane may have been talking about the internal notification centre, but to people who are new to ASOC, this wasn’t obvious. Not all of us have Cocoa coursing through our veins!
Yes, doing everything in Cocoa using proper notifications would be the correct thing to do.
However, the original poster asked about doing this in ASOC, not cocoa. So, yes, it’s wasteful on resources, but I offered a solution that works.
I would love, however, to see an alternative. I suppose you could use
DJ: FYI, you can actually call functions under 10.7 or later, although in practice the facility is very limited. They must be part of frameworks loaded at launch time, and they must be described in bridgesupport files. But perhaps the biggest practical limitation is that ASObjC does not support many of the structures used as arguments.
But “current application’s NSBeep()” works just fine
Shane: Correct, as you can use some C-Structs as well. Like function, these needs to be defined in the AppleScriptObjC framework and therefore also limited in use.
I’m sorry if you felt that way, but I wasn’t. I would vote for your answer in plain AppleScript but this is an AppleScriptObjC forum. I think it’s better to make use of Cocoa objects than slow Apple Events if there is a choice of course. Certainly when there is an helper application needed in between for plain AppleScript, speaking of overhead. Just my opinion that’s all