Help in creating batch file

Discussion in 'macOS High Sierra (10.13)' started by gogreen1, Feb 5, 2018.

  1. gogreen1 macrumors regular

    gogreen1

    Joined:
    Nov 20, 2017
    #1
    I’ve created two copy commands for use in Terminal. I’m just saving and backing up two files from two different directories. I’d like to place them in a batch file and run them from a desktop icon—I used to do this in Windows. I’m new to MacOS, and I’m a bit bewildered by the different instructions on doing this from Google searches. Can someone please tell me how to create a batch file with these two copy commands for use in MacOS High Sierra? Thanks.

    cp -av /Users/[UserName]/Library/Safari/Bookmarks.plist /Users/[UserName]/Documents/Backups

    cp -av /Users/[UserName]/Library/Group\ Containers/UBF8T346G9.Office/User\ Content.localized/Templates.localized/Normal.dotm /Users/[UserName]/Documents/Backups
     
  2. BrianBaughn macrumors 603

    BrianBaughn

    Joined:
    Feb 13, 2011
    Location:
    Baltimore, Maryland
    #2
    Search the web for how to create an application with the Automator app that runs Terminal commands via Applescript.
     
  3. neliason macrumors 6502

    Joined:
    Oct 1, 2015
    #3
    You can use Automator as suggested. Another thing you could do is create a script file, which is the macOS batch equivalent. Using Bash (the default) you'd just create a file with a '.sh' extension and add '#!/bin/bash' as the first line. This is telling the command line that this is a Bash script and use Bash to interpret it. You'd then put your copy commands on lines below this. You then have to make the .sh file executable. Then you can run the script from Terminal. I think you can have Automator run the script. That way you just change the script as your needs change.

    I'm thinking you want some sort of regular backup. You can register your Bash script to run automatically using a plist (XML file) you create and load with launchctl.
     
  4. cruisin macrumors 6502a

    cruisin

    Joined:
    Apr 1, 2014
    Location:
    Canada
    #4
    The quick way to to join them with && and copy/paste them into terminal, hitting [enter or return] afterwards:
    Code:
    cp -av /Users/[UserName]/Library/Safari/Bookmarks.plist /Users/[UserName]/Documents/Backups && cp -av /Users/[UserName]/Library/Group\ Containers/UBF8T346G9.Office/User\ Content.localized/Templates.localized/Normal.dotm /Users/[UserName]/Documents/Backups
    To make a equivalent .bat file you need to make a .command file with these contents:
    Code:
    #!/bin/bash
    cp -av /Users/[UserName]/Library/Safari/Bookmarks.plist /Users/[UserName]/Documents/Backups
    cp -av /Users/[UserName]/Library/Group\ Containers/UBF8T346G9.Office/User\ Content.localized/Templates.localized/Normal.dotm /Users/[UserName]/Documents/Backups
    The important step is that .command files are not executable by default (for security). So you need to open terminal, go to the to the directory with the .command file and make it executable. Type cd, a space, then the directory of the file (you can just drag the folder that contains your file into terminal, it will add the correct directory)
    Code:
    cd [path to the file]
    Then make it executable:
    Code:
    chmod +x [filename.command]
    If you want the terminal window to close after running, you need to add two lines to the end of the .command file:
    Code:
    osascript -e 'tell application "Terminal" to close (every window whose name contains ".command")' &
    exit
    You can also schedule it to run occasionally.
     
  5. gogreen1 thread starter macrumors regular

    gogreen1

    Joined:
    Nov 20, 2017
    #5
    Wow! You guys must have advanced black belts in MacOS. Thanks!
    One more question, if I may: Does this file need further tinkering to place it on my desktop and execute it by double-clicking?
    Thanks again (deep bow)!
     
  6. neliason macrumors 6502

    Joined:
    Oct 1, 2015
    #6
    If you follow what @cruisin said to do (make the .command file) you can double click on the file to execute it. The key step is the ‘chmod’ command. Once the file is executable then wherever you place it you should be able to execute it. The permission to execute stays with the file.

    I didn’t know about .command files and learned something myself.
     
  7. gogreen1 thread starter macrumors regular

    gogreen1

    Joined:
    Nov 20, 2017
  8. gogreen1, Feb 7, 2018
    Last edited: Feb 7, 2018

    gogreen1 thread starter macrumors regular

    gogreen1

    Joined:
    Nov 20, 2017
    #8
    I've run into a snag on this. I think I created the .command file correctly (I called it "Batchsaves"). It's trying to execute the commands. I tried copying items as text only, but that didn't fix the errors. I also tried re-entering the text manually. How do I correct the errors in the attached? :confused: Thanks. command error.png
     
  9. neliason macrumors 6502

    Joined:
    Oct 1, 2015
    #9
    Can you post the complete contents of your .command file or a screenshot of it?

    Did it copy anything?
     
  10. gogreen1 thread starter macrumors regular

    gogreen1

    Joined:
    Nov 20, 2017
    #10
    neliason: Here's a screenshot. It didn't copy anything. Thanks

    command file text.png
     
  11. neliason macrumors 6502

    Joined:
    Oct 1, 2015
    #11
    Is the 3rd line ‘Templates’ just wrapped and really part of the 2nd line?

    It looks like you are missing the ampersand ‘&’ at the end of the osascript line. I believe this is necessary to get that to work properly. There should be a space and then the ampersand.

    I’d fix the missing ampersand. If that doesn’t work then I’d start commenting out all the lines but the first ‘cp’. You comment with a pound symbol ‘#’ at the beginning of the line. If the first command works and copies data then uncomment the next line. Keep doing that until you find a line that causes an error.
     
  12. gogreen1 thread starter macrumors regular

    gogreen1

    Joined:
    Nov 20, 2017
    #12
    Thanks, neliason--yup, I missed the ampersand at the end of the osascript.

    I get errors in the two save commands no matter what. But when I enter them individually in Terminal, they work. What gives with that? I think it's odd that capturing the path to these files produces such a strange-looking path with unexpected spaces and slashes. Still, by themselves, they work in Terminal but not in the .command file.
     
  13. BrianBaughn macrumors 603

    BrianBaughn

    Joined:
    Feb 13, 2011
    Location:
    Baltimore, Maryland
    #13
    You'll get that script running, I'm sure, but you might be better off (like me) doing this sort of thing with Automator.
     
  14. neliason macrumors 6502

    Joined:
    Oct 1, 2015
    #14
    The slash escapes the space in a file or directory name. If the commands work when directly entered in a Terminal they should work in a script. The error message should point you to the problem. I’d comment out everything but the cp lines and see what the error is.
     
  15. gogreen1 thread starter macrumors regular

    gogreen1

    Joined:
    Nov 20, 2017
    #15
    Thanks, BrianBaughn. I'm totally lost when it comes to Automator--never used it before. For my .command desktop file, what type of document would I choose, and how would I then enter the copy commands?
     
  16. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #16
    Info: textedit-plain-text-mode
     
  17. gogreen1 thread starter macrumors regular

    gogreen1

    Joined:
    Nov 20, 2017
    #17
    Thanks, kryten2. For the moment, I've created a sync job in FreeFileSync for this task.
     

Share This Page

16 February 5, 2018