Hi,
I would like to delete a file ending with the word ‘report’ only if exists. If it doesn’t exists then the applescript should continue till end.
Here is the code I’m currently using in my script
delete (every file whose name contains "report")
I need something like:
if exists file whose name contains “report” then delete (file whose name contains “report”)
In the same way, I would like to move a file whose extension is PDF to a certain folder with if exists condition: Here is the current code from my applescript:
move (1st file whose name is NName & ".pdf") to folder "PDF"
To easily understand the situation, the complete applescript is given below:
tell application "Finder"
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"."}
set theLocation to (target of front window)
tell theLocation
if not (exists folder "BL") then make new folder at it with properties {name:"BL"}
if not (exists folder "BL_RNT") then make new folder at it with properties {name:"BL_RNT"}
if not (exists folder "CT") then make new folder at it with properties {name:"CT"}
if not (exists folder "DF") then make new folder at it with properties {name:"DF"}
if not (exists folder "MZ") then make new folder at it with properties {name:"MZ"}
if not (exists folder "PDF") then make new folder at it with properties {name:"PDF"}
if not (exists folder "QXP") then make new folder at it with properties {name:"QXP"}
if not (exists folder "QXP_RNT") then make new folder at it with properties {name:"QXP_RNT"}
if not (exists folder "TAB") then make new folder at it with properties {name:"TAB"}
if not (exists folder "TAB_RNT") then make new folder at it with properties {name:"TAB_RNT"}
delete (every file whose name contains "report")
try
set RN to (1st file whose name extension is "qxp")
set filename to name of RN
set nwe to first text item of filename
move (1st file of folder "Pictures" whose name contains nwe & ".jpg") to theLocation with replacing
set NName to text returned of (display dialog "Enter New FileName with Page No. for : " & nwe default answer nwe)
set name of 1st file whose name extension is "qxp" to NName & ".qxp"
set name of 1st file whose name extension is "pdf" to NName & ".pdf"
set name of 1st file whose name contains nwe & ".jpg" to NName & ".jpg"
move (1st file whose name is NName & ".qxp") to folder "DF"
move (1st file whose name is NName & ".pdf") to folder "PDF"
move (1st file whose name is NName & ".jpg") to folder "CT"
move (every file of folder "Pictures") to theLocation with replacing
delete folder "Pictures"
set AppleScript's text item delimiters to oldDelims
end try
end tell
end tell
display dialog "Folder creation complete..... Enjoy !" & return & return & "© Masood Ahmad" buttons {"Thanks"} giving up after 2
I tried it myself, here is my final script… I know it is too long, It will be great if someone can squeeze it.
tell application "Finder"
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"."}
set theLocation to (target of front window)
tell theLocation
if not (exists folder "BL") then make new folder at it with properties {name:"BL"}
if not (exists folder "BL_RNT") then make new folder at it with properties {name:"BL_RNT"}
if not (exists folder "CT") then make new folder at it with properties {name:"CT"}
if not (exists folder "DF") then make new folder at it with properties {name:"DF"}
if not (exists folder "MZ") then make new folder at it with properties {name:"MZ"}
if not (exists folder "PDF") then make new folder at it with properties {name:"PDF"}
if not (exists folder "QXP") then make new folder at it with properties {name:"QXP"}
if not (exists folder "QXP_RNT") then make new folder at it with properties {name:"QXP_RNT"}
if not (exists folder "TAB") then make new folder at it with properties {name:"TAB"}
if not (exists folder "TAB_RNT") then make new folder at it with properties {name:"TAB_RNT"}
if exists (1st file whose name contains "report") then delete (1st file whose name contains "report")
try
set RN to (1st file whose name extension is "qxp")
set filename to name of RN
set nwe to first text item of filename
if exists (1st file of folder "Pictures" whose name contains nwe & ".jpg") then
move (1st file of folder "Pictures" whose name contains nwe & ".jpg") to theLocation with replacing
end if
set NName to text returned of (display dialog "Enter New FileName with Page No. for : " & nwe default answer nwe)
if exists (1st file whose name extension is "qxp") then set name of 1st file whose name extension is "qxp" to NName & ".qxp"
if exists (1st file whose name extension is "pdf") then set name of 1st file whose name extension is "pdf" to NName & ".pdf"
if exists (1st file whose name extension is "jpg") then set name of 1st file whose name contains nwe & ".jpg" to NName & ".jpg"
if exists (1st file whose name is NName & ".qxp") then move (1st file whose name is NName & ".qxp") to folder "DF"
if exists (1st file whose name is NName & ".pdf") then move (1st file whose name is NName & ".pdf") to folder "PDF"
if exists (1st file whose name is NName & ".jpg") then move (1st file whose name is NName & ".jpg") to folder "CT"
move (every file of folder "Pictures") to theLocation with replacing
if exists folder "Pictures" then delete folder "Pictures"
set AppleScript's text item delimiters to oldDelims
end try
end tell
end tell
display dialog "Folder creation complete..... Enjoy !" & return & return & "© Masood Ahmad" buttons {"Thanks"} giving up after 2
Hi.
Instead of testing every time with ‘exists’, you could put the moves and deletions in ‘try’ statements. This way, the script would simply assume that the files and folders existed (which would save time when they did) and ignore the errors when they didn’t. And if you put every action which depended on the existence of a file or folder into the same ‘try’ statement, you could skip all those actions as soon as an error occurred without having to test or try again.
In the version of the script below, I’ve used a shell script to create the folders because it’s much easier. But I’ve stayed with the Finder for everything else as you’re obviously more familiar with it.
tell application "Finder" to set theLocation to (target of front Finder window)
set locationPOSIX to quoted form of POSIX path of (theLocation as alias)
do shell script ("mkdir -p " & locationPOSIX & "{BL,BL_RNT,CT,DF,MZ,PDF,QXP,QXP_RNT,TAB,TAB_RNT}")
-- Or: do shell script ("mkdir -p " & locationPOSIX & "{{BL,QXP,TAB}{,_RNT},CT,DF,MZ,PDF}")
tell application "Finder"
tell theLocation
try
delete (1st file whose name contains "report")
end try
try
set filename to name of (1st file whose name extension is "qxp")
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"."}
set nwe to text 1 thru text item -2 of filename
set AppleScript's text item delimiters to oldDelims
try
move (1st file of folder "Pictures" whose name contains nwe & ".jpg") to theLocation with replacing
end try
set NName to text returned of (display dialog "Enter New FileName with Page No. for : " & nwe default answer nwe)
try
set name of 1st file whose name extension is "qxp" to NName & ".qxp"
move file (NName & ".qxp") to folder "DF"
end try
try
set name of 1st file whose name extension is "pdf" to NName & ".pdf"
move file (NName & ".pdf") to folder "PDF"
end try
try
set name of 1st file whose name contains nwe & ".jpg" to NName & ".jpg"
move file (NName & ".jpg") to folder "CT"
end try
try
move every file of folder "Pictures" to theLocation with replacing
delete folder "Pictures"
end try
end try
end tell
end tell
display dialog "Folder creation complete..... Enjoy !" & return & return & "© Masood Ahmad" buttons {"Thanks"} giving up after 2