How to move files instead of deleting in Applescript

Hello!

I found this awesome code by SGIII in an older thread and it works great to delete RAW files that do not match JPG in another folder. However for safety it would be perfect if the script could:

  • Create a new subfolder "RAW files to delete" in the RAW folder
  • Move those RAW files that do not have a equivalent JPG file to that folder


I tried to google and change the script, but since I'm a total newbie I couldn't do it.


That would make it more safe, since I otherwise could accidentally delete some files that are still needed.


Here is the script:



tell application "Finder"



set fr to choose folder with prompt "Choose subfolder with RAW files"



set fj to choose folder with prompt "Choose subfolder with corresponding JPEG files"



set {rFiles, jFiles} to {fr's files as alias list, fj's files as alias list}



set {rNames, jNames} to {my stripExt(rFiles), my stripExt(jFiles)}



repeat with i from 1 to count rFiles



if rNames's item i is not in jNames then delete rFiles's item i



end repeat



end tell








to stripExt(fileList) -- removes extension so names can be compared



set nn to {}



tell application "Finder"



repeat with f in fileList



set nn's end to ¬


(f's name as text)'s text 1 thru -(((f's name extension as text)'s length) + 2)



end repeat



end tell



return nn



end stripExt


I would really appreciate if somebody could help me out on with this :)

Macbook (2015 or later)

Posted on Dec 27, 2018 3:10 AM

Reply

Similar questions

6 replies

Dec 30, 2018 2:00 AM in response to Rosenvald

There is bug in the code that Niel proposed.


Instead of


set raw_folder to (make folder at rFiles with properties {name:"RAW files to delete"})

repeat with i from 1 to count rFiles

if rNames's item i is not in jNames then move rFiles's item i to raw_folder

end repeat



it should be:


set raw_folder to (make folder at fr with properties {name:"RAW files to delete"})

repeat with i from 1 to count rFiles

if rNames's item i is not in jNames then move rFiles's item i to raw_folder

end repeat


Note that in the first line where it says "rFiles" it should say "fr" - the latter is a folder, and the former is a collection of the files in that folder.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How to move files instead of deleting in Applescript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.