Re: Streamlining a script using the "Record" class
Re: Streamlining a script using the "Record" class
- Subject: Re: Streamlining a script using the "Record" class
- From: Andy Wylie <email@hidden>
- Date: Mon, 26 Nov 2001 20:31:27 +1300
on 26/11/01 5:54 PM, Marc K. Myers at email@hidden wrote:
>
> Date: Wed, 21 Nov 2001 12:36:11 -0600
>
> Subject: Streamlining a script using the "Record" class
>
> From: Eric Schult <email@hidden>
>
> To: <email@hidden>
>
>
>
> 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.
>
>
I don't know how records could help you, but you could simplify your
>
script greatly by using a couple of lists.
yes but isn't it slow?
-->Added 1076 suffixes to 1182 files in 2037 ticks
-->Removed 1076 suffixes from 1182 files in 2107 ticks
anyone know a faster way?...
property typeSufx : "
EPSf == .eps
EPSp == .eps
TEXT == .txt
JPEG == .jpg
GIFf == .gif
PICT == .hah
"
try
display dialog "Suffix" buttons {"mac", "windozey", "cancel"} default
button 2 with icon 1
set butRet to button returned of result
set tick1 to system value lm ticks
set xlst to the entries in (choose folder) whose kinds are file as alias
to a depth of -1
set tick1 to ((system value lm ticks) - tick1)
set xlstCount to (count of xlst)
on error m number n
--use cursor 0
if n = -48 then
error {n, m}
else
error {n, m}
end if
end try
if butRet = "Remove" then
removeSuffix(xlst, xlstCount, tick1)
else if butRet = "Add" then
addSuffix(xlst, xlstCount, tick1)
end if
to addSuffix(xlst, xlstCount, tick1)
set tick2 to system value lm ticks
set procCount to 0
use cursor -1
repeat with i in xlst
set theInfo to basic info for i
set type to ((theInfo)'s file type)
set fileName to ((theInfo)'s name)
set suffix to REMatch typeSufx pattern (type & " == (.+)") using
"\\1"
if (suffix false) and ((suffix is in fileName) = false) then
if (count of fileName) < 27 then
set newName to fileName
else
set newName to (text 1 thru 27 of fileName)
end if
try
collate {file ("" & i)} by renaming it to newName & suffix
set procCount to procCount + 1
on error m number n
if n = -48 then -- duplicate filename
if (length of newName) = 27 then set newName to
(newName)'s text 1 thru 26
set newName to "" & {newName, "2", suffix}
collate {file ("" & i)} by renaming it to newName
set procCount to procCount + 1
else
error {n, m}
end if
end try
end if
end repeat
use cursor 0
set tickTot to tick1 + ((system value lm ticks) - tick2)
showDone(xlstCount, procCount, "Added ", "to ", tickTot)
end addSuffix
to removeSuffix(xlst, xlstCount, tick1)
set tick2 to system value lm ticks
set procCount to 0
use cursor -1
repeat with i in xlst
set theInfo to basic info for i
set {type, fileName} to {((theInfo)'s file type), ((theInfo)'s
name)}
set suffix to REMatch typeSufx pattern (type & " == (.+)") using
"\\1"
if (fileName's length) > 3 then --<- dodgey filter
if (text -1 thru -4 of fileName) = suffix then
set fileName to (text 1 thru -5 of fileName)
try
collate {file ("" & i)} by renaming it to (fileName)
set procCount to procCount + 1
on error m number n
if n = -48 then --<- duplicate filename
collate {file ("" & i)} by renaming it to (fileName)
& "2"
set procCount to procCount + 1
else
error {n, m}
end if
end try
end if
end if
end repeat
use cursor 0
set tickTot to tick1 + ((system value lm ticks) - tick2)
showDone(xlstCount, procCount, "Removed ", "from ", tickTot)
end removeSuffix
to showDone(xlstCount, procCount, procName, toFrom, tickTot)
set bla to "" & {procName, procCount, " suffixes ", toFrom, xlstCount, "
files in ", tickTot, " ticks"}
display dialog bla giving up after 5
set the clipboard to bla
end showDone
-- how to animate that cursor?
_____________________________ Andy