So, I think this should be a pretty simple thing to do, but I can’t seem to figure it out…
How do you get a script do to something to the affect of when you launch a program it does something as simple as
Well - it sounds simple and it could be - but it’s not practical. What you’d have to do is have a script in a loop that is checking to see if a certain process (the application is running)
for example - if you had this script run every minute or so - I guess you could do it… but like I said - I don’t know that it’s practical.
tell application "System Events"
if process named "TextEdit" exists then
say "TextEdit is Open" using "Fred"
else
say "TextEdit is NOT open" using "Fred"
end if
end tell
So, you cant have a script auto-run when a program launches? Short of a system wide loop check of whether or not the program is open?
Ok, so to further reveal my applescript newbieness, how about a plug-in for the application? Can applescripts be saved as plugins? that launch when the app does?
There are certainly ways to do it, but they all involve a ‘watcher’ of some sort because what is otherwise required is that the application you want to have trigger the script will have to do it for you and most won’t.
The usual ‘watcher’ is an on idle script which consumes practically no resources between cycles. This must be saved as a stay-open application and left running.
on idle
tell application "System Events" to if exists process "Safari" then
say "Safari is running"
else
say "Safari is not running"
end if
return 10 -- this is the cycle time in seconds
end idle
Not to mention the fact that regardless of which method you choose, every time the script cycles, it will speak, so now you have an annoying voice piping every few seconds that some application is running. Introducing a property into Adam’s script will stop that:
property has_spoken : 0
on idle
tell application "System Events" to if exists process "Safari" then
if has_spoken = 0 then
say "Safari is running"
set has_spoken to 1
end if
else
say "Safari is not running"
set has_spoken to 0
end if
return 10 -- this is the cycle time in seconds
end idle
Now, it speaks once, and will not speak again until Safari has been shut down and then restarted.
Yes, that should certainly help to reduce the potential irritation factor.
A further refinement might be to compare the current running status with the previous one ” so that announcements occur only after a change of status. (The above routine continues to make announcements following a quit.)
Perhaps something like:
property target_process : "TextEdit"
property initial_announcement : true
property was_running : missing value
on idle
tell application "System Events" to set is_running to exists process target_process
if is_running is not was_running then
set was_running to is_running
if is_running then
say target_process & " is running"
else
say target_process & " is not running"
end if
end if
10
end idle
if initial_announcement then
set was_running to missing value
else
tell application "System Events" to set was_running to exists process target_process
end if
You’ve failed to mention the application. Yes, many applications do support plugins. BUT… the number of different types and formats for plugins is equal to the number of apps that support them. Every app that has a plugin architecture has to implement it’s own system of allowing the plugin to speak to it (in a meaningful way, of course), so no one solution is going to just WORK for all apps. Plugins do all sort of things, so making sure that the app has a mechanism for interfacing with it in a way that we can use is important. Some apps support plugins that only do certain things, and don’t leave a lot of room for doing others.
Not necessarily. A plugin is usually written in some other language, such as one of the c languages, java, etc. There are some apps which natively support applescripts, and some others like Xcode that actually implement their plugin architecture through the use of applescript-based plugins.
Again, knowing the actual app you’re trying to be notified about is the first bit you could share. Can’t make any promises, but there may be no other way than what the guys have suggested above… and there’s a good chance that no one solution will work for more than one specific app. Applescripting in this way can tend to be quite un-modular, with many solutions being custom-build only.
Thanks for all of the input guys, its been very educational. The app I was playing with (referencing the plug-in portion) was iPhoto. But no worries, I was more just trying to figure some things out and get my feet wet and have a little fun. Thanks again, I really appreciate it. Maybe now I’ll move on to something useful