Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

brenm666

macrumors regular
Original poster
May 30, 2010
161
0
I would like to know if it would be possible to schedule the opening and the quiting of an application at certain times. Let's say for example I would like to start Mail at 7 am and i would like it to quit at 8 pm.
I've googled a bit, found a program to start any application (or document or whatever) at any time, but its not able to quit it at a specified time. I also read something about iCal, but since im new to Applescript, I'd like a bit of help! :D

Thanks!
 

numero

macrumors regular
Jul 23, 2002
106
3
OR
While you could get this done with pure AppleScript, a better way will mix some launchd with AppleScript.

First the AppleScript part. Make two scripts.
Script 1:
tell application "Mail"
activate
end tell

Script 2:
tell application "Mail"
quit
end tell

Now you need something to control the calling of each script at the right time of day. I suggest using launchd for this. Read up about launchd on Apple's site. It can do a lot of stuff. In this case you will want two launchd jobs. One that has a trigger at your start time and the other that has a trigger at your end time.

Why use launchd? Because your alternative is to write AppleScript code that is constantly running and gets the current time and compares it to your target time? What happens if you have a big video encoding job going and your AppleScript doesn't get to run right at 7:00:00PM. What if your script doesn't get the processor until 7:00:01PM? You might say that you will just ignore the seconds and run if you are on the "00" minute. But what if your computer isn't busy and you run at 7:00:00 and 7:00:01? In this case the worst that happens is Mail is told to activate twice. No big deal to call "activate" if it is already running. But what if you wanted to get fancier in the future and do something that you only want to do once per day?

The answer to all of this is to use Apple's facilities to schedule tasks. They have got this all worked out. The rules of launchd say that your job is guaranteed to run at or after the requested time. They can't guarantee 7:00:00, but they can get you pretty close and it will only run once.

Pay close attention to the ownership/permissions on your launchd jobs. It will not run if the permissions aren't 100% right.

Let me know if you run into any questions/problems.

-numero
 

chown33

Moderator
Staff member
Aug 9, 2009
10,750
8,422
A sea of green
I would like to know if it would be possible to schedule the opening and the quiting of an application at certain times. Let's say for example I would like to start Mail at 7 am and i would like it to quit at 8 pm.
I've googled a bit, found a program to start any application (or document or whatever) at any time, but its not able to quit it at a specified time. I also read something about iCal, but since im new to Applescript, I'd like a bit of help! :D

Thanks!
Since you can launch any script at a specified time, then you can also tell any application to quit at a specified time. You simple write a one-liner script:
Code:
tell app "YourAppNameHere" to quit
and launch that at the specified time.

I suggest letting someone else do the hard work, especially if you're new to AppleScripting.
http://www.appsandmore.com/script_timer.htm
 

brenm666

macrumors regular
Original poster
May 30, 2010
161
0
While you could get this done with pure AppleScript, a better way will mix some launchd with AppleScript.

First the AppleScript part. Make two scripts.
Script 1:
tell application "Mail"
activate
end tell

Script 2:
tell application "Mail"
quit
end tell

Now you need something to control the calling of each script at the right time of day. I suggest using launchd for this. Read up about launchd on Apple's site. It can do a lot of stuff. In this case you will want two launchd jobs. One that has a trigger at your start time and the other that has a trigger at your end time.

Why use launchd? Because your alternative is to write AppleScript code that is constantly running and gets the current time and compares it to your target time? What happens if you have a big video encoding job going and your AppleScript doesn't get to run right at 7:00:00PM. What if your script doesn't get the processor until 7:00:01PM? You might say that you will just ignore the seconds and run if you are on the "00" minute. But what if your computer isn't busy and you run at 7:00:00 and 7:00:01? In this case the worst that happens is Mail is told to activate twice. No big deal to call "activate" if it is already running. But what if you wanted to get fancier in the future and do something that you only want to do once per day?

The answer to all of this is to use Apple's facilities to schedule tasks. They have got this all worked out. The rules of launchd say that your job is guaranteed to run at or after the requested time. They can't guarantee 7:00:00, but they can get you pretty close and it will only run once.

Pay close attention to the ownership/permissions on your launchd jobs. It will not run if the permissions aren't 100% right.

Let me know if you run into any questions/problems.

-numero

ok, numero. I read about launchd and i cant make it work. Is it on terminal? I tried pretty much everything I could think of, and nothing came out else then : "launchd: This program is not meant to be run directly."

thank you for everything!


Since you can launch any script at a specified time, then you can also tell any application to quit at a specified time. You simple write a one-liner script:
Code:
tell app "YourAppNameHere" to quit
and launch that at the specified time.

I suggest letting someone else do the hard work, especially if you're new to AppleScripting.
http://www.appsandmore.com/script_timer.htm

well, chown. I tried Script Timer and it does what I want it to do! The only thing is that its a trial version, and I'm looking for something... well... free, but also a solution where I could learn a bit more of programming! ;)

but thanks a lot for the program! I might use it until I figure this thing out!
 

Billy Boo Bob

macrumors 6502
Jun 6, 2005
493
0
Dark Side Of The Moon
I would say save the two different scripts as indicated above...

StartMail.scpt =
Code:
tell application "Mail" to activate

and StopMail.scpt =
Code:
tell application "Mail" to quit

then set up a pair of repeating events in iCal to run them.

I tested, and you can even make a new Calendar (checkboxes on left) for this, then turn off the checkbox so it doesn't clutter up your calendar by showing the event every day, but it will still run.

For start:
Screen shot 2010-06-07 at 1.05.00 AM.png

Then create your opening event:
Screen shot 2010-06-07 at 1.20.41 AM.png

Now create your quit event:
Screen shot 2010-06-07 at 1.20.07 AM.png

Then hide your events:
Screen shot 2010-06-07 at 1.03.12 AM.png

I tested this (with times adjusted to a couple minutes apart) and it works, without iCal running.

It's just too bad that iCal doesn't have a repeat option for "Every Weekday". It really should.
 

brenm666

macrumors regular
Original poster
May 30, 2010
161
0
Well thanks Billy Boo Bob! It's exactly what I needed! And it works really well! :D
 

Billy Boo Bob

macrumors 6502
Jun 6, 2005
493
0
Dark Side Of The Moon
Well thanks Billy Boo Bob! It's exactly what I needed! And it works really well! :D
Yeah. It was fun to answer to myself a "Hmmm. I wonder if this would work?" idea.

I'll keep this one in the back pocket for some day when someone else needs something like it (that I'm with in person), and I'll whip it up in a few minutes and make them think that I'm like Wile E. Coyote... "Sooooper Genius". :cool:

Actually, this would make a neat little utility that I could build in SuperCard that would toss AppleScripts at iCal to set it up. Fill in the app you wanna run, start time and end time, and maybe actually make an option to have it run on weekdays only (or any one day if preferred). For weekdays it would require 5 copies of each event, with a pair on each day, and told to repeat "every week". If I remember right, though, iCal is quite scriptable, but it's kinda tricky getting things right.

On the other hand, I've been learning AppleScript Obj-C lately. Maybe a Cocoa app like this would be a good way to learn the Obj-C calendar routines with a clear objective in mind. Hmmmmm......
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.