I wrote this script that will add a .JPG file extension to files in a specific folder that currently have no file extension.
tell application "Finder"
activate
set theFolder to choose folder with prompt "Select the folder containing the items you wish to rename."
set theList to every file of theFolder whose name extension is ""
repeat with i from 1 to count the theList
set thisItem to item i of theList as alias
set currentName to name of thisItem
set newName to currentName & ".JPG" as Unicode text
set name of thisItem to newName
end repeat
end tell
What I’m wondering is how to make it work on subfolders as well.
Model: MacPro
AppleScript: 2.3
Browser: Firefox 12
Operating System: Mac OS X (10.6)
Just use “entire contents” in this line…
set theList to every file of entire contents of theFolder whose name extension is ""
Thanks regulus6633!
That is so simple. I have seen other posts on the subject that are rather complex & only go 1 level deep.
Now it works perfectly!
Hi,
btw, the name extension property can be set directly
Yes, for some reason it’s not a widely known command. Glad to help. Good luck.
I suspect it’s because it’s notoriously unreliable for anything other than relatively shallow hierarchies with no too many files. For larger hierarchies it tends to time out or just truncate the list. The big problem is knowing how big is too big.
You could use recursion to go down into however many sub-folders there are. I tweaked an existing script to do your extension addition.
Save this as an application. It is set up to run as a droplet. Drag a file, a group of files, or a folder full of files/folders. It will drill down into all the folders and rename the files.
property theTotalCount : 0
-- This droplet processes both files or folders of files dropped onto the applet
on open these_items
set theTotalCount to 0
repeat with i from 1 to the count of these_items
set this_item to (item i of these_items)
set the item_info to info for this_item
if folder of the item_info is true then
process_folder(this_item)
else if (alias of the item_info is false) then
my process_item(this_item)
end if
end repeat
beep 2
tell application "Finder"
activate
with timeout of 9999 seconds
display dialog "Total Files: " & theTotalCount buttons "OK" default button "OK" with icon 2 giving up after 9990
end timeout
end tell
end open
-- this sub-routine processes folders
on process_folder(this_folder)
set these_items to list folder this_folder without invisibles
repeat with i from 1 to the count of these_items
set this_item to alias ((this_folder as text) & (item i of these_items))
set the item_info to info for this_item
if folder of the item_info is true then
process_folder(this_item)
else if (alias of the item_info is false) then
my process_item(this_item)
end if
end repeat
end process_folder
on process_item(this_item)
tell (info for this_item) to set FileName to name's text 1 thru ((my (offset of ("." & name extension) in name)) - 1)
tell application "Finder" to set name of this_item to FileName & ".jpg"
set theTotalCount to theTotalCount + 1
end process_item