Hi all, I 've been trying to learn this powerful tool that is Applescript, but am in the toddler stages.
I’ve got a couple of file renaming commands and modified them. They work well on their own. However, I’m trying to make them work together as one script, that I can in turn, make it into an app. This is what I seek to accomplish:
-Prompt the user to specify the folder the files to be renamed are located in, then, input a variable , which we will call version number (I have half of this done, the variable input problem isn’t a problem to do)
-Go through the filenames in a Replace the characters “b.” for “b_” (This is done)
then
-Add the version number, such as “_v777” to the suffix before file extension (This is done)
-Display a message when all of it is done
These are both finder actions and I can’t figure out how to make them play well with each other in Applescripts. I don’t want to use automator, since I’d like to make one single app and also keep it simple so other non experienced people can use. I did try putting in a delay command, but that gave me a weird combination of results.
Any help with this would be awesome! Also, if anyone can recommend a good book or online course that touches on important foundations of apple script, that would be great to know too.
Thank you
Note: All the files inside a given folder would need rename. In other words, I don’t need any sorting to happen.
Here is the first command to replace b. with b_ (Isolated):
set source_folder to (choose folder with prompt “Find and Replace all files in folder:”) as alias
set FindText to “b.”
set ReplaceText to “b_”
tell application “Finder”
set AllFiles to every file of entire contents of source_folder whose name does not start with “.”
repeat with x in AllFiles
set FileName to name of x
if FileName contains FindText then
set AppleScript’s text item delimiters to FindText
if the (count of text items of FileName) is 2 then
set NewName to (((first text item of FileName) as string) & ReplaceText & ((last text item of FileName) as string)) as text
set name of x to NewName
else
display dialog ("Text appears more than once. No changes made to " & FileName)
end if
end if
end repeat
end tell
Here is the second command to add version to suffix (Isolated):
try
tell application “Finder” to set the source_folder to (choose folder with prompt “Pick the folder containing the files to rename:”) as alias
end try
set the item_list to list folder source_folder without invisibles
set source_folder to source_folder as string
repeat with i from 1 to number of items in the item_list
set this_item to item i of the item_list
set this_item to (source_folder & this_item) as alias
set this_info to info for this_item
set the current_name to the name of this_info
tell application “Finder”
set current_name to name of this_item
set prefix to “_v777” as text
set extension_length to length of (get name extension of file this_item)
set new_name to (characters 1 through (-2 - extension_length) of current_name) & prefix & “.” & name extension of file this_item as text
set name of this_item to new_name
end tell
end repeat