Here I am.
--[SCRIPT insertQuotes]
(* Save this script as a … Script in the Folder Actions Scripts folder
<startupVolume>:Library:Scripts:Folder Action Scripts:
Attach this Folder Actions script to the folder receiving youd downloaded quotes.csv files.
If you don't know the way to use Folder Actions Scripts, look at the chapter entitled
"Running an automation when a folder is changed" in the Finder's Help.
When the script is attached to the folder, it will be triggered each time a file is added to the folder.
If some conditions are verified, the script deciphers the file's contents and insert the grabbed values in a Numbers '09 document.
These conditions are:
Numbers '09 must be running.
The added file must be named "quotes.csv".
A document whose named is defined in the property myDoc must be open in Numbers.
The document must contain a sheet whose name is defined in the property mySheet.
The document must contain in this sheet a table whose name is defined in the property myTable.
The first cell of the destination area is defined by the properties columnNum1 and rowNum1.
If the table is too small to store the download datas, it's enlarged accordingly.
CAUTION, only one script may be active when attached to a folder.
If you attach several of them, only the last attached one will be triggered.
If you need several active scripts of this kind you must attach a single script
which will dispatch files to other scripts given the received items.
It's out of the range of this thread but the script is ready to do that.
Yvan KOENIG (Vallauris FRANCE)
29 janvier 2009
*)
--=====
(* Edit these five properties to fit your needs *)
property myDoc : "My quotes.numbers"
property mySheet : "quotes"
property myTable : "quotes"
property columnNum1 : 5
property rowNum1 : 8
--=====
property quotesFile : "quotes.csv" (* this name is the one use by the Hyperlink *)
--=====
(*
-- for tests
set this_folder to "Macintosh HD:Users:yvankoenig:Desktop:Téléchargés:"
set theFile to this_folder & "quotes.csv"
my itsAquoteFile(theFile, this_folder)
*)
on adding folder items to this_folder after receiving added_items
set theFile to (item 1 of added_items) as text (*Grab the pathname of first added item *)
tell application "System Events"
set maybe to name of disk item theFile (* Grabs it's name *)
set theProcesses to title of application processes
end tell
if (theProcesses contains "Numbers") and (maybe is quotesFile) then
my itsAquoteFile(theFile, this_folder as text) (* Bingo, two conditions are already satisfied *)
else
(* here you may add other tasks *)
end if
end adding folder items to
--=====
on itsAquoteFile(leFichierCSV, leDossier)
tell application "Numbers"
activate
set lesDocuments to name of every document
end tell
if lesDocuments contains myDoc then (* if myDoc is open it's really an automated download *)
tell application "Numbers" to set lesFeuilles to name of every sheet of document myDoc
if lesFeuilles contains mySheet then (* OK, the document contains the wanted sheet *)
tell application "Numbers" to set lesTables to name of every table of sheet mySheet of document myDoc
if lesTables contains mySheet then (* OK, the document contains the wanted table *)
if character 2 of (0.5 as text) is "." then
set deci to "."
else
set deci to ","
end if
set delim to ","
set liste1 to paragraphs of (read file leFichierCSV from 1)
set liste2 to {}
repeat with p in liste1
if (p as text) is not "" then copy my decoupe(p, delim) to end of liste2
end repeat
set rowsCount to count of liste2
set columnsCount to count of item 1 of liste2
tell application "Numbers"
tell document myDoc to tell sheet mySheet to tell table myTable
set nbRows to row count
set nbColumns to column count
if rowNum1 + rowsCount - 1 > nbRows then
repeat (rowNum1 + rowsCount - 1 - nbRows) times
add row below row nbRows
end repeat
end if
if columnNum1 + columnsCount - 1 > nbColumns then
repeat (columnNum1 + columnsCount - 1 - nbColumns) times
add column after column nbColumns
end repeat
end if
repeat with y from 1 to rowsCount
tell row (rowNum1 - 1 + y)
repeat with x from 1 to columnsCount
try
set maybe to item x of item y of liste2
if maybe starts with quote then set maybe to text 2 thru -1 of maybe
if maybe ends with quote then set maybe to text 1 thru -2 of maybe
if maybe starts with "+" then set maybe to text 2 thru -1 of maybe
if (deci is ",") then set maybe to my remplace(maybe, ".", deci)
set value of cell (columnNum1 - 1 + x) to maybe
end try
end repeat -- x
end tell -- row
end repeat -- y
end tell -- table of sheet of document
end tell -- application
end if -- lesTables…
end if -- lesFeuilles
end if -- lesDocuments contains …
tell application "Finder" to delete file quotesFile of folder leDossier
end itsAquoteFile
--=====
on decoupe(t, d)
local l
set AppleScript's text item delimiters to d
set l to text items of t
set AppleScript's text item delimiters to ""
return l
end decoupe
--=====
on remplace(t, d1, d2)
local l
set AppleScript's text item delimiters to d1
set l to text items of t
set AppleScript's text item delimiters to d2
set t to l as text
set AppleScript's text item delimiters to ""
return t
end remplace
--=====
--[/SCRIPT]
Of course, I ask for 10% of the quotes incomes 😉
Yvan KOENIG (from FRANCE jeudi 29 janvier 2009 20:13:38)