Here is a story. I record some stuff daily from television with EyeTV. I also download torrents daily (all day, mostly). When I download torrents, while I record a TV show, the recording often gets choppy and skips a lot, and makes artifacts in the picture. In other words, torrent f*cks up my recordings.
I would like to make a script, that does the following:
It runs all the time (stay open), and waits for this condition: If the application “EyeTV” becomes active (it starts recording), then the application “Transmission” will quit.
I guess that should be simple enough, or what? I’d also like it to start up Transmission again, when the recording is over. But I don’t know if it can detect when EyeTV has finished recording, because EyeTV does not quit when the recording is over. If it can just shut down Transmission, when EyeTV starts up, I’m a happy man.
I have been trying to understand how to make such a simple script, but it’s a jungle for me. I found out how to start up an app, and how to shut it down. But I don’t know how you make conditions (if’s), and set up the script otherwise… I’d like to learn though.
Thanks for your answer. Transmission is always running on my machine (pretty much). Shouldn’t it be possible to make a script that just assume Transmission is running, and then quit it, if EyeTV starts up? And I guess if the script tries to quit an app that is not running, then just nothing will happen? Or does it not work that way?
So eyeTV is not normally running? In that case you need an “on idle” script like so:
on run
(*do setup - this runs when the application is started to set things up. It is not necessary to have an 'on run' handler because the 'on idle' handler runs immediately after this anyway. If you want this to run all the time after every login, list it your startup items. You quit it from it's dock icon.*)
end run
(*'on idle' runs immediately following the 'on run' handler if there is one, and thereafter on the interval specified in the return at the end. (An 'on run' handler is not required, so omit if there is no setup)*)
on idle
-- In this section, you do your script operations every interval
return xx -- do this every xx *seconds*.
end idle
on quit
(*do stuff - assumes you want to clean things up or save preferences or change properties before the program quits. This section runs only if the application is quit from the dock, or by any other means. It is not necessary to have one.*)
(*if there is an on quit handler, then 'continue quit' must be the last statement or the script will not stop! The presence of an 'on quit' handler 'grabs' the system command to stop, so it will not complete the quit process until notified to do so. 'continue quit' does that.*)
continue quit
end quit
To see if an app is running you ask System Events:
tell application "System Events" to set running to exists process "eyeTV"
-- or whatever it's process is in the Activity Monitor
if running then -- quit something here.
You said you knew how to quit and start programs, so that’s all you need.
I’m very new to this, so I don’t understand how to weave it all together. I don’t know all the syntaxes. But I tried to do something here, just for testing. I tried to have it check if “Mail” is running, and if it is, then quit “TextEdit”:
tell application "System Events" to set running to exists process "Mail"
if running then tell application "TextEdit" to quit
…but it gives me an error. “Can’t set running to true”…
But I have figured out, that “EyeTV” supports AppleScript. It has a Triggered Script command, which is RecordingDone. It’s described like this “Scripts using this trigger will start after a recording has finished.”. I understand what that means, and I know where to put the scripts for EyeTV to use it. But again, I don’t know how to use the command in a script.
I have also discovered the Dictionary, which seems to be a good place to get help, once I figure out how to use the commands. I hoped the Dictionary also had some example code, so I could see how to do it. But I could not see any.
I hope I’m not annoying… I’m trying my best to understand, but I have a very hard time wrapping my head around this… I’m sorry.
Model: 24" iMac (2006 - white)
AppleScript: 2.2.1 (100.1)
Browser: Safari 525.20.1
Operating System: Mac OS X (10.5)
Thanks, both, a lot for your help! I made my first working script just now. Here:
on idle
if application "EyeTV" is running then
tell application "Transmission" to quit
end if
return 60
end idle
I know it’s not “hardcore”, but it does the job. I saved it as a Stay Open application, and had it startup when the machine starts up. I tested it, and it works great. I still have a couple of questions though.
Is it possible to turn Time Machine on and off, with AppleScript? If yes, how do you do that? I looked for it in the dictionary for a while, but couldn’t spot anything about Time Machine.
Is it possible to hide the script, so I don’t see the icon in the dock all the time? I tried to tick Hide in the Login Items, but that is ignored.
Drop Script Backgrounder is the easy way to go to convert a script or app to run in the background. Since it won’t then show in the dock, you have to quit it with a script or in the Activity Monitor.
I don’t know how to turn off the Time Machine except by using GUI scripting.
I figured it out. It was because I saved as an application - I should save it as an Application Bundle. Then it works. There is also an app called Dockless, which is just as good. Maybe even better, it has more features than DSB.
Thanks for all your help.
Anyone know the Terminal command or AppleScript command for turning Time Machine on and off?
Thanks for all your help Adam. But I meant turning the service on and off (like the slider in Time Machine Preferences) - not starting the backup process. You’d think that was somehow possible, but I can’t find anything about that on the net, so maybe that is just not possible?
Thanks for sharing it, too – I’ll build it into my backup script.
Here it is in a form that simply toggles it on and off:
-- If ON turn it OFF, else turn it ON
if (do shell script "defaults read /Library/Preferences/com.apple.TimeMachine AutoBackup") = 1 then
do shell script "defaults write /Library/Preferences/com.apple.TimeMachine AutoBackup -boolean NO"
else
do shell script "defaults write /Library/Preferences/com.apple.TimeMachine AutoBackup -boolean YES"
end if