AppleScript C Perl Shell Xcode Other

Replacing records in "Numbers" table

Post Reply
coding / applescript     Views: 3281Prev .. Next
Replacing records in "Numbers" tablePosted: Friday, August 27, 2010 [20:58:07] - 1
rootPosted by:rootMember Since:
June 16 2010
Posts: 356
I have a need to update records from plain text file on a daily basis.
Honestly, got tired of it, and decided to make a small applescript that'll do it automatically. I saved the script as an application and dropped it on a dock. Now all I have to do is get a text file in-place and click on an app.
Here is an applescript:
View Code-- path to Numbers file that needs to be updated
-- adjust path to your Numbers file accordingly
set p to "/current.numbers"
set a to POSIX file p
-- open Numbers file - calling a subroutine
doOpenDocument(a)

-- a little of embedded Perl script that'll read the plain text file - /current.txt
-- adjust path to your text file accordingly
set bList to do shell script "perl -e '$d = `cat /current.txt`;@w=split(/\\n/,$d);foreach $l(@w){if($l =~ m/\\bABA\\b/){next;}print\"$b$l\";$b=\";;\";}'"
-- create an array. each element equals one line in a text file
set myArray to splitData(";;", bList)

-- now getting a number of lines in text file (records separated by Tab and it includes the Header row
-- so Perl cuts the header row out and returns clean number of lines
set nOfLines to do shell script "perl -e '$d = `cat /current.txt`;$d=~ s/\\n+$//;$nof = $d =~ s/\\n/$&/g;print\"$nof\";'"

if nOfLines > 0 then
tell application "Numbers"
set numOfRows to 0
tell first document
tell first sheet
tell first table
-- select all tablecells
set selection range to range (name of first cell of first row & ":" & name of last cell of last row as string)
repeat with r from 1 to the count of rows
set numOfRows to numOfRows + 1
if numOfRows > 1 then
if (exists row numOfRows) then
if numOfRows > 2 then
remove row numOfRows
else
tell application "System Events" to key code 51
end if
end if
end if
end repeat
set selection range to last row
end tell
end tell

tell application "Numbers" to activate
set wrRaw to 1
tell first sheet
tell first table
repeat with RowNmb in myArray
set wrRaw to wrRaw + 1
--set RowNmb to (the clipboard) as text
tell application "Finder" to set the clipboard to the RowNmb as text
if wrRaw is equal to 2 then
tell application "System Events" to keystroke "v" using {command down}
else
add row below last row
set selection range to last row
tell application "System Events" to keystroke "v" using {command down}
end if
delay 1
end repeat
end tell
end tell
end tell
end tell
end if

on doOpenDocument(theDocument)
tell application "Numbers"
open (theDocument as alias)
if not (exists first document) then
display dialog "There is no document opened" buttons {"Cancel"} default button 1
end if
tell first document
tell first sheet
if not (exists table 1) then
display dialog "The document contains no table" buttons {"Cancel"} default button 1
end if
end tell
end tell
end tell
end doOpenDocument

on splitData(delimeter, splText)
set AppleScript's text item delimiters to delimeter
set output to text items of splText
return output
end splitData

That is itThere's no place like ~
coding / applescriptPrev .. Next
 
Post Reply
Home - Coding: AppleScript C Perl Shell Xcode Other
Our Telegram Group