I am trying to select a folder to purge from, select a text document with a list of items to purge, then either select a folder to move the files to, or send the files straight to the trash.
Below Is what I have so far. It half works. It deletes the files from the list, but then it’s last action is to delete the original folder chosen to purge.
I am sure I am missing something simple, I think I just need a second set of eyes.
Thanks in advance for any help you can provide.
tell application "Finder"
--Select folder with files to Purge
set folderofiles to choose folder with prompt "Select the folder of files you want to purge."
--Choose txt file list of files to be deleted
set filelist to choose file with prompt "Select the textfile containing your list of EXPIRED PDF Ad files."
--set finaldestination to choose folder with prompt "Select the directory these files will be moved to."
--Present choice of choosing a folder or sending to trash
set question to display dialog "Where would you like the Expired Ad files to be placed." buttons {"Cancel", "Trash", "Browse..."} with icon 2
set answer to button returned of question
if answer is "Browse..." then
set finaldestination to choose folder with prompt "Select the directory these files will be moved to."
end if
if answer is "Trash" then
set finaldestination to path to trash
end if
tell application "Finder"
set the_list_count to paragraphs of (read filelist from 1)
repeat with i from 1 to (count the_list_count)
try
set filepath to POSIX path of folderofiles
set destination to POSIX path of finaldestination
set filename to item i of the_list_count
set filetomove to filepath & filename
do shell script "/bin/mv '" & filetomove & "' '" & destination & "'"
end try
end repeat
display dialog "Purge is complete"
end tell
if finaldestination is (path to trash) then
empty trash
end if
end tell
Model: macbook pro
AppleScript: 2.61
Browser: Firefox 32.0
Operating System: Mac OS 9.1.x
if the contents of the text file ends with a LF or CR character, the read command treats it as a paragraph containing an empty string. Then the parent folder is deleted.
As you empty the trash anyway, the Finder is not needed at all
This version of the script checks the length of the paragraph
--Select folder with files to Purge
set folderofiles to POSIX path of (choose folder with prompt "Select the folder of files you want to purge.")
--Choose txt file list of files to be deleted
set filelist to choose file with prompt "Select the textfile containing your list of EXPIRED PDF Ad files."
--set finaldestination to choose folder with prompt "Select the directory these files will be moved to."
--Present choice of choosing a folder or sending to trash
set {button returned:answer} to display dialog "Where would you like the Expired Ad files to be placed." buttons {"Cancel", "Trash", "Browse..."} with icon 2
if answer is "Browse..." then
set finaldestination to POSIX path of (choose folder with prompt "Select the directory these files will be moved to.")
else
set finaldestination to missing value
end if
set fileNames to paragraphs of (read filelist)
repeat with aFile in fileNames
if length of aFile > 0 then
try
set filetomove to folderofiles & aFile
if finaldestination is missing value then
do shell script "/bin/rm " & quoted form of filetomove -- deletes the file immediately
else
do shell script "/bin/mv " & quoted form of filetomove & space & quoted form of finaldestination
end if
end try
end if
end repeat
display dialog "Purge is complete"
choose folder with prompt "Select the folder of files you want to purge."
--> alias "CRP-IT-10010:Users:thumser:Desktop:PURGE:"
choose file with prompt "Select the textfile containing your list of EXPIRED PDF Ad files."
--> alias "CRP-IT-10010:Users:thumser:Desktop:wMactive-RC.txt"
display dialog "Where would you like the Expired Ad files to be placed." buttons {"Cancel", "Trash", "Browse..."} with icon 2 given «class Krtn»:{button returned:"answer"}
--> {button returned:"Browse..."}
choose folder with prompt "Select the directory these files will be moved to."
--> alias "CRP-IT-10010:Users:thumser:Desktop:Remove:"
end tell
tell current application
read alias “CRP-IT-10010:Users:thumser:Desktop:wMactive-RC.txt”
→ "337624-01.PDF
337641-01.PDF
364023-01.PDF
364963-01.PDF
365197-01.PDF
367776-01.PDF
368267-01.PDF
368993-01.PDF
369039-01.PDF
…
373707-01.PDF
373709-01.PDF
373772-01.PDF
"
end tell
tell application “AppleScript Editor”
display dialog “Purge is complete”
→ {button returned:“OK”}
end tell
Result:
{button returned:“OK”}
Working script posted below. Thanks for your help Stefen
--Select folder with files to Purge
set folderofiles to POSIX path of (choose folder with prompt "Select the folder of files you want to purge.")
--Choose txt file list of files to be deleted
set filelist to choose file with prompt "Select the textfile containing your list of EXPIRED PDF Ad files."
--set finaldestination to choose folder with prompt "Select the directory these files will be moved to."
--Present choice of choosing a folder or sending to trash
set {button returned:answer} to display dialog "Where would you like the Expired Ad files to be placed." buttons {"Cancel", "Trash", "Browse..."} with icon 2
if answer is "Browse..." then
set finaldestination to POSIX path of (choose folder with prompt "Select the directory these files will be moved to.")
else
set finaldestination to missing value
end if
set fileNames to paragraphs of (read filelist)
repeat with aFile in fileNames
if length of aFile > 0 then
try
set filetomove to folderofiles & aFile
if finaldestination is missing value then
do shell script "/bin/rm " & quoted form of filetomove -- deletes the file immediately
else
do shell script "/bin/mv " & quoted form of filetomove & space & quoted form of finaldestination
end if
end try
end if
end repeat
display dialog "Purge is complete"