Streamlining a script using the "Record" class
Streamlining a script using the "Record" class
- Subject: Streamlining a script using the "Record" class
- From: Eric Schult <email@hidden>
- Date: Wed, 21 Nov 2001 12:36:11 -0600
I'm trying to demonstrate to a PC-centric audience how Applescript can
automate a task where Windows cannot. I've written an example script (below)
that checks the file type of every file in a folder and appends an
appropriate Windows suffix to the file name.
I know I could do this simpler by keeping the file types and related
suffixes in a record, but haven't worked much with the record class. I don't
want to scare folks away from learning this if I can help it. Can anybody
lend a hand with this or other simplification suggestions?
Any help greatly appreciated.
WES
P.S. - Watch for the <option-return> characters ( , ) below!
set myFolder to choose folder with prompt "Select a folder." as string
tell application "Finder"
activate
set fileList to name of every file in folder myFolder
repeat with i from 1 to count of items in fileList
set fileType to file type of file (myFolder & ,
(item i in fileList) as string)
if fileType is "GIFf" then
if item i in fileList does not end with ".gif" then
try
set name of file (myFolder & (item i in fileList) as string) to ,
(item i in fileList & ".gif" as string)
on error
display dialog "Couldn't rename file \"" & ,
(item i in fileList) & "\"" & " to " & ,
((item i in fileList) & ".gif")
end try
end if
else if ((fileType is "EPSf") or (fileType is "EPSp")) then
if item i in fileList does not end with ".eps" then
try
set name of file (myFolder & (item i in fileList) as string) to ,
(item i in fileList & ".eps" as string)
on error
display dialog "Couldn't rename file \"" & ,
(item i in fileList) & "\"" & " to " & ,
((item i in fileList) & ".eps")
end try
end if
else if fileType is "PDF " then
if item i in fileList does not end with ".pdf" then
try
set name of file (myFolder & (item i in fileList) as string) to ,
(item i in fileList & ".pdf" as string)
on error
display dialog "Couldn't rename file \"" & ,
(item i in fileList) & "\"" & ,
" to " & ((item i in fileList) & ".pdf") with note caution ,
giving up after 5
end try
end if
else if fileType is "JPEG" then
if item i in fileList does not end with ".jpg" then
try
set name of file (myFolder & (item i in fileList) as string) to ,
(item i in fileList & ".jpg" as string)
on error
display dialog "Couldn't rename file \"" & ,
(item i in fileList) & "\"" & ,
" to " & ((item i in fileList) & ".jpg")
end try
end if
end if
end repeat
end tell
beep 2