I’m trying to write an applescript that will go through all files in a folder, remove the last 41 characters, then add back “.jpg”
An example of a file name would be: AB01A-img01-479c07e5-a498-4806-83b8-322ef7fa2032.jpg
I want it to be changed to: AB01A-img01.jpg
Here’s what I came up with…
tell application “Finder”
set file_list to every file of ((path to desktop as text) & “Source”)
repeat with myfile in file_list
set name of myfile to “not sure what to do here”
end repeat
end tell
in addition to being incomplete, I get errors right on the second line!
Thanks for your help!
Browser: Safari 525.20.1
Operating System: Mac OS X (10.5)
The error on the second line comes from a conflict in vocabulary between Finder and the StandardAdditions OSAX. If you want to use the path to desktop variation (from StandardAdditions) you can use code like this:
set f to ((path to desktop as Unicode text) & "Source:")
tell application "Finder"
set file_list to every file of folder f
.
end tell
If you want to use Finder’s terminology, you can do (among other things):
tell application "Finder"
set file_list to every file of folder "Source" of desktop
.
end tell
With some minimal error checking included, the whole thing looks something like this:
tell application "Finder"
set file_list to every file of folder "Source" of desktop
repeat with myfile in file_list
set n to name of myfile
if n ends with ".jpg" and length of n is greater than 41 then
set n to (text 1 through -42 of n) & ".jpg"
set name of myfile to n -- will throw an error if the short filename already exists
end if
end repeat
end tell
The inflexibly hardcoded “.jpg”, 41, and -42 leave something to be desired, but it should accomplish your stated goal.
Model: iBook G4 933
AppleScript: 1.10.7
Browser: Safari Version 3.1.2 (4525.22)
Operating System: Mac OS X (10.4)