launchd help

Discussion in 'Mac Programming' started by Latch, Sep 27, 2012.

  1. Latch macrumors newbie

    Joined:
    Sep 27, 2012
    #1
    Hello, I am new to these forums, so I hope I don't do anything too stupid.

    I'll just give all the details of what I would like to do.

    I would like to run a shell script at 8:00AM every Monday, Wednesday, and Friday.

    What this shell script does is run a python script that automatically downloads the newest xkcd comic.

    If there is a more efficient way to do this, please let me know, but my primary concern is how to use launchd to set up this script scheduling.

    Thanks in advance!
     
  2. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #2
    You can execute a shell script from Applescript with the do shell script command. You can run an Applescript from iCal as an alarm.

    More info on launchctl and launchd.plist here :

    https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man1/launchctl.1.html#//apple_ref/doc/man/1/launchctl
    https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man5/launchd.plist.5.html#//apple_ref/doc/man/5/launchd.plist

    or in Terminal :

    Code:
    man launchctl
    man launchd.plist

    Can you post your python script? Thanks
     
  3. Latch thread starter macrumors newbie

    Joined:
    Sep 27, 2012
    #3
    The iCal -> applescript -> shell script -> python script is actually what I am using right now, and it works... I was just hoping for a more elegant solution.

    I attached a zip file. Unzip it into the folder where you want all of the comics to be downloaded to. It includes the .py file, the applescript, and the shell script. It also contains a file called _count.txt, which is used to store the last comic number the script downloaded. So, if you put a 1 in it, it will download comic 1 and keep going all the way up until the most recent comic. When it gets a 404 error, because it is trying to download a comic that Randall has not made yet, call it comic 1200, it will overwrite the contents of _count.txt, replacing the contents with 1200. Thus, when the script starts again, it will keep going where it last left off. Unless the script gets interrupted in the middle, in which case _count.txt will remain at whatever it was when the script was started.

    The reason the files all have _ in front of them is so that they are all grouped together at the beginning of the folder (assuming organized by name). Otherwise, they would get lost in the sea of 1000+ xkcd comics.

    Have fun! I hope my code is commented well enough you can sort of tell what it does!
     

    Attached Files:

  4. kryten2, Sep 28, 2012
    Last edited: Sep 28, 2012

    kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #4
    I made a launchagent you can use. Open it with TextEdit to see if anything regarding paths need to be changed. Copy this to your /Users/yourusername/Library/LaunchAgents folder. Give yourself read and write permissions on the plist file. In Finder-->Get Info and change the Sharing & Permissions at the bottom of the window. Load the plist file with launchctl. In Terminal :

    Code:
    launchctl load /Users/yourusername/Library/LaunchAgents/nameofplistfile.plist
    Tested by downloading the last 5 images.
     

    Attached Files:

Share This Page