Re: script to rename files (2)
Re: script to rename files (2)
- Subject: Re: script to rename files (2)
- From: has <email@hidden>
- Date: Wed, 7 Aug 2002 16:11:27 +0100
[Muttering darkly about snotty list depriving poor readers of my many gems
of wisdom, just because I go a few (dozen) KB over maximum length on
occasion...;p]
p.s. I cooked up the following bit of code for my own amusement, but
someone out there might find it useful. It turns a tab-delimited table
string into an AS object; you can then query each line in turn by calling
the object's nextEntry() method.
======================================================================
on newTableReader(tableString)
(*
Creates a new tableReader object when passed a tab-delimited
table (string), where each row of the string is of form:
[value1][tab][value2].
The tableReader object contains three methods:
countRows() -- returns number of rows in table
nextRow() -- returns the next row in table. Note: empty or
invalid rows generate errors 2001 and 2002 respectively;
the user should trap and handle these errors as required.
startOver() -- start over again at the first entry
*)
if tableString's class is not string then error "Not a string."
[NO-BREAK]number -1703
script -- tableReaderObject
property _table : tableString
property _rowCounter : 0
on countRows()
return count of _table's paragraphs
end countRows
on nextRow()
set _rowCounter to _rowCounter + 1
try
set tableRow to _table's paragraph _rowCounter
on error number -1728
error "End of table." number 2000
end try
--
set oldTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to tab
if (count of tableRow's text items) is not 2 then --validate
[NO-BREAK]row (should have 2 entries)
considering punctuation, hyphens and white space --[for
[NO-BREAK]safety, just in case user is ignoring these]
if tableRow is "" then
error "Empty table row (line " & _rowCounter & ")."
[NO-BREAK]number 2001
else
error "Invalid table row (line " & _rowCounter &
[NO-BREAK]")." number 2002
end if
end considering
end if
set value1 to tableRow's first text item
set value2 to tableRow's last text item
set AppleScript's text item delimiters to oldTID
return {value1, value2}
end nextRow
on startOver()
set _rowCounter to 0
return
end startOver
end script
end newTableReader
--TEST CODE
--generate a test table
set testTable to ""
repeat with x from 1 to 10 by 2
set testTable to testTable & x & tab & (x + 1) & return
end repeat
log testTable
--make new table object for testTable
set conversionTableObj to newTableReader(testTable)
--do something with each entry in the table
try
repeat --until table object runs out of entries
try
set {fromValue, toValue} to conversionTableObj's nextRow()
--DO STUFF HERE
log {fromValue, toValue} --(test)
--
on error number 2001 --ignore empty table rows and continue
end try
end repeat
on error number 2000 --exit at end of table
return
end try
======================================================================
[formatted using ScriptToEmail - gentle relief for mailing list pains]
[
http://files.macscripter.net/ScriptBuilders/ScriptTools/ScriptToEmail.hqx]
--
(My email address has changed from <email@hidden> to
<email@hidden>. Please update your address books accordingly.)
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.