Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

This is my first Mac application.

I would like to send emails by comparing the current date and saved date of clients by the user in sqlite only one time- every day depending on the condition, asynchronously.

share|improve this question

closed as off topic by jtbandes, gentmatt, bmike Aug 14 '12 at 19:41

Questions on Ask Different are expected to relate to Apple hardware or software within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

2  
You should consider posting a message to Stack Overflow instead, this appears to be a programming question. –  Adam Davis Apr 11 '12 at 11:56
2  
If the app is just for personal use or limited distribution, this seems like a perfect job for a shell script run via cron. –  roosto Apr 11 '12 at 12:11
    
That seems tricky, since we do help with AppleScript and shell scripts here. But do really get some help, you may need to update your title to make more sense, and we will need a lot more information. I tried to update slightly, but your title is very confusing. –  jmlumpkin Apr 11 '12 at 12:33
1  
Welcome Lion :-) Your question will need you to list more of your research to be useful. What did you try to solve the problem? We do ask that questions about code not be asked here - so if you are looking for support in something compiled - comment and we can perhaps direct you to the programming sections of stack exchange. –  bmike Apr 11 '12 at 13:15

2 Answers 2

(Assuming you're using Objective-C/Cocoa)

The route I would take:

  1. Calculate the amount of time to the event

  2. Use performSelector:withObject:afterDelay: (available on NSObject)

  3. Include any additional additional logic (such as calls to other methods, or your email logic) in the method indicated in the above method.

share|improve this answer

Note: I'm assuming that by "asynchronous" you mean "do this whether or not the user is logged in and running the application at 9:30 am".

The right way to do this is to register a call with launchd that handles the appropriate work for your application. When and how you do this is somewhat flexible. If you're using a .pkg-type installation script you can register the helper on installation. Or you can register on first run of your application, perhaps after the user has provided sufficient configuration information for the helper app to do it's job.

The Daemons and Services guide delves in to how to do this in great detail. I recommend reading that. And for a description of plist properties that launchd looks at see this manual page.

As a very high-level example, let's assume your helper application can be found in your package under MyPackage.app/Contents/SharedSupport/bin/myhelper. Here's a plist file that would launch it every day at 9:30 am and 9:30 pm on the machine:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <!-- This label needs to be unique to the system -->
    <key>Label</key>
    <string>com.mycompany.mypackage.myhelper</string>

    <!-- Toggle to true to prevent it from running -->
    <key>Disabled</key>
    <false/>

    <!-- This is the full path to the helper application -->
    <key>Program</key>
    <string>/Applications/MyPackage.app/Contents/SharedSupport/bin/myhelper</string>

    <!-- This is an array of string arguments to pass in to the app when it's called -->
    <key>ProgramArguments</key>
    <array>
        <string>--daemon</string>
    </array>

    <!-- This works much like cron to state when the job should be run -->
    <key>StartCalendarInterval</key>
    <array>
        <!-- Run at 9:30 am -->
        <dict>
            <key>Minute</key>
            <integer>30></integer>
            <key>Hour</key>
            <integer>9</integer>
        </dict>
        <!-- Run at 9:30 pm -->
        <dict>
            <key>Minute</key>
            <integer>30></integer>
            <key>Hour</key>
            <integer>21</integer>
        </dict>
    <array>
</dict>
</plist>

Save that in /Library/LaunchAgents/com.mycompany.mypackage.myhelper.plist on your machine and then type:

sudo launchctl load /Library/LaunchAgents/com.mycompany.mypackage.myhelper.plist

To register it right away with launchd. On reboot it should register automatically. See the launchctl man page for more information on using it to interface with launchd.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.