Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Tardypigeon

macrumors newbie
Original poster
Jan 1, 2014
6
0
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!
 
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
 
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

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
 
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!

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.
 
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)'.

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.
 
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.