0

Desired scenario is: Tasks are set to run every day at 2am, and user want to run the task every day at 6:00pm. He starts an application and change it.

I've successfully created an agent that runs a task every given calendar dates. But I need these dates to be configurable by the user at his will, using an application. Is it possible to, programmatically, create/start/update an agent/daemon to run under launchd? If yes, how can I do this?

Marcelo
  • 2,075
  • 5
  • 21
  • 38
  • 2
    Are you running into trouble with app sandboxing specifically? Because there are google results from years ago for exactly this ***without*** sandboxing and entitlements. In any event it's [fairly impossible with sandboxing](http://www.cocoabuilder.com/archive/cocoa/317446-sandboxing-and-calling-launchctl.html) – stevesliva Sep 11 '14 at 18:03
  • 1
    I wonder whether there is a way to get a notification to wake up at a specific time? You could then have your app activate in accessory mode and do the non-GUI scheduled task. I [wrote a bunch about accessory mode here](http://stackoverflow.com/questions/24088906/how-to-display-application-icon-in-menubar-even-application-is-quit/24094273#24094273). It would keep things easier for you to have one executable that runs both the GUI and the background task in a sandboxed environment. "Helper" apps are a pain if you want to use core data like you mentioned in another question. – stevesliva Sep 11 '14 at 18:13
  • Stevesliva, thank you very much for sharing your experiences. My case, putting specifically, is in need to run background tasks with dynamic/updatable parameters that comes from stored CoreData databases (saved in ~/Library/Application Support/[MyApp] directory). Right now, I've made a Command Line Tools project as the executed task. I'm looking on how agents/daemons works. I haven't figured out yet how to properly link the .plist files and the exec file, in a dynamic way. My current approach is trying to read/write the plist file within ~/Library/LauchAgents, but it ins't opening... – Marcelo Sep 11 '14 at 18:42
  • What's the actual problem? Is it that your program doesn't have write access to the ~/Library/LaunchAgents directory? – TheDarkKnight Sep 12 '14 at 08:29
  • Hi Merlin, thanks for replying. Just figured out how to put things working here. In the end, the real issue was me: I'm just starting to develop to OSX, and many things are yet unknown for me. I was putting the .list file into the system/root folder. – Marcelo Sep 12 '14 at 17:12

1 Answers1

0

Just figured out how to put thing working. I was trying to write the agent into the system/root, and haven't noted it.

I just created the .plist file in the logged user agent folder, using the following code:

// create the plist agent file
NSMutableDictionary *plist = [[NSMutableDictionary alloc] init];

// set values here...

// saves the agent plist file
NSString *userLaunchAgentsPath = [[NSString alloc] initWithFormat:@"%@%@%@", @"/Users/", NSUserName(), @"/Library/LaunchAgents/myagent.plist"];
[plist writeToFile:userLaunchAgentsPath atomically:YES];
Marcelo
  • 2,075
  • 5
  • 21
  • 38