Monday, December 9, 2019

#1 2016-08-06 01:47:44 pm

1994firehawk
Member
Registered: 2016-08-06
Posts: 23

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

Is it possible to write an applescript that after 30 days, video files that were copied into my "new" folder
would then be moved into my "old" folder??

My folder locations : volumes/files/NEW       and         volumes/files/OLD.

The "modified date" doesn't change by copying the file so I can't use that. Also I don't want to have to open the file before or after copying to use the "last opened" date.
Thanks in advance for any help!!

Offline

 

#2 2016-08-07 01:49:40 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3670

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

You may use :

Applescript:

# Define your own paths here
set sourceFolder to "volumes/files/NEW"
set destFolder to "volumes/files/OLD"

# paths used here for tests
set sourceFolder to POSIX path of ((path to desktop as text) & "NEW")
set destFolder to POSIX path of ((path to desktop as text) & "OLD")

set limit to (current date) - 30 * days
set limit to date (short date string of limit) # drops the time component -- ADDED
tell application "System Events"
   tell folder sourceFolder
       set needMove to path of every file whose modification date < limit
   end tell
end tell
set quotedDest to quoted form of destFolder
repeat with aPath in needMove
   do shell script "mv " & quoted form of POSIX path of aPath & space & quotedDest
end repeat

Yvan KOENIG running El Capitan 10.11.6 in French (VALLAURIS, France) dimanche 7 aout 2016 09:49:36

Last edited by Yvan Koenig (2016-08-07 01:54:21 am)

Offline

 

#3 2016-08-07 09:50:45 am

1994firehawk
Member
Registered: 2016-08-06
Posts: 23

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

Hi, thanks for the reply. In your script, you are using the "modification date" which doesn't change when a file is copied, so when I run the script the video is moved immediately because the date is usually months/years prior. I want the file to remain in the "new" folder for 30 days and then be moved to the "old" folder.

Thanks, Mike.

Offline

 

#4 2016-08-07 02:27:46 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3670

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

As is, the script move every files which weren't modified during the late 30 days.
It's the way I understood your question.

You may replace modification date by creation date if it fits your real needs.

We may also filter according to date added but doing that requires ASObjC.

I will be back tomorrow.


Yvan KOENIG running El Capitan 10.11.6 in French (VALLAURIS, France) dimanche 7 aout 2016 22:27:41

Offline

 

#5 2016-08-07 10:04:14 pm

1994firehawk
Member
Registered: 2016-08-06
Posts: 23

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

Hi, I think you may not understand what I'm trying to do.  What I want is, when I copy a video file on 8/8/2016 into my "volumes/files/NEW", I want it to remain there until 9/8/2016 regardless of the file creation date, date modified, or last opened date.

I want to use the copy date (8/8/2016) to start the timer to move it to "volumes/files/OLD" after 30 days.

Could this be done by applescript to create a log file with the filename and date copied and then have the script use the name and date to start the 30 day timer? I've been searching to write a script for that, with no luck.

Or you said it could be filtered according to date added but doing that requires ASObjC.

I hope this clarifies what I'm trying to do.

Thanks.

Offline

 

#6 2016-08-08 12:03:13 am

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

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

1994firehawk wrote:

it could be filtered according to date added but doing that requires ASObjC.


That's correct.

Applescript:

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)
   -- 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:


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

Offline

 

#7 2016-08-08 07:04:42 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3670

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

Hello Shane

Is there a better way to drop the time component in dateLimit ?

Applescript:

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

set dateLimit to current application's NSDate's dateWithTimeIntervalSinceNow:-(30 * days)
set theCalendar to current application's NSCalendar's currentCalendar()
set theComponents to theCalendar's components:252 fromDate:dateLimit
set theYear to theComponents's |year|()
set theMonth to theComponents's |month|()
set theDay to theComponents's |day|()
set dateLimit to theCalendar's dateWithEra:1 |year|:theYear |month|:(theMonth as integer) |day|:theDay hour:0 minute:0 |second|:0 nanosecond:0
--dateLimit as date

Yvan KOENIG running El Capitan 10.11.6 in French (VALLAURIS, France) lundi 8 aout 2016 15:01:46

Offline

 

#8 2016-08-08 07:13:20 am

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

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

Yvan Koenig wrote:

Is there a better way to drop the time component in dateLimit ?


You mean to start from the beginning of the day? Sure:

Applescript:

set dateLimit to current application's NSDate's dateWithTimeIntervalSinceNow:-(30 * days)
set dateLimit to current application's NSCalendar's currentCalendar()'s startOfDayForDate:dateLimit


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

Offline

 

#9 2016-08-08 07:47:13 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3670

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

Thanks Shane.

From my point of view it would be fine to do that in the move handler because I feel that it would be a bit odd to take the time component in account for such a maintenance feature. This is why I dropped the time in my proposed code.


Yvan KOENIG running El Capitan 10.11.6 in French (VALLAURIS, France) lundi 8 aout 2016 15:46:26

Offline

 

#10 2016-08-08 08:16:16 am

1994firehawk
Member
Registered: 2016-08-06
Posts: 23

Re: 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

Offline

 

#11 2016-08-08 08:45:33 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3670

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

Applescript:

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

Offline

 

#12 2016-08-10 08:10:12 am

1994firehawk
Member
Registered: 2016-08-06
Posts: 23

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

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.

Offline

 

#13 2016-08-10 08:16:49 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3670

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

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

Offline

 

#14 2016-08-11 03:14:48 pm

1994firehawk
Member
Registered: 2016-08-06
Posts: 23

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

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: 

Applescript:

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.


Applescript:

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

Offline

 

#15 2016-08-11 05:30:53 pm

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

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

You check if it already exists like this:

Applescript:

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

And you move to the trash like this:

Applescript:

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

Edited to make it delete the correct file.

Last edited by Shane Stanley (2016-09-18 05:35:28 am)


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

Offline

 

#16 2016-08-12 04:37:31 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3670

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

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

Applescript:

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

Offline

 

#17 2019-06-13 11:26:42 am

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

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

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?)

Offline

 

#18 2019-06-13 11:46:18 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3670

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

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

Applescript:

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

as

Applescript:

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

Last edited by Yvan Koenig (2019-06-13 11:46:55 am)

Offline

 

#19 2019-06-13 11:57:55 am

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

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

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?

Offline

 

#20 2019-06-13 01:12:05 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3670

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

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

Offline

 

#21 2019-06-13 05:24:49 pm

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

Re: 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.


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

Offline

 

#22 2019-06-13 06:14:58 pm

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

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

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:

Applescript:


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.

Offline

 

#23 2019-06-13 06:31:51 pm

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

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

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

https://www.macosxautomation.com/apples … tadata_Lib

This should get you started:

Applescript:

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}


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

Offline

 

#24 2019-06-13 06:42:24 pm

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

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

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?

Last edited by JQ (2019-06-13 06:53:08 pm)

Offline

 

#25 2019-06-13 09:03:53 pm

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

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

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.


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

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)