newbie: scheduling FTP actions

Discussion in 'Mac Programming' started by bunkre, Apr 22, 2005.

  1. macrumors member

    Joined:
    Mar 10, 2004
    #1
    (I'm really new to applescript)

    How hard of a task would it be to write an applescript that will go and grab specific folders from a server via FTP, or even better, schedule it to grab them every week or so? Am I better off scripting an app like Transmit?

    My brother is a unix/linux preacher and wants be to us wget, but I don't know **** about command line.
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    wget is not good for ftp! It's really for http. I would personally recommend using the command line ftp client. Write a shell script to do whatever you want then schedule with cron.

    A basic ftp script would look something like
    Code:
    #!/bin/sh
    
    ftp servername_or_ip << FTP_EOF
    user username password
    cd directory_name
    get file
    FTP_EOF
    
    Replace servername_or_ip, username, password, directory_name and file with the required stuff. If you want to get whole directories (this assumes you have the correct local directories) you need something like

    Code:
    #!/bin/sh
    
    ftp servername_or_ip << FTP_EOF
    user username password
    cd directory_name
    lcd directory_name
    prompt
    mget *
    FTP_EOF
    
    lcd changes your local directory, prompt turns off prompting, mget is multiple get.

    Cron is pretty easy to setup. Do a Google search for that. Save the script somewhere and make it executable (chmod u+x <scriptname>). You may want to add a line to the start of the script (above the ftp) to change the local working directory.

    The reason I think this is better is that you don't need to buy Transmit! I don't think AppleScript can do FTP on it's own. If you give me some more details I can write a better script for you.

    Edit: Been playing. This is better.
    Code:
    #!/bin/sh
     
    FTP_USER=<USERNAME>
    FTP_PASS=<PASSWORD>
    FTP_HOST=<HOSTNAME>
     
    DIRS='<LIST_OF FILES>'
     
    LOCAL_BASE=<LOCAL_DIR>
     
    export FTP_USER FTP_PASS FTP_HOST DIRS LOCAL_BASE
     
    mkdir -p $LOCAL_BASE
    cd $LOCAL_BASE
     
    for dir in $DIRS ;\
    do \
    (\
    ftp $FTP_HOST << FTP_EOF\
    user $FTP_USER $FTP_PASS\
    cd $dir\
    lcd $dir\
    prompt\
    mget *\
    FTP_EOF\
    )\
    ;\
    done                              
    
    If you replace the variables (contained in <> above) with the correct details (the list should be space separated, i.e. 'dir1 dir2 dir3') then this will ftp to the given host and copy each of the directories specified to the directory specified in LOCAL_BASE. Note that unless LOCAL_BASE is absolute it will be relative to the current directory. So if your user is called fred setting LOCAL_BASE /Users/fred/Desktop would result in a set of directories on freds desktop.
     

Share This Page