Drill Deeper In Recursive Processing - applescript

I have a folder that is three levels deep, each level contains a good amount of files. I am able to get two levels deep with the script below, however the third level is posing a bit of a challenge. Would someone mind offering some guidance as to how I am supposed to get one level deeper? Use python or another language wouldn't be acceptable, as I am trying to see how this works with AppleScript.
set sourceFolder to (choose folder)
tell application "Finder"
my changeFileNameCase(sourceFolder, "upper")
repeat with subFolder in (get every folder of folder sourceFolder)
my changeFileNameCase(subFolder as alias, "upper")
#This Is No Good
repeat with theFolder in (get every folder of folder subFolder)
my changeFileNameCase(theFolder, "upper")
end repeat
end repeat
end tell
on changeFileNameCase(targetFolder, caseToSwitchTo)
tell application "Finder"
set fileList to every file of folder targetFolder
repeat with theFile in fileList
set oldName to name of theFile
set newName to my changeCaseOfText(oldName, caseToSwitchTo)
set the name of theFile to newName
end repeat
end tell
end changeFileNameCase

To make the handler recursive you have to get all items of the folder and check the class of each item.
If the class is folder call the handler passing the folder
If the class is file rename it
set sourceFolder to (choose folder)
changeFileNameCase(sourceFolder, "upper")
on changeFileNameCase(targetFolder, caseToSwitchTo)
tell application "Finder"
set theList to every item of targetFolder
repeat with i from 1 to count theList
set theItem to item i of theList
if class of theItem is folder then
my changeFileNameCase(theItem, caseToSwitchTo)
else
set oldName to name of theItem
set newName to my changeCaseOfText(oldName, caseToSwitchTo)
set name of theItem to newName
end if
end repeat
end tell
end changeFileNameCase

Related

AppleScript Loop Print All File Folder Names Path

Help I need to print into the output of IDE all of the folder and file names contained in a folder. Here is my code so far:
set Music_Collection_Folder to POSIX file "/Volumes/A_Live HD/Music/Collection" as alias
set the_string to ""
tell application "Finder"
repeat with this_item in (get items of ("/Volumes/A_Live HD/Music/Collection"))
set the_string to the_string & name of this_item & tab & modification date of this_item & tab & size of this_item & tab & kind of this_item & return
end repeat
end tell
You created an alias of the target folder. Use it!
The Finder has no idea what a POSIX path is
repeat with this_item in (get items of Music_Collection_Folder) ...

Applescript move all files from a specific folder name.

I have a drive that has multiple "Approved" folders with files in them.
The Approved folders are in various locations on the drive.
I need to move the contents of the Approved folders to another directory via applescript.
This is what I have come up with so far but does not seem to be doing the trick, it runs but no files are moved...
Any tips would be great
Thanks
set sourceFolder to "THIS:" as alias
set destinationFolder to "THAT:" as alias
tell application "Finder"
repeat with aFolder in (get folders of sourceFolder)
set folderName to name of aFolder
set filesToMove to (files of sourceFolder whose name = "Approved")
move filesToMove to destinationFolder
end repeat
end tell
According to your description, you are looking for folders named Approved, but your script is searching for files named Approved. Try this:
set sourceFolder to choose folder
set destinationFolder to choose folder
tell application "Finder"
repeat with aFolder in (get folders of sourceFolder)
if aFolder's name is "Approved" then
set filesToMove to (files of aFolder)
move filesToMove to destinationFolder
end if
end repeat
end tell

applescript strange behaviour with rename file script

I wrote this simple script that changes filenames of the folder Images on my desktop. The script works on the first run, however when I rerun it with a different string for the variable newName (same string would throw error: filename already exists). It only changes the name of the even numbers of "item x of the Folder".
Can someone enlighten me on how this happens and what to do to avoid this peculiar behaviour. I'm having a hard time figuring this out.
Thank you very much in advance.
tell application "Finder"
set theFolder to ((path to desktop folder) & "Images") as string as alias
set myImages to the name of every file of theFolder
set indexOfFolder to (count items in myImages) as number
set newName to "whateverName" as string
repeat with x from 1 to indexOfFolder
set name of item x of theFolder to newName & x & ".JPG"
end repeat
end tell
edit: After some more testing, it appears that the problem would not point exclusively to unchanged uneven numbers. With more then 20 files it appears to switch from 20 on to the even numbers that will not be renamed.
It seems to me that you are working on the name of the Folder, and also the chars of the folder (items of the folder)
Please compare with this that should work correctly:
tell application "Finder"
set theFolder to ((path to desktop folder as text) & "Images")
set myImages to the name of every file of folder theFolder
set indexOfFolder to (count myImages)
set newname to "Z picture " as string
repeat with x from 1 to indexOfFolder
set name of item x of folder theFolder to newname & x & ".JPG"
end repeat
end tell
It still isn't bug free, next discrepancy is that you use files, when gathering the contents into the myImages variable, but you address the item of the folder, which are two different object classes. I have also, finally set the folder as the in this last implementation, to avoid redundancy. Pleas also observe where I have placed the coercions, and which I have removed, because they were redundant.
tell application "Finder"
set theFolder to folder ((path to desktop folder as text) & "Images")
set myImages to the name of every file of theFolder
set indexOfFolder to (count myImages)
set newname to "Z picture " as string
repeat with x from 1 to indexOfFolder
set name of file x of theFolder to newname & x & ".JPG"
end repeat
end tell
This version, should work even though when files changes names, as the file-list is obtained up front.
tell application "Finder"
set theFolder to folder ((path to desktop folder as text) & "Images")
set myImages to every file of theFolder
set indexOfFolder to (count myImages)
set newname to "Z picture " as string
repeat with x from 1 to indexOfFolder
set name of item x of myImages to newname & x & ".JPG"
end repeat
end tell

applescript to move 2 kinds of files to different folders

Hi, I'm trying to create an Applescript to do the following:
Over the week a have several jpg and eps files on my hard drive. I have two folders: Vector and Images.
After some few days I get a few dozens of this mixed files which I have to manually select and move to its respective folder.
How can I move the .eps to the VECTOR folder and the jpg to IMAGES folder.
I donĀ“t know anything about applescripting, so I copied parts of other scripts and put this together:
on run {input, parameters} -- copy
if theExtension is "eps" then
set destination to ~/user/Desktop/VECTO
end if
tell application "Finder"
move anItem to destination
if theExtension is "jpg" then
set destination to ~/user/Desktop/IMAGO
end if
tell application "Finder"
move anItem to destination
end run
Of course it is wrong, but wish someone could help me put this straight
Thnx!
I assume you are incorporating this into an Automator workflow from the way the question is phrased.
on run {input, parameters} -- copy
repeat with aFile in input
tell application "Finder"
if name extension of aFile is "eps" then
move aFile to folder ((path to desktop as text) & "VECTO")
else if name extension of aFile is "jpg" then
move aFile to folder ((path to desktop as text) & "IMAGO")
end if
end tell
end repeat
end run
If not you can use:
set myFiles to (choose file with multiple selections allowed)
repeat with aFile in myFiles
tell application "Finder"
if name extension of aFile is "eps" then
move aFile to folder ((path to desktop as text) & "VECTO")
else if name extension of aFile is "jpg" then
move aFile to folder ((path to desktop as text) & "IMAGO")
end if
end tell
end repeat

AppleScript to create aliases of files in a folder

So I have this AppleScript for making aliases of all the contents of one folder to another folder.
I works perfectly unless there is only one item in the source folder.
tell application "Finder"
set SourceFolder to (choose folder with prompt "Please choose Source folder:") as string
set DestinationFolder to (choose folder with prompt "Choose Destination folder for aliases:") as string
set theFolders to every item of entire contents of folder SourceFolder
repeat with thisFolder in theFolders
set thisName to name of thisFolder
make new alias file at folder DestinationFolder to thisFolder without invisibles
end repeat
end tell
Any idea why it's not getting anything when there's one item only? When there's at least 2 items in the source folder it creates aliases for both in the destination.
On a side note, any way to get it to remember the source and destination folders between times you run it?
Try:
property SourceFolder : missing value
property DestinationFolder : missing value
if SourceFolder = missing value then
set SourceFolder to (choose folder with prompt "Please choose Source folder:")
set DestinationFolder to (choose folder with prompt "Choose Destination folder for aliases:")
else
tell application "Finder"
set theFolders to every item of entire contents of SourceFolder as list
repeat with thisFolder in theFolders
make new alias file at DestinationFolder to thisFolder
end repeat
end tell
end if

Resources