I am trying to get a list of all files (visible ones only) that are less than x days old on a volume/in a folder.
I have this:
tell application "Finder"
set SearchFolder to "MacBook Pro:Users:macbookwork:Documents:ScanSnap:"
set FilesToCopy1 to (every file of entire contents of folder SearchFolder whose modification date > ((current date) - 14 * days))
end tell
But cannot seem to get it to work. The script at the above referred to post seems to return filepaths with an extra backslash just before the filename in each case. It does it very quickly though:)
set FilestoCopy to {}
set theFolder to "MacBook Pro:Users:macbookwork:documents:"
set theFiles to paragraphs of (do shell script "find " & quoted form of POSIX path of theFolder & " -type f ! -name '.*'")
repeat with oneFile in theFiles
try
if modification date of (info for (oneFile as POSIX file)) > ((current date) - 14 * days) then set end of FilestoCopy to oneFile as POSIX file
end try
end repeat
Pretty quick. At least much quicker than using Finder or System Events…
Need to add the copy routine but that will be tomorrow.
set documentsFolder to POSIX path of (path to documents folder)
set destinationFolder to POSIX path of (choose folder)
do shell script "/usr/bin/find " & quoted form of documentsFolder & " -type f ! -name '.*' -mtime -14d -print -exec /bin/cp {} " & quoted form of destinationFolder & " \\;"
-mtime -14d finds all files with modification date within the last 14 days -print -exec passes the file paths to the following command (cp)
I have a further query. I have decided to make aliases to the files in question, rather than copy them.
I have this script, which works as far as it goes:
set FilestoCopy to {}
set theFolder to "MacBook Pro:Users:macbookwork:documents:"
set AliasFolder to "MacBook Pro:Users:macbookwork:documents:aliases:"
set theFiles to paragraphs of (do shell script "find " & quoted form of POSIX path of theFolder & " -type f ! -name '.*'")
repeat with oneFile in theFiles
try
if modification date of (info for (oneFile as POSIX file)) > ((current date) - 14 * days) then set end of FilestoCopy to oneFile as POSIX file
end try
end repeat
tell application "Finder"
repeat with eachfile in FilestoCopy
make alias to eachfile at AliasFolder
end repeat
end tell
It creates aliases as expected. But if I run it a second time, it creates alias2, and if I run it again, it creates alias 3 etc etc
How do I test to see if the alias already exists before executing the make alias command?
I have tried various syntactic ways, but none work.
set FilestoCopy to {}
set theFolder to path to documents folder
set AliasFolder to theFolder & "aliases:" as string
set theFiles to paragraphs of (do shell script "find " & quoted form of POSIX path of theFolder & " -type f ! -name '.*'")
repeat with oneFile in theFiles
try
if modification date of (info for (oneFile as POSIX file)) > ((current date) - 14 * days) then set end of FilestoCopy to oneFile as POSIX file
end try
end repeat
tell application "Finder"
repeat with eachfile in FilestoCopy
set theFile to name of (eachfile as alias)
log theFile
if not (exists (AliasFolder & theFile)) then
make alias to eachfile at AliasFolder
end if
end repeat
end tell
set FilestoCopy to {}
set theFolder to ((path to home folder as text) & "Downloads:Testfolder" as alias)
set AliasFolder to (path to desktop folder as text) & "Aliases"
set theFiles to paragraphs of (do shell script "find " & quoted form of POSIX path of theFolder & " -type f ! -name '.*'")
repeat with oneFile in theFiles
try
if modification date of (info for (oneFile as POSIX file)) > ((current date) - 14 * days) then set end of FilestoCopy to oneFile as POSIX file
end try
end repeat
tell application "Finder"
repeat with eachfile in FilestoCopy
set theFile to name of (eachfile as alias)
log theFile
if not (exists (AliasFolder & theFile)) then
make alias to eachfile at AliasFolder
end if
end repeat
end tell
And now it fails to spot any duplicate aliases:( It recreates fresh aliases every time I run the script.
Don’t suppose you have a suggestion to work around packages - the script will systematically duplicate an alias for Page documents (and Numbers documents - and I presume all other types of document that are in fact packages).
I don’t think that is the answer. I want to create aliases for the Pages documents (so need to be included in find command), but I only want to create alias once.
The problem seems to be with the
.
When I look at the values of theFile, nothing untoward looks different between a PDF and a Pages document (for example). Yet the existence text does not “see” the Pages document. Whereas it “sees” the PDF.
As a hack, I could have a repeat loop at the end of the script delete every item in AliasFolder whose name contains alias - the first alias created has exactly the same name as the original file - subsequent aliases are names “…alias”, “… alias2” etc etc. So this would “work”, albeit in a rather messy fashion.
I will do this as last resort, if mystery cannot be solved.
set aliasList to every item in folder AliasFolder
repeat with eachfile in aliasList
if name of (eachfile as alias) contains "alias" then
do shell script ("rm " & POSIX path of eachfile as alias)
end if
end repeat
Except that the shell script returns an error - file not found. I am trying to delete the offending aliases WITHOUT going to the trash: ie a permanent delete.
OK. I have it, complete with hack to avoid duplication of alias files:
set FilestoCopy to {}
set theFolder to ((path to home folder as text) & "Downloads:Applications" as alias)
set AliasFolder to (path to desktop folder as text) & "Aliases:"
set theFiles to paragraphs of (do shell script "find " & quoted form of POSIX path of theFolder & " -type f ! -name '.*'")
repeat with oneFile in theFiles
try
if modification date of (info for (oneFile as POSIX file)) > ((current date) - 14 * days) then set end of FilestoCopy to oneFile as POSIX file
end try
end repeat
tell application "System Events"
repeat with eachfile in FilestoCopy
set theFile to name of (eachfile as alias)
log theFile
if not (exists item (AliasFolder & theFile)) then
tell application "Finder" to make alias to eachfile at AliasFolder
end if
end repeat
set aliasList to every item in folder AliasFolder
repeat with eachfile in aliasList
if name of (eachfile as alias) contains "alias" then
my deleteFile(eachfile as alias)
end if
end repeat
end tell
on deleteFile(filename)
do shell script "rm -Rf " & quoted form of POSIX path of filename
end deleteFile