Automate add filename to text document in Automator/AppleScript

Discussion in 'Mac Programming' started by Tardypigeon, Jan 1, 2014.

  1. Tardypigeon macrumors newbie

    Joined:
    Jan 1, 2014
    #1
    Hi there - first post from someone relatively new to AppleScript, just getting my head around Automator and wondered if there's a way of setting up a folder action which takes the newly-added files and gets the filename (not necessarily the full filename with extension) and adds it as a line of text (along with a date stamp) to a text document.

    I've got an Automator application called 'Get filename' but no idea how to turn that into a variable or put that variable into AppleScript format.

    Would anyone be able to help me devise a workflow to do this?

    I should explain why this would be useful: I'd like to have a record of when files were originally added to a folder so that if I later edit them I at least know what date particular files were first added.

    Thanks!
     
  2. Red Menace macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #2
    You don't mention what OS you are running, but currently items in the file system already have an kMDItemDateAdded attribute, for example:
    Code:
    set someItem to quoted form of POSIX path of (choose file)
    set addedDate to (do shell script "/usr/bin/mdls -name kMDItemDateAdded -raw " & someItem)
    display dialog "The file item " & someItem & " was added on " & addedDate buttons {"OK"} default button 1
     
  3. Tardypigeon thread starter macrumors newbie

    Joined:
    Jan 1, 2014
    #3
    Hi there RM

    Running 10.6.8. Tried popping that code into AppleScript but seemed to get an error. The message I get reads 'The file (name of file) was added on (null)'.

    I guess you're saying that the info is there, which is useful, but is there a way of grabbing it either in text form or in some kind of table?

    I just thought something like creating a workflow which automatically keeps track of when new files are added would be a really common procedure. Perhaps it's more complicated than I thought!

    Thanks

    Tardy

     
  4. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #4
    You already had something going, with a folder action right?

    You need to add a second action after "Get Selected Finder Items" that add the date and file name to a log file. You can pick either Applescript or Shell script I suppose. For shell script, to add the date and file name to a file called "Added Files.log" in your "Documents" folder, you can try this: Add the "Run Shell Script" action and select "As arguments" in the drop down menu in the upper left corner with the "Pass input:" label.

    Code:
    for f in "$@"
    do
            echo $(date)": $f" >> ~/Documents/Added\ Files.log
    done
    

    This file will grow forever, so if there are many files that are added, it can get big over time, so you might want to keep an eye on it.
     
  5. Red Menace macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #5
    The kMDItemDateAdded metadata item was added a couple of OS versions ago (10.7 Lion), so it is not available in Snow Leopard and earlier.
     
  6. Tardypigeon thread starter macrumors newbie

    Joined:
    Jan 1, 2014
    #6
    Thanks subsonix, that seems to have done it! There is a bit of code at the start of the output which I can probably prune away later, but that's not a problem. That's brilliant!

    Do you know if the code is recursive - if that's the right word; will it detect if folders below the first level have had files added to them or is that a different piece of code? It works for top level but doesn't seem to detect files added within folders on that level for me.

    Thanks again!

    Also, thanks Red Menace - typical I've missed out on the juicy coding!

    Tardy

    ----------

    I should add it doesn't much matter if it's recursive: I usually put files on that top level first anyway, so don't worry about it.

    Can you recommend any good books to get to grips with the sort of shell/Applescript coding that would be useful here? Reading from the screen is one thing but I could probably get a lot more from a book!

    Thanks

    Tardy
     

Share This Page