Probably. ![:wink: :wink:](/images/emoji/apple/wink.png?v=12)
-- Unless you're working with a *really huge file*, it's usually easier
-- to read the entirety of a file's contents into memory:
--
-- set dataFile to choose file
--
-- set fileContent to read dataFile
-- debugging:
--
set fileContent to "A1B2C3D45678,HCC107-3024,HCC107-3024,,,,,,,,,,,
1A2B3C4D5678,HCC107-4581,HCC107-4581,,,,,,,,,,,
12A3B4C5D678,HCC107-4582,HCC107-4582,,,,,,,,,,,
123A4B5C6D78,HCC107-4583,HCC107-4583,,,,,,,,,,,"
-- Before we break the text apart, we should save the line-ending
-- character that the file uses:
--
try
set lineEnding to character (1 + (length of paragraph 1 of fileContent)) of fileContent
on error -- the file contained only one line
set lineEnding to ""
end try
-- We want to remove a final line ending character before getting
-- "every paragraph"
--
if (character -1 of fileContent = lineEnding) then ¬
set fileContent to text 1 thru -2 of fileContent
-- Each line is a record:
--
set fileContent to every paragraph of fileContent
--
-- { "A1B2C3D45678,HCC107-3024,...", "1A2B3C4D5678,HCC107-4581", ... }
-- Each comma delimits a Cell of data:
--
set astids to AppleScript's text item delimiters
try
set AppleScript's text item delimiters to ","
repeat with i from 1 to length of fileContent
set (item i of fileContent) to every text item of (item i of fileContent)
--
--> { "A1B2C3D45678", "HCC107-3024", ... }
end repeat
set AppleScript's text item delimiters to astids
on error e number n from f to t partial result p
set AppleScript's text item delimiters to astids
error e number n from f to t partial result p
end try
fileContent
--
--> { { "A1B2C3D45678", "HCC107-3024", ... }, { "1A2B3C4D5678", "HCC107-4581", ... }, ... }
-- We want to show the user a modified version of the first record.
-- We use "copy" rather than "set" to protect the original data:
--
copy item 1 of fileContent to userSelect
-- Modify the cells such that they indicate their position in
-- the record:
--
repeat with i from 1 to length of userSelect
-- Zero-pad the index. This will make parsing the user's
-- choice easier later on:
--
set num to text -2 thru -1 of ("0" & i) -- ie: "01", "02", ..., "10", ...
set item i of userSelect to "Cell " & num & " : " & (item i of userSelect)
end repeat
userSelect
--
--> { "Cell 01 : A1B2C3D45678", "Cell 02 : HCC107-3024", ... }
set userChoice to choose from list (userSelect) ¬
with prompt ("Please select cells whose case is to be changed.") ¬
cancel button name ("Quit") ¬
OK button name ("Change Case...") ¬
multiple selections allowed (true) ¬
empty selection allowed (false)
-- userChoice is either:
-- false User selected the "Quit" button.
-- { "Cell XX : ...", ... } User's choices.
--
-- Note: If we had specified "empty selection allowed (true)", then
-- the value returned might also be an empty list.
--
if (userChoice = false) then return -- OR quit OR error number -128
-- Now, let's ask the user what case to give each cell:
--
repeat with i from 1 to length of userChoice
set displayCell to item i of userChoice --> "Cell XX : ..."
set thisOperation to button returned of ¬
(display dialog ("How should this cell be modified?" & ¬
return & return & displayCell) ¬
buttons {"lowercase", "UPPERCASE"})
-- Let's grab the index:
--
set num to text 6 thru 7 of displayCell -- grab "XX" out of "Cell XX..."
-- Listify each userChoice:
--
set item i of userChoice to {num as number, thisOperation}
--
--> ie: { 5, "lowercase" }
end repeat
userChoice
--
--> { { 5, "lowercase" }, { 7, "UPPERCASE" }, ... }
repeat with i from 1 to length of fileContent
set oneRecord to item i of fileContent
repeat with j from 1 to length of userChoice
set {num, op} to item j of userChoice --> ie: { 5, "lowercase" }
set oneCell to item (num) of oneRecord
if (op = "lowercase") then
set oneCell to MakeLowercase(oneCell)
else
set oneCell to MakeUppercase(oneCell)
end if
set item j of oneRecord to oneCell
end repeat
end repeat
-- All modifications have been made. We now coerce the records and cells
-- back into a string:
-- Insert the commas:
--
set astids to AppleScript's text item delimiters
set AppleScript's text item delimiters to ","
repeat with i from 1 to length of fileContent
set (item i of fileContent) to ¬
(item i of fileContent) as string
end repeat
-- Insert the returns:
--
set AppleScript's text item delimiters to lineEnding
set fileContent to fileContent as string
set AppleScript's text item delimiters to astids
fileContent
--
--> ie:
-- "hcc107-3024,hcc107-3024,HCC107-3024,,,,,,,,,,,
-- hcc107-4581,hcc107-4581,HCC107-4581,,,,,,,,,,,
-- hcc107-4582,hcc107-4582,HCC107-4582,,,,,,,,,,,
-- hcc107-4583,hcc107-4583,HCC107-4583,,,,,,,,,,,"
-- You can now write this content back to the same file:
--
-- set openedFile to open for access dataFile with write permission
-- set eof openedFile to 0
-- write fileContent to openedFile
-- close access openedFile
--
-- or to be safe, write it to another file:
--
-- set openedFile to open for access (choose file name) with write permission
-- set eof openedFile to 0
-- write fileContent to openedFile
-- close access openedFile
-- Er... sorry, I can't locate my internationally-friendly
-- version at the moment.
--
property UpperLetters : "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
property LowerLetters : "abcdefghijklmnopqrstuvwxyz"
on MakeLowercase(str)
set astids to AppleScript's text item delimiters
try
repeat with i from 1 to 26
set AppleScript's text item delimiters to character i of UpperLetters
set str to every text item of str
set AppleScript's text item delimiters to character i of LowerLetters
set str to str as string
end repeat
on error e number n from f to t partial result p
set AppleScript's text item delimiters to astids
error e number n from f to t partial result p
end try
set AppleScript's text item delimiters to astids
return str
end MakeLowercase
on MakeUppercase(str)
set astids to AppleScript's text item delimiters
try
repeat with i from 1 to 26
set AppleScript's text item delimiters to character i of LowerLetters
set str to every text item of str
set AppleScript's text item delimiters to character i of UpperLetters
set str to str as string
end repeat
on error e number n from f to t partial result p
set AppleScript's text item delimiters to astids
error e number n from f to t partial result p
end try
set AppleScript's text item delimiters to astids
return str
end MakeUppercase