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

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

Metadata search results are inherently recursive. It sounds like you just need to sort out the permissions problems.

This should be the correct syntax, or just use Metadata Lib from Shane …

set theFile to (quoted form of POSIX path of (choose file))
set DateAdded to do shell script "mdls -name kMDItemDateAdded " & theFile
return DateAdded

I’ve finally been able to figure out a work around for the permissions issues I ran into.

This is the script that I’ve been able to get to work:


set filelist to (do shell script "mdfind -onlyin /volumes/servername 'kMDItemDateAdded<=$time.today(-730)'")

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

This creates a list of all the files/folders that meet the criteria. Is there a way to filter the list so that it does not include folders?

My end goal is to use this script to do a batch delete, but I don’t want to delete a folder that meets my search criteria if it contains an item that does not meet my search criteria.

I was thinking I’d do the file delete first, then run another script that searches for empty folders and deletes them. Is this possible?

TRY THIS:


set server_address to choose URL showing {File servers} with editable URL
mount volume server_address

set theFolder to quoted form of POSIX path of ((choose folder) as text) --choose here server volume 
set _30daysAgo to -30 * days

do shell script "mdfind -onlyin " & theFolder & " 'kMDItemDateAdded>$time.now(" & _30daysAgo & ") && ! kMDItemContentType == public.folder'"

NOTE 1) I correctly understood that this shell command finds files only in a folder, but not in its subfolders. If this is the case, then the script must be converted into a recursive form.

NOTE 2) I correctly understood that this shell command cannot work with file names that contain non-ASCII characters? If so, it is best to return to the approach proposed by Shane Stanley.

use script "Metadata Lib" version "2.0.2"
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 > %@ AND kMDItemContentType != %@" search arguments {dateLimit, "public.folder"}
set {saveTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {linefeed}}
set thePaths to thePaths as text
set AppleScript's text item delimiters to saveTID
tell application "TextEdit"
	activate
	set newDoc to (make new document with properties {text:thePaths})
end tell

thank you Shane! I’ve finally been able to get it to work. For some reason I can’t get MetaDataLib to work on a server. I have to use the mdfind shell script or I don’t get any results when I search a folder on our server.

I’m now trying to add the step where I move the files returned from the search to a holding folder.

This is what I have so far:


set itemlist to (do shell script "mdfind -onlyin /Volumes/server/ 'kMDItemDateAdded<=$time.today(-730)&& kMDItemContentType!=\"public.folder\"'")

--removed line creating text file

repeat with i from 1 to count of itemlist
	set theitem to item i of itemlist
	set filename to do shell script "basename " & quoted form of POSIX path of theitem
	set theoldpath to quoted form of the POSIX path of theitem
	set thenewpath to quoted form of ("/Volumes/server/To_Be_Deleted/" & filename)
	do shell script "mv " & theoldpath & " " & thenewpath
end repeat

I’ve used this repeat loop in scripts before to copy/paste files and it’s worked. But it looks like “item i” is what I thought it should be. My previous scripts using this type of repeat loop usually involve me dropping files onto an application, not a search in terminal. If I add “as list” to the end of my set itemlist line item i becomes the entire list.

Is there a way to move all the files that come back from the shell script search at once? I’ve been reading about adding something like “| xargs” but I feel like I’m in over my head on this one. I don’t really understand it.

Given that it uses the same underlying approach as mdfind, I find that hard to fathom. What version of macOS are you running?

El Capitan 10.11.5.

I wish I could explain it better. I don’t know what kind of server it is or how it was set up, I only know the address because that’s where I was told to store my files.

I was able to get your 2.0.2 script to work, but the search returned more files that it should’ve.

The way I was able to get the 2.0.2 script to work was by removing the “choose folder” step and just hard coding the server address into it. It looks like if I use any variables the search doesn’t work right (for reasons that I can’t understand or explain unfortunately). Which is why I’ve been basically trying to reverse engineer the scripts that you’ve helping me with.

Variables have nothing to do with it. If you read the posts carefully, you would understand that your server has subfolders in addition to the main folder. Your script only searches the video in the main folder, and the Shane Stanley’s script also recursively searches the subfolders of the main folder. Therefore, there are more files as a result.

Yes, your “onlyin” script 1) finds files resided only in main folder 2) with my videos with Russian names it fails, and generally with no ASCII names and with special characters in the names

Ah, I missed the -onlyin bit. See below.

Try it like this:

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

set theFolder to POSIX path of (choose folder)
set dateLimit to (current date) - (2 * 365 * days)
set thePaths to perform search just in theFolder predicate string "kMDItemDateAdded < %@ AND kMDItemContentType != %@" search arguments {dateLimit, "public.folder"}
set {saveTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {linefeed}}
set thePaths to thePaths as text
set AppleScript's text item delimiters to saveTID
tell application "TextEdit"
	activate
	set newDoc to (make new document with properties {text:thePaths})
end tell

Thank you!

Sorry I wasn’t able to explain myself better, I’m still pretty new at this.