How to delete multiple folders together - applescript

Instead of doing
if exists folder1 then
delete folder1
end if
if exists folder2 then
delete folder2
end if
...
...
Is there a way to do:
delete folder1 folder2 ... folderN
Thanks.
LJ

You didn't say how you're setting folder1, folder2, etc, but assuming those variables are already set, here's how to use the list and repeat iterate through the list.
set folderList to {folder1, folder2}
repeat with thisFolder in folderList
delete thisFolder
end repeat

Related

AppleScript - Move all the files contained in subfolder to a top folder

I am looking for an edit to the current script. What I need is moving all the files from subfolders (recursively) in to one top folder, however if a file with the same name exist, create a new top-folder and continue there, this is what I have so far:
tell application "Finder"
try
set Random_name to random number from 100 to 9999
set theTopFolder to (choose folder)
set theFiles to a reference to every file of (entire contents of folder theTopFolder)
set theNewFolder to make new folder at theTopFolder with properties {name:"Flattened Files"}
move theFiles to theNewFolder
on error
set theNewFolder to make new folder at theTopFolder with properties {name:"Flattened Files" & Random_name}
move theFiles to theNewFolder
end try
end tell
Just to be clear the structure of the path is not:
Mainfolder/subfolder/file.xxx but Mainfolder/subfolder/sulbfolder2/subfolder3/....100/file.xxx so the script needs to work recursively which it does but it stops when a file exist with the same name
When a file with the same name exist my edit creates a new folder with Flattened Files+random number however when another file with the same name is moved the script stops for an error instead going ahead and creating a new Flattened Files+randonnumber folder. Any ideas?
Thank you
Your script isn't using recursion, it just lets the Finder get all the files. The additional error you are getting is because you are trying to move the whole list of files again.
One solution would be to step through the file items, testing for duplicates as you go, and make new folders as needed. The following script just moves duplicates to added folders (note that an error will still stop the script). I don't know how you are sorting the file list, so I included a line to uncomment to continue moving file items to the added folder.
set theTopFolder to (choose folder)
tell application "Finder"
set theNewFolder to make new folder at theTopFolder with properties {name:"Flattened Files"}
set theFiles to every file of (entire contents of folder theTopFolder) as alias list
repeat with aFile in theFiles
if file ((theNewFolder as text) & (name of aFile)) exists then -- use added folders for duplicates
set counter to 1
set done to false
repeat until done
set suffix to text -2 thru -1 of ("000000" & counter) -- leading zeros for sorting
set alternateFolder to (theTopFolder as text) & "Flattened Files" & space & suffix
tell me to (do shell script "mkdir -p " & quoted form of POSIX path of alternateFolder) -- make new folder as needed
if file (alternateFolder & ":" & (name of aFile)) exists then -- continue to next one
set counter to counter + 1
else
move aFile to folder alternateFolder
# set theNewFolder to folder alternateFolder -- uncomment to continue moving here after a duplicate
set done to true
end if
end repeat
else
move aFile to folder (theNewFolder as text)
end if
end repeat
end tell

Copying files in Finder with AppleScript

This is my first attempt at AppleScripting.
I have 3 folders that contain image files.
Test Folder 1 has 77 large, master files.
Test Folder 2 has 4 smaller files in a subfolder called ABC with the same name as files in Test Folder 1.
Test Folder 3 Contains an empty sub folder called ABC.
I want a script to check the file names in Test Folder 2 and copy the equivalent file names from Test Folder 1 to Test Folder 3 subfolder ABC
Here is what I have so far:
tell application "Finder"
set the_files to (files of folder "Macintosh HD:Users:Ronnie:Pictures:Test Folder 1:")
set the_file_names to (files of folder "Macintosh HD:Users:Ronnie:Pictures:Test Folder 2:ABC:")
set target_folder to ("Macintosh HD:Users:Ronnie:Pictures:Test Folder 3:ABC:")
if document files of the_file_names is equal to document files of the_files then duplicate the_files to target_folder
end tell
Currently it will copy all the files from Test Folder 1 so its seams that the "If" script does not work.
Can someone help?
Ronnie
This is one way to do it.
--this is my preference. I prefer to work with strings then coerce them to aliases
--"aliases" in the old AppleScript/Mac API sense, meaning special file/folder path data type
-- (this is what is returned when you use the choose file or choose folder commands, e.g.)
-- aliases can be coerced to and from strings
-- not to be confused with POSIX style paths which use "/" as separator, OR
-- Finder item objects, which are specific to the Finder ('file x of folder y of startup disk z' style)
set tf1 to "Macintosh HD:Users:Ronnie:Pictures:Test Folder 1:"
set tf2 to "Macintosh HD:Users:Ronnie:Pictures:Test Folder 2:ABC:"
set target_folder to "Macintosh HD:Users:Ronnie:Pictures:Test Folder 3:ABC:"
--above not needed in Finder tell block
tell application "Finder"
set fileNames to (name of files of alias tf1) --we get the names of the files here; "name" filters, so does "files"
set matchNames to (name of files of alias tf2) --and the names of the files we want to match in testing
repeat with thisName in fileNames --repeat loop is necessary for this
if thisName is in matchNames then
--notice the concatenating of folder and name to make new alias that gets copied
duplicate alias (tf1 & thisName) to alias target_folder
end if
end repeat
end tell

Move files to folder with the same first 5 characters in Applescript

Suppose I have the following files in folder "A":
"AAAAA 1x1", "AAAAA 1x2", "BBBBB 1x1", "BBBBB 1x2", "CCCCC 1x1", "CCCCC 1x2".
And in folder "B", I have the following folders:
"AAAAA", "BBBBB", "CCCCC".
What I'd like to do is move all the "AAAAA" files in folder "A", to folder "AAAAA" in folder "B", "BBBBB" files to folder "BBBBB", and so on.
How would I do this using Apple Script?
Try running a command like this in Terminal:
for f in A/*; do echo mv "$f" B/${f:2:5}; done
Remove echo to actually move the files.
Try:
set folderA to "/Users/Joao/Desktop/A"
set folderB to "/Users/Joao/Desktop/B"
tell application "System Events"
set subFolders to folders of (folder folderB)
repeat with subfolderB in subFolders
move (files of folder folderA whose name starts with (name of subfolderB)) to (path of subfolderB)
end repeat
end tell
Here's the code (thanks to some fine folks at MacScripter):
set sourceFolder to alias "SSD:Users:JPCanaverde:Documents:A"
set destinationFolder to alias "SSD:Users:JPCanaverde:Documents:B"
tell application "Finder"
repeat with aFolder in (get folders of destinationFolder)
set folderName to name of aFolder
set filesToMove to (files of sourceFolder whose name begins with folderName)
move filesToMove to (contents of aFolder)
end repeat
end tell

Finding and listing all subfolders but nothing else

I am trying to make a looping script that will take a parent folder and find all the sub folders. This way I can take them one at a time and run a second script on them.
This would work if I was looking for text files. I want to try to use it for find just folders but don't know how to and can't find anything in that direction.
-- Search for Subfolder
set ParentPath to choose folder
set allFiles to (do shell script "find " & quoted form of POSIX path of ParentPath & " -iname \"*.eps\""))
-- Process files
repeat with nFile in allFiles
my runScript(nFile)
end repeat
In theory it would be this.
-- Search for Subfolder
set ParentPath to choose folder
set allFiles to all folders inside ParentPath
-- Process files
repeat with nFile in allFiles
my runScript(nFile)
end repeat
Thanks!
With bash, you'd say:
shopt -s globstar nullglob
# the trailing slash below restricts matches to directories.
for dir in **/; do
some command with "$dir"
done
This recursively finds all directories under the current directory. If you're only looking for subdirectories of the current dir: for dir in */
find <dirname> -type d > subdirlist
You don't need to specify depth levels for find, either; it will keep going down to the end. You actually only specify a max depth if you don't want it to go too far.
You can use a find loop as the basis for your processing script, as well. As one example:-
find *.mp3 d.mp3 -type f | while read mp3s
do
echo $mp3s
<more commands>
done
Just try below:
find ./ -type d
Found this works perfectly.
set ParentPath to (choose folder) as string
set FolderList to {}
tell application "System Events"
set ItemList to disk items of folder ParentPath
repeat with CurrentItem in ItemList
if (class of CurrentItem) = folder then
set end of FolderList to ((path of CurrentItem) as alias)
end if
end repeat
end tell

Apple Script: Move files to appropriate Folder

I am fairly new to Apple Script and I was hoping to get some help doing this simple but redundant task.
Lets say I have a folder that has these folders
Jabba
Foo
Biggie
and I want to drop files in to this folder, and have it automatically sort 1 of 4 options by just file name.
If file name contains jabba go to the Jabba folder, if foo then Foo folder, ... if none, don't do anything leave it be.
Thanks
I have OSX 10.7.5
Try:
on adding folder items to theFolder after receiving theFiles
repeat with aFile in theFiles
tell application "Finder"
if aFile's name contains "Jabba" then
move aFile to (first folder of theFolder whose name = "Jabba")
else if aFile's name contains "Foo" then
move aFile to (first folder of theFolder whose name = "Foo")
else if aFile's name contains "Biggie" then
move aFile to (first folder of theFolder whose name = "Biggie")
end if
end tell
end repeat
end adding folder items to

Resources