Applescript Challenge: Move video files that are > 30 days old

Hi again and thanks very much for the replies. You’ll have to help me a little more to put both of your replies together as I am a rookie when it comes to scripting. Where do I put in the source volumes/files/NEW
and the destination volumes/files/OLD

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

on moveFilesFrom:posixFolderPath toFolder:destPosixPath numberOfDaysOld:numDays
	-- get date limit
	set dateLimit to current application's NSDate's dateWithTimeIntervalSinceNow:-(numDays * days)
	set dateLimit to current application's NSCalendar's currentCalendar()'s startOfDayForDate:dateLimit
	-- make URLs of POSIX paths
	set destFolderURL to current application's |NSURL|'s fileURLWithPath:destPosixPath
	set sourceFolderURL to current application's |NSURL|'s fileURLWithPath:posixFolderPath
	-- get file manager
	set theNSFileManager to current application's NSFileManager's defaultManager()
	-- get contents of directory, ignoring invisible items
	set theURLs to theNSFileManager's contentsOfDirectoryAtURL:sourceFolderURL includingPropertiesForKeys:{} options:(current application's NSDirectoryEnumerationSkipsHiddenFiles) |error|:(missing value)
	-- loop through URLs
	repeat with aURL in theURLs
		-- get date added
		set {theResult, theDate} to (aURL's getResourceValue:(reference) forKey:(current application's NSURLAddedToDirectoryDateKey) |error|:(missing value))
		-- test date
		if theResult as boolean and (theDate's compare:dateLimit) as integer < 0 then
			-- get name of file
			set theName to aURL's lastPathComponent()
			-- make new URL
			set destURL to (destFolderURL's URLByAppendingPathComponent:theName)
			-- move file
			(theNSFileManager's moveItemAtURL:aURL toURL:destURL |error|:(missing value))
		end if
	end repeat
end moveFilesFrom:toFolder:numberOfDaysOld:

#=====

# Define your own paths here
set posixFolderPath to "volumes/files/NEW"
set destPosixPath to "volumes/files/OLD"

my moveFilesFrom:posixFolderPath toFolder:destPosixPath numberOfDaysOld:30

Yvan KOENIG running El Capitan 10.11.6 in French (VALLAURIS, France) lundi 8 aout 2016 16:45:28

Hi again, thank you very much Yvan, this script worked very well for what I wanted to do. Also thanks to Shane Stanley for his input.

Thanks, Mike.

Thanks for the feedback.

The main component of the script was written by Shane STANLEY !

Yvan KOENIG running El Capitan 10.11.6 in French (VALLAURIS, France) mercredi 10 aout 2016 16:16:30

Hi again, I hope this will be my last question. The computer I will be using this script on has very little user interaction so I’m trying to make everything automatic. I have found that this script below does not give any indication if the file already exists in the destination folder. What would the code be and where would it be added, to move the file to trash if it already exists?

Then display a dialog box such as this only if it was moved to the trash:

tell application "Finder"
		 display dialog "File Already Exists" {"OK", "No"} default button 1 with title "Move to Trash?" giving up after 60
				if button returned of result is "OK" or gave up of result then
				empty trash
                          end tell

Thank you for your help.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

on moveFilesFrom:posixFolderPath toFolder:destPosixPath numberOfDaysOld:numDays
   -- get date limit
   set dateLimit to current application's NSDate's dateWithTimeIntervalSinceNow:-(numDays * days)
   set dateLimit to current application's NSCalendar's currentCalendar()'s startOfDayForDate:dateLimit
   -- make URLs of POSIX paths
   set destFolderURL to current application's |NSURL|'s fileURLWithPath:destPosixPath
   set sourceFolderURL to current application's |NSURL|'s fileURLWithPath:posixFolderPath
   -- get file manager
   set theNSFileManager to current application's NSFileManager's defaultManager()
   -- get contents of directory, ignoring invisible items
   set theURLs to theNSFileManager's contentsOfDirectoryAtURL:sourceFolderURL includingPropertiesForKeys:{} options:(current application's NSDirectoryEnumerationSkipsHiddenFiles) |error|:(missing value)
   -- loop through URLs
   repeat with aURL in theURLs
       -- get date added
       set {theResult, theDate} to (aURL's getResourceValue:(reference) forKey:(current application's NSURLAddedToDirectoryDateKey) |error|:(missing value))
       -- test date
       if theResult as boolean and (theDate's compare:dateLimit) as integer < 0 then
           -- get name of file
           set theName to aURL's lastPathComponent()
           -- make new URL
           set destURL to (destFolderURL's URLByAppendingPathComponent:theName)
           -- move file
           (theNSFileManager's moveItemAtURL:aURL toURL:destURL |error|:(missing value))
       end if
   end repeat
end moveFilesFrom:toFolder:numberOfDaysOld:

#=====

# Define your own paths here
set posixFolderPath to "volumes/files/NEW"
set destPosixPath to "volumes/files/OLD"

my moveFilesFrom:posixFolderPath toFolder:destPosixPath numberOfDaysOld:30

You check if it already exists like this:

if (destURL's checkResourceIsReachableAndReturnError:(missing value)) as boolean then

And you move to the trash like this:

theNSFileManager's trashItemAtURL: destURL resultingItemURL:(missing value) |error|:(missing value)

Edited to make it delete the correct file.

As the asker used “empty trash” he may be interested by an other instruction which delete the file without passing thru the trash :

theNSFileManager's removeItemAtURL:aURL |error|:(missing value)

Yvan KOENIG running El Capitan 10.11.6 in French (VALLAURIS, France) vendredi 12 aout 2016 12:37:21

Sorry for bumping the old post, but I think this is script will do what I’m looking for if I can figure out one issue.

When I tested it on my desktop computer it worked perfectly and moved the correct files. I’m now trying to test it on folders on my server and I’m getting the following error:

Script Error
missing value doesn’t understand the “compare_” message.

Is this error related to me trying to use it on folders that are on a server? (can it not tell how long a file has been in a folder if that folder is located on a server?)

I never stored files on servers so I have no idea of the way they are treated.
You may try to edit the instruction:

if theResult as boolean and (theDate's compare:dateLimit) as integer < 0 then

as

if theResult as boolean and (theDate as text is not "missing value") and (theDate's compare:dateLimit) as integer < 0 then

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 13 juin 2019 19:45:58

Thanks Yvan.

I don’t get the error anymore, but it does not move any files regardless of the value I enter for number of days old.

Curious if this means that the script is unable to tell how long a file has been in a folder if it’s not housed on the HD of the computer running the script?

Yes, it appears that the instruction supposed to return the date returns in fact missing value.

With a bit of luck Shane STANLEY will see this thread and will be able to help you.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 13 juin 2019 21:11:42

It’s possible the volume doesn’t support NSURLAddedToDirectoryDateKey. The documentation for it says:

Note that inconsistent behavior may be observed when this attribute is requested on hard-linked items. This property is not supported by all volumes.

What I’m trying to do is create a script that searches a root folder and all subfolders for folders/files that have existed in the root folder for longer than 2 years. Shane, your script from this thread did exactly what I was looking for, but unfortunately it looks like it might not work on my system. So I was trying to come up with a different way to do it.

I was looking around at file metadata in Terminal, and this is the metadata I think I need:

kMDItemDateAdded

I’ve tested it on my server, and I can pull up this metadata information through Terminal. Is there a way to run a shell script that uses the Terminal to pull that information? In Terminal I’m using “mdls -name kMDItemDateAdded [drag & drop file name]” to get the information.

I thought this would work to get the Date Added information:


set theFile to (choose file)
set DateAdded to do shell script "mdls -name kMDItemDateAdded " & theFile
return DateAdded

But I’m just starting to learn how to use shell scripts and I clearly don’t have the shell script syntax correct. (Is a shell script even how you would do this? or a tell block using Terminal?)

Is this no longer related to this original post? (ie. should I start a new thread?)

Thanks.

If you’re happy to rely on the Spotlight metadata, my Metadata Lib will do what you want:

https://www.macosxautomation.com/applescript/apps/Script_Libs.html#Metadata_Lib

This should get you started:

use script "Metadata Lib" version "2.0.1"
use scripting additions

set theFolder to choose folder
set dateLimit to (current date) - (2 * 365 * days)
set thePaths to perform search in folders {theFolder} predicate string "kMDItemDateAdded < %@" search arguments {dateLimit}

Thanks Shane, I will start with this.

Just one question, is using Spotlight Metadata not the best way to accomplish this?

I should clarify, originally I was going to use a script that just went by Date Modified/Date Created as listed in Finder.

However sometimes I receive files that, while older than 2 years, are new to me. I thought this would be the best way to distinguish between the files I’ve had for more than 2 years and the files that were created more than 2 years ago but are new to me. Does that make sense?

Spotlight metadata isn’t guaranteed to be accurate or up-to-date – although I have to say that these days reports of problems with it seem to have just about dried up.

So what is the problem? All files in your file system except Date Created and Date Modified have Date Added property

I deal primarily in video production. The problem that I run into is I will receive a consolidated Adobe After Effects project that has been created recently, but within that project are video files that may be a few years old.

If I were to use a delete script going solely by date created or date modified, it’s possible I may delete a reference file that is needed for one of those After Effects Projects.

That’s why I thought Date Added might be the safest way to go in my particular situation.

I’m still having trouble with this script. I can’t figure out why I’m unable to read metadata off of a volume.

Starting with Shane’s suggested script:


use script "Metadata Lib" version "2.0.1"
use scripting additions

set theFolder to choose folder
set dateLimit to (current date) - (1 * days)
set thePaths to perform search in folders {theFolder} predicate string "kMDItemDateAdded < %@" search arguments {dateLimit}
set FileList to thePaths as list
set textdoc to ""
repeat with i from 1 to count of FileList
	set theFile to item i of FileList as text
	set textdoc to textdoc & theFile & return
end repeat

tell application "TextEdit"
	activate
	set newDoc to (make new document with properties {text:(textdoc as text)})
end tell

This works perfectly when selecting a folder on my desktop. If I try to use it on a folder that exists on a mounted volume it returns no results.

I’ve even tried a very basic terminal shellscript:


set thefile to (choose file)

set dateadded to (do shell script "mdls " & quoted form of POSIX path of thefile & " -name kMDItemDateAdded")

return dateadded & return & thefile

I have the same issue with this script. Files on my desktop yield the kMDItemDateAdded information I’m looking for. Files on a mounted volume do not. The above script will return the file name information, just not the metadata.

Where I get confused is if I manually use Terminal to get the data from a file on the mounted volume, it works fine.

Am I missing something?

I sort of answered my own question. It turns out it’s a permission issue with how our server is set up. I’m able to get the metadata when using a non-fibre optic connection.

Unfortunately Shane’s search script also doesn’t work, most likely for the same permissions reason.

So what I need to do is use my barebones script:


set thefile to (choose file)

set dateadded to (do shell script "mdls " & quoted form of POSIX path of thefile & " -name kMDItemDateAdded")

return dateadded & return & thefile

And build it up so that it searches a folder and all it’s subfolders, and compares the kMDItemDateAdded to a date that is 2 years old. I’ve seen scripts here for searching folders/subfolders, but what would be the best way to compare the date information?

Browser: Safari 537.36
Operating System: macOS 10.14