Hey guys.
This is a follow on from my previous thread, but it’s a different problem so it’s probably best to keep a seperate thread?
I’ve got a series of subfolders where there is a single JPEG file with the name structure xxxxxxxx_yyyyyyyyy.jpg
I need to remove the underscore and the yyyyyyyy part. So I went on the internet, and I found this:
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to "_"
set pFolder to choose folder
processThisFolder(pFolder)
on processThisFolder(sourceFolder)
set the theList to list folder sourceFolder without invisibles
set sourceFolder to sourceFolder as string
repeat with i from 1 to number of items in theList
set thisItem to item i of theList
set thisItem to (sourceFolder & thisItem) as alias
set thisItemInfo to info for thisItem
set isFolder to folder of thisItemInfo
if isFolder is true then
processThisFolder(thisItem)
else
set thisFileName to name of thisItemInfo
set withoutPrefix to last text item of thisFileName
tell application "Finder"
set name of thisItem to withoutPrefix
end tell
end if
end repeat
end processThisFolder
set AppleScript's text item delimiters to oldDelims
This will remove xxxxxxxx_ for ALL files in the folders. I need it to rename only the jpg’s - everything else including the containing folders must remain untouched. And of course, I need it to remove the _yyyyyyyy portion of the name, leaving just xxxxxxxxx.jpg
If I change:
set withoutSuffix to first text item of thisFileName
tell application "Finder"
set name of thisItem to withoutSuffix
end tell
I thin that should work for all files within all subfolders… as it is now looking for the string of text before the delimiter “_” and renaming the files as that string?
If thats right, then all I need to do is somehow limit it to jpg files only… How can I do that?
Thanks!
You may try this one
set oldDelims to AppleScript's text item delimiters
set pFolder to choose folder
processThisFolder(pFolder)
set AppleScript's text item delimiters to oldDelims
#=====
on processThisFolder(sourceFolder)
set the theList to list folder sourceFolder without invisibles
set sourceFolder to sourceFolder as string
repeat with i from 1 to number of items in theList
set thisItem to item i of theList
set thisItem to (sourceFolder & thisItem) as alias
set thisItemInfo to info for thisItem
set isFolder to folder of thisItemInfo
if isFolder is true then
processThisFolder(thisItem)
else if type identifier of thisItemInfo is "public.jpeg" then
set thisFileName to name of thisItemInfo
set AppleScript's text item delimiters to "_"
set beg to first text item of thisFileName
set AppleScript's text item delimiters to ""
tell application "Finder"
set name of thisItem to (beg & ".jpg")
end tell
end if
end repeat
end processThisFolder
I hope that I understood well what you need.
Yvan KOENIG (VALLAURIS, France) mercredi 15 janvier 2014 23:07:42
Thank you, I will give it a try.
else if type identifier of thisItemInfo is "public.jpeg" then
set thisFileName to name of thisItemInfo
set AppleScript's text item delimiters to "_"
set beg to first text item of thisFileName
set AppleScript's text item delimiters to ""
tell application "Finder"
set name of thisItem to (beg & ".jpg")
end tell
end if
So I am following this correctly, this is checking the file metadata for the file type “public.jpeg”, reading the file name before the underscore and saving that as a variable, and then telling finder to rename that file as that withj .jpg at the end? I assume without the “& .jpg” part, I would lose the file extensions? Thats cool, so I could adapt this to add file extensions to files that are missing their extension which could be useful in the future!
Thanks for your help
I wish to add that I dislike this code because :
(1) it uses the function info for which is deprecated.
(2) it uses the Finder for a task which may be done more efficiently by System Events
If the edited script really match your needs, I will rewrite it to fit my preferences.
Yvan KOENIG (VALLAURIS, France) jeudi 16 janvier 2014 12:50:39
It seems to work exactly as intended, thank you!
I found the original code by searching on Google (I think it led me to an old thread on here or on the Apple discussion forums). If you can make it better then please do
Here is the version matching my own specs.
set oldDelims to AppleScript's text item delimiters
set pFolder to choose folder
processThisFolder(pFolder)
set AppleScript's text item delimiters to oldDelims
#=====
on processThisFolder(sourceFolder)
set sourceFolder to sourceFolder as string
tell application "System Events"
set the theList to name of disk items of folder sourceFolder whose visible is true
end tell
repeat with thisFileName in theList
set thisItem to (sourceFolder & anItem)
tell application "System Events" to tell disk item thisItem
set isFolder to its class is folder
if isFolder then
set isJpeg to false
else
try
set isJpeg to its type identifier is "public.jpeg"
end try
end if
end tell
if isFolder then
processThisFolder(thisItem)
else if isJpeg then
set AppleScript's text item delimiters to "_"
set beg to first text item of thisFileName
set AppleScript's text item delimiters to ""
tell application "System Events"
set name of disk item thisItem to (beg & ".jpg")
end tell
end if
end repeat
end processThisFolder
Yvan KOENIG (VALLAURIS, France) jeudi 16 janvier 2014 18:31:52
Thanks for that - this version is much faster