Applescript for for matching creation date on two files

Discussion in 'Mac Programming' started by Errabundo, Aug 15, 2012.

  1. Errabundo macrumors newbie

    Joined:
    Aug 15, 2012
    #1
    Hello,

    I have converted almost 1,000 video files from MPG1 to MOV in order importing them to Final Cut Pro (FCP can't import directly MPG1!).

    So I have TWO folders:
    Folder 1 with the original VideoXXXX.mpg1 files
    Folder 2 the converted VideoXXXX.mov files.

    The file names on both folders are the same. And the number of files in the two folders are the same.

    My problem is that the creation date of files in the Folder 2 is the "conversion" time (July 2012) instead the original creation time (from 2005 to 2010).

    When imported to FCP, I need the creation time. The name doesn't include the time, so I don't know the file date looking only at the file name.

    I need an Apple Script for changing the creation date to the files in Folder 2 files matching the creation date on the Folder 1 files.

    - For file n on Folder 1, take name and creation date
    - Look for the same file name on Folder 2
    - Set creation date of file on Folder 2 to Creation date of original file

    I have seen that it can be made with the "touch" command, but I'm new to applescripting and any help would be very appreciated!!

    Thanks
     
  2. kryten2, Aug 15, 2012
    Last edited: Aug 15, 2012

    kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #2
    Open Terminal. Type in the following line :

    Code:
    nano touchtestscript
    press enter. You should have something similar like the first tumbnail.

    paste the following code in the Terminal window. You should have something similar like the second tumbnail.

    Code:
    #!/bin/bash
    # change source and destination to location of your mpg and mov folder
    # Script tested with spaces in foldername and in filenames!
    source="/Users/test/Desktop/Movies mpg"
    destination="/Users/test/Desktop/Movies mov"
    fileslist=`ls "$source"`
    SAVEIFS=$IFS
    IFS=$'\n'
    for file in $fileslist;do
    echo "trying $file"
    filename="${file%.*}"
    touch -r "$source/$file" "$destination/$filename.mov" && echo "touched $filename.mov"
    done
    IFS=$SAVEIFS
    echo "Done"
    
    press ctrl and x. At the bottom you will see SAVE MODIFIED BUFFER? Type Y.
    At the bottom you will see File Name to Write : touchtestscript. Press enter.

    Run the script in Terminal(don't forget to change the source and destination paths!) :

    Code:
    sh touchtestscript
    To be on the safe side test it with a copy of a couple of files.
    A test run on 1000 files took about 20 seconds.
     

    Attached Files:

  3. Errabundo thread starter macrumors newbie

    Joined:
    Aug 15, 2012
    #3
    Thank you very much.

    I appreciate a lot your help and i'll check it carefully, as I have never used that terminal way.

    As I had no response and was a little worried, I posted the same question at the Apple discussion forum, and after some hours of work, I got a solution.

    https://discussions.apple.com/thread/4210192?start=0&tstart=0

    Your solution is very simple compared to the final one I used!

    Thank you
     
  4. tinydot macrumors newbie

    Joined:
    Aug 21, 2016
    #4
    How can I modify the script to copy the "creation_time" too? I realised the Photos app sort my videos through the "creation_time" instead of the file attribute, and the creation_time still remain the same as the encoded time.
     
  5. idealcheese macrumors regular

    Joined:
    Sep 18, 2014
    #5
    Hello, I found this old thread while searching for a solution to a problem similar to the OP's, but in my case I need to change the dates of rtf files to match the dates of the txt files with identical names. I modified the script above accordingly, but I get
    rtf”: command not found
    for lines 5 and 20. Anyone able to help me sort this out?
    P.S. Is there a way to also modify this script to grab creation dates from files without extensions? (I have a lot of old text documents from the 90s that are lacking extensions.)
    TIA!
     
  6. chown33, Sep 26, 2018
    Last edited: Sep 26, 2018

    chown33 macrumors 604

    Joined:
    Aug 9, 2009
    Location:
    Sailing beyond the sunset
    #6
    Turn off smart quotes or smart pasting.

    The fragment you pasted contains a curly right quote. Here it is enlarged:

    rtf”: command not found


    It should be a neutral double-quote, which looks like this enlarged:

    rtf": command not found

    Whatever you did when you modified the script, you either typed in a curly quote, or Smart Quotes converted a neutral quote to a curly quote, or Smart Paste converted a pasted neutral quote to a curly quote.


    Without seeing your entire script, my guess is there will be some curly left quotes in there, too. If you're not sure, use a larger font size, like 24 points, or paste the entire script into a post and use the CODE tags.

    EDIT

    There may be something else amiss, because the original script only has 15 lines. There is no line 20. So if you're seeing 20 lines, then you should post your modified script, using CODE tags.
     
  7. idealcheese macrumors regular

    Joined:
    Sep 18, 2014
    #7
    Thank you, you're brilliant!!! That did work to fix the command. However, the script still isn't working. It's returning the error:
    "Users/alison/Dropbox/Writing/1996a txt: No such file or directory"

    Here are the folder path lines in the script:
    source="⁨Users⁩/⁨alison⁩/Dropbox⁩/Writing⁩/1996a txt"⁨
    destination="Users⁩/⁨alison⁩/Dropbox⁩/Writing⁩/1996a rtf"⁨

    The path is correct, and the folder contains pairs of documents with the same filename in both txt and rtf versions. Terminal is not returning an error for the rtf line though. Why is it giving me such a hard time?

    I'm kind of making stabs in the dark here, as I have almost no experience with Terminal or programming language. Somehow I thought this would be simpler than it is.

    Thanks in advance for any input!
     
  8. chown33, Sep 26, 2018
    Last edited: Sep 26, 2018

    chown33 macrumors 604

    Joined:
    Aug 9, 2009
    Location:
    Sailing beyond the sunset
    #8
    The path it's complaining about needs to start with a /. Yours doesn't.

    It should be this (I assume):
    "/Users/alison/Dropbox/Writing/1996a txt"
    The original script has the starting /, so you apparently deleted it somehow in your modifications.

    Details are important in programming.

    If you continue to have problems, I suggest that you post your complete modified script, and use CODE tags. That way we can see the entire problem laid out, rather than having to guess about unseen parts.

    EDIT

    I fixed a typo in the corrected path.
     
  9. idealcheese macrumors regular

    Joined:
    Sep 18, 2014
    #9
    Thanks again! I added the forward slash and it still didn't work. Lots of hair pulling ensued, until I finally realized that invisible space had inserted themselves into the text that were not visible in TextEdit but were being pasted into Terminal. My guess is that because I copied the folder path from the Get Info window in Finder, where rather than slashes there are triangular characters, these invisible characters sneaked in there.

    Anyway, once I weeded them out, the script worked!! I'm over the moon, thanks so much.
     
  10. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    Location:
    Sailing beyond the sunset
    #10
    Glad you got it working.

    If you'd said that you copied from a Get Info window, I would have known that was where the unusual characters came from. I can see them as "no glyph" boxes in post #7, and I even tried decoding them, but it was unclear where they came from. Details are important, not just details of an individual script, but details of where data comes from. I mention this not just for your future reference, but for others who may see this thread in the future.
     
  11. idealcheese macrumors regular

    Joined:
    Sep 18, 2014
    #11
    Indeed, too bad I had to learn this the hard way! Incidentally, I was surprised to find that there is (apparently) no way to copy a file's path on MacOS. I found an old tip somewhere about copying the path from the Get Info window, but the screenshot showing the path (with slashes) must have been from a previous version of MacOS. Too bad Apple had to "innovate" by adding those dumb glyphs.
     
  12. chown33, Sep 26, 2018
    Last edited: Sep 26, 2018

    chown33 macrumors 604

    Joined:
    Aug 9, 2009
    Location:
    Sailing beyond the sunset
    #12
    In Terminal, you can drag a file or folder over a window and drop it. It will expand to the full pathname, with \ escapes. Terminal has always accepted dropped files like this.

    I think this works when dropped on a TextEdit.app window, too. Try it, see what happens. In Terminal windows, you'll need to type in a leading space if there's a partial command-line you're trying to prefix the pathname with.

    In some cases, if you did a Copy in Finder, pasting into a TextEdit document or a Terminal window will paste the entire pathname. I find this to be unpredictable, because sometimes it only pastes the leaf name (i.e. the final part of the pathname), while other times it pastes the whole pathname.

    Doing a quick test just now, Terminal windows seem to paste the \ escaped pathname, while TextEdit windows pastes only the leaf name. Again, try it and see what happens. You can backspace to erase what's dropped, or press ctrl-C or ⌘. (cmd-dot) to discard the entire line (in Terminal). In a TextEdit window, ⌘Z Undo suffices.
     

Share This Page