Hi pavilion.
Here’s a slight reworking of your adaptation. It’s not been tested as my test document’s still set up for columns B, G, and H. But I’ve looked at it very hard.
Your doColumnF() and doColumnI() handlers now just fetch the values from their relevant columns and pass them to a common doPhoneStuff() handler. (Feel free to change the handler names if you don’t like them!) doColumnD()'s repeat only needs to span rowCount items (as opposed to doColumnsGAndH()'s rowCount * 2), so it’s a bit simpler.
By the way, I see you’re posting your AppleScript code indented by 4 spaces to get a window for it. If instead you put groups of three backticks on separate lines above and below it, the forum software will render it with a button that can be clicked to open it in people’s default script editors, eg.:
```
AppleScript code here.
```
Anyway. See how this goes:
use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
use scripting additions
on main()
tell application "Numbers"
activate
set theTable to document 1's sheet 1's table 1
set rowCount to theTable's row count
end tell
doPhoneColumn("F", theTable, rowCount)
doPhoneColumn("I", theTable, rowCount)
doColumnsGAndH(theTable, rowCount)
doSingleColumn("D", "Monday", "Sunday", theTable, rowCount)
end main
on doPhoneColumn(columnLetter, theTable, rowCount)
set cellRange to columnLetter & "1:" & columnLetter & rowCount
set columnValues to selectAndGetValuesFromRange(theTable, cellRange)
-- Handle phone column values as a single (tab & linefeed)-delimited text.
-- (The tab's to make Numbers not take spaces for column separators.)
set columnText to join(columnValues, tab & linefeed) & tab -- NEW: TAB AT THE END AS WELL.
set columnText to current application's class "NSMutableString"'s stringWithString:(columnText)
tell columnText to replaceOccurrencesOfString:("(?m)^(\\d{3})\\.?(\\d{3})\\.?(\\d{4})(?=\\t$)") withString:("($1) $2-$3") ¬
options:(current application's NSRegularExpressionSearch) range:({0, its |length|()})
tell columnText to replaceOccurrencesOfString:("missing value") withString:("") options:(0) range:({0, its |length|()})
tell columnText to replaceOccurrencesOfString:space withString:(character id 160) options:(0) range:({0, its |length|()})
pasteIntoNumbers(columnText as text)
end doPhoneColumn
on doColumnsGAndH(theTable, rowCount)
script o
property ghValues : missing value
end script
set o's ghValues to selectAndGetValuesFromRange(theTable, "G1:H" & rowCount)
-- Edit the values individually.
repeat with r from 1 to rowCount
set {gValue, hValue} to o's ghValues's items (r + r - 1) thru (r + r)
if (gValue is missing value) then
set gValue to ""
else if (gValue contains "Messaging") then
set gValue to "Text"
end if
if (hValue is missing value) then
set hValue to ""
else if (hValue contains "in") then
set hValue to "Incoming"
else if (hValue contains "out") then
set hValue to "Outgoing"
end if
-- Store each pair as a tab-joined text in a reused slot in o's ghValues.
set o's ghValues's item r to (gValue & tab) & (hValue & tab) -- NEW: TAB AT THE END AS WELL.
end repeat
-- Coerce the stored texts to a single linefeed-delimited one and paste.
set ghText to join(o's ghValues's items 1 thru rowCount, linefeed)
pasteIntoNumbers(ghText)
end doColumnsGAndH
on doSingleColumn(columnLetter, searchText, replacementText, theTable, rowCount)
script o
property columnValues : missing value
end script
set cellRange to columnLetter & "1:" & columnLetter & rowCount
set o's columnValues to selectAndGetValuesFromRange(theTable, cellRange)
-- Edit the values individually.
repeat with r from 1 to rowCount
set cellValue to o's columnValues's item r
if (cellValue is missing value) then
set cellValue to ""
else if (cellValue contains searchText) then
set cellValue to replacementText
end if
-- Append a tab and store the item back in o's columnValues.
set o's columnValues's item r to cellValue & tab
end repeat
-- Coerce the stored texts to a single linefeed-delimited one and paste.
set columnText to join(o's columnValues, linefeed)
pasteIntoNumbers(columnText)
end doSingleColumn
on join(lst, delim)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to delim
set txt to lst as text
set AppleScript's text item delimiters to astid
return txt
end join
on selectAndGetValuesFromRange(theTable, cellRange)
tell application "Numbers"
set theTable's selection range to theTable's range cellRange
return theTable's selection range's cells's formatted value
end tell
end selectAndGetValuesFromRange
on pasteIntoNumbers(txt)
set the clipboard to txt
tell application "System Events"
set frontmost of application process "Numbers" to true
keystroke "v" using {shift down, option down, command down}
end tell
delay 1 -- Allow time for the paste to complete. (Adjust if/as necessary.)
end pasteIntoNumbers
main()