Help with simple script

Discussion in 'Mac Programming' started by gpchess2k, Oct 12, 2015.

  1. gpchess2k macrumors newbie

    gpchess2k

    Joined:
    Oct 12, 2015
    #1
    Hey guy,

    Looking for ANY help on creating a simple solution to automate a process. I have a copy of the /etc/cups folder that I copy into multiple computers on a weekly basis and would like to know if there is a quicker way of doing this using some kind of script. Where I can double click from the USB drive and have the contents copied from a specific folder on the USB over to etc/cups.
     
  2. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #2
    Hi,

    You could use an AppleScript saved as an Application, to make it nice and clickable...

    Paste this into Script Editor:

    Code:
    set theSourceFolder to "/path/to/location/on/the/USB drive/"
    set theDestFolder to "/path/to/location/on/the/mac/"
    do shell script "rsync -avz --delete " & quoted form of theSourceFolder & " " & quoted form of theDestFolder with administrator privileges
    
    Read up a bit about rsync before you run it though! You may want to omit the "--delete" bit - that deletes any file in the destination folder that is *not* in the source folder.

    Hope that helps.
     
  3. gpchess2k thread starter macrumors newbie

    gpchess2k

    Joined:
    Oct 12, 2015
    #3
    Thank you! What command could I use for source folder without having to put and actual path? For example the folder where the script is located. Helpful if I have multiple drives and folders change.
     
  4. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #4
    Hi,

    Assuming you know the path to the source folder relative to your script then could use something like this:

    Code:
    set thePathToTheSourceFolder to (POSIX path of (path to me)) & "mysourcefolder/"
    
    ...that assumes that your source folder is in the same folder as the script, and is called "mysourcefolder". Note that if you run this in Script Editor then it will, slightly confusingly, return the path to Script Editor. Once you save it as an app, you'll get the path to the running script.
     
  5. gpchess2k thread starter macrumors newbie

    gpchess2k

    Joined:
    Oct 12, 2015
    #5
    Eeeek. Close. Get this error:

    rsync: link_stat "/Volumes/WININSTALL/AtlantaPrinters/Test1.app/printers/." failed: No such file or directory (2)
    rsync error: some files could not be transferred (code 23) at /SourceCache/rsync/rsync-45/rsync/main.c(992) [sender=2.6.9]

    Script:
    set thePathToTheSourceFolder to (POSIX path of (path to me)) & "printers/"

    set theDestFolder to "/Users/ypanasyuk/Desktop/Atlanta Printers/"

    do shell script "rsync -avz " & quoted form of thePathToTheSourceFolder & " " & quoted form of theDestFolder with administrator privileges
     
  6. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #6
    Hmmmm... not sure about that one. Obviously the error is coming from rsync and it's trying to copy a file that isn't there. Why that would be, I don't know.

    I notice the USB stick is called WININSTALL. Is it Windows formatted rather than Mac? I'll admit that I'm guessing here, but if so then that *might* be part of the problem.
     
  7. chrfr macrumors 603

    Joined:
    Jul 11, 2009
    #7
    What are you actually trying to accomplish by copying the cups folder? I think you're likely going about whatever you're trying to do in an incorrect way.
     
  8. gpchess2k thread starter macrumors newbie

    gpchess2k

    Joined:
    Oct 12, 2015
    #8
    Its EXFAT. But just tested with Mac Extended and same error. When i remove
    Code:
    & "printers/"
    it copies the contents of the script.
     
  9. MacUser2525 macrumors 68000

    MacUser2525

    Joined:
    Mar 17, 2007
    Location:
    Canada
    #9
    This to me seems like the perfect candidate for a simple bash script, a cronjob and ssh copy in the bash script. Something like this in the script.

    Code:
    #!/bin/bash
    scp -r /path/to/copy/etc/cups/* user1@123.456.789.012:/path/to/machine1/printer/directory/
    scp -r /path/to/copy/etc/cups/* user2@123.456.789.012:/path/to/machine2/printer/directory/
    .....
    
    Now it would get tricky if the IP on the destination machines are always changing due DHCP leasing so may not be practical in that situation. A cron job entry example.

    Code:
    @weekly /path/to/cups/sync/script.sh
    
    Done by using crontab -e on the source machine this would have it run at midnight Sunday every week I do believe, you could change the weekly time to run to anything you want. You would use a day of the week and time of day entry for that. For the ssh part you would want to on the source machine use ssh-keygen -t rsa accepting the defaults not entering password at all when asked. This would allow you to do passwordless copy once you have taken the resulting ~/.ssh/id_pub.rsa file contents and placed it into the ~/.ssh/authorized_keys file on the destination machines. WINscp installed onto the windows machines should get you the ssh required for them although not certain on that as I have not used it in damn near twenty years now.

    Edit: A chmod +x /path/to/cups/sync/script.sh would be required to make it executable so it will run this done in a Terminal window.
     
  10. RcktMan77 macrumors member

    Joined:
    May 21, 2008
    Location:
    Fort Worth, TX
    #10
    The error appears because

    Code:
    set thePathToTheSourceFolder to (POSIX path of (path to me))
    returns the current path of the applescript/application. This POSIX path includes the script name itself (i.e. Test.app). I'm guessing the the "printers/" directory is not a sub-directory of Test.app, but resides as a sub-directory of the AtlantaPrinters parent; and therefore, rsync is unable to locate the source directory. You will either need to parse and strip the script's filename from the POSIX path that is returned by the aforementioned code snippet using applescript, or fix the path that you wish to use if it doesn't change (i.e. /Volumes/WININSTALL/AtlantaPrinters/).
     
  11. gpchess2k thread starter macrumors newbie

    gpchess2k

    Joined:
    Oct 12, 2015
    #11
    RcktMan77 that is exactly it. Thanks for your tip MacUser2525 but unfortunately it is setup DHCP. We use an imaging server so creating an AppleScript as an application would be the best solution for both newly imaged machines and machines who dont have printers on them currently.

    Here is the script that i used and like RcktMan77 said placing the printers within the script makes it a nice single package. Thanks Superscape!

    Code:
    set thePathToTheSourceFolder to (POSIX path of (path to me)) & "printers/"
    
    set theDestFolder to "/etc/cups"
    
    do shell script "rsync -avz --delete " & quoted form of thePathToTheSourceFolder & " " & quoted form of theDestFolder with administrator privileges
     

Share This Page