Monday, July 22, 2019

#26 2019-06-13 10:49:31 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 234

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

JQ wrote:

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?



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


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#27 2019-06-14 05:16:10 am

JQ
Member
Registered: 2019-02-04
Posts: 37

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

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.

Offline

 

#28 2019-06-26 01:58:30 pm

JQ
Member
Registered: 2019-02-04
Posts: 37

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

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:

Applescript:


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:

Applescript:


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?

Offline

 

#29 2019-06-26 05:02:55 pm

JQ
Member
Registered: 2019-02-04
Posts: 37

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

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:

Applescript:


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

Offline

 

#30 2019-06-26 05:46:30 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5763

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

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


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#31 2019-06-27 06:49:49 am

ldicroce
Member
Registered: 2017-11-25
Posts: 120

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

JQ wrote:


Applescript:


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

I clearly don't have the shell script syntax correct



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

Applescript:

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

Online

 

#32 2019-07-09 11:32:07 am

JQ
Member
Registered: 2019-02-04
Posts: 37

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

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:

Applescript:


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?

Offline

 

#33 2019-07-09 11:47:08 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 234

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

TRY THIS:

Applescript:


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.

Last edited by KniazidisR (2019-07-10 12:08:33 am)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#34 2019-07-10 12:29:44 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5763

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

Applescript:

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


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#35 2019-07-10 02:31:09 pm

JQ
Member
Registered: 2019-02-04
Posts: 37

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

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:

Applescript:


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.

Offline

 

#36 2019-07-10 05:37:20 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5763

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

JQ wrote:

For some reason I can't get MetaDataLib to work on a server.



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


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#37 2019-07-11 08:07:59 am

JQ
Member
Registered: 2019-02-04
Posts: 37

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

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.

Offline

 

#38 2019-07-11 11:21:58 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 234

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

JQ wrote:


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



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

Last edited by KniazidisR (2019-07-11 11:38:09 am)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#39 2019-07-11 05:38:17 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5763

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

JQ wrote:

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



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

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.



Try it like this:

Applescript:

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

Last edited by Shane Stanley (2019-07-11 06:39:13 pm)


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#40 2019-07-18 07:13:15 pm

JQ
Member
Registered: 2019-02-04
Posts: 37

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

Thank you! 

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

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)