Bento, iwork 09 Numbers & a Folder Action

Hello,

I believe I’ve figured out a way around some of the limitations of Bento and Numbers integration.
Enough for me to be happy anyway. But before I can even get to the fine tuning stage, I’m running into some trouble.

Here’s the plan.

I use Bento as my front end, client data input form etc.
I then export the database as a Numbers file to a SPECIFIC folder.
This folder contains a folder action that open the numbers file, copies the contents and closes the file.
Next, the folder action opens another SPECIFIC numbers file, pastes its contents and saves.

So far, I found a script by Yvan Koenig, that copies, and closes. It works great on its own. But for some reason, it’s not working as a folder action.

Any assistance would be greatly appreciated.

Thanks in advance,

Darjon

I wrote too many scripts to be able to guess which one you are trying to use.
If you give the reference to the original script I guess that it would be easier to help you.

Yvan KOENIG (VALLAURIS, France) mardi 27 septembre 2011 11:50:36

The script’s filename is open&copy.

Hello.

I wrote a new one from scratch based upon what you wrote.


--[SCRIPT dropped_to_Numbers ]
(*
Yvan KOENIG (VALLAURIS, France)
2011/09/27
*)

(*
Useful tip to check the script's behavior.

my main((path to desktop as text) & "dossier sans titre:", {(path to desktop as text) & "dossier sans titre:DOW_JONES_20110916_224630.numbers"})
on main(this_folder, added_items)
*)
on adding folder items to this_folder after receiving added_items
	
	local fichierSource, nbd, nomDestination, fichierDestination
	
	my activateGUIscripting()
	(*
Get the pathname of the first dropped document	*)
	set fichierSource to item 1 of added_items
	(*
Check that it's a Numbers document. Exit silently if itsn't. *)
	try
		tell application "System Events"
			(get type identifier of disk item (fichierSource as text))
		end tell
		if result is not "com.apple.iwork.numbers.numbers" then error number -128
	on error
		error number -128
	end try
	(*
Open the dropped spreadsheet *)
	tell application "Numbers"
		set nbd to count of documents
		open fichierSource
		repeat while nbd = (count of documents)
			--
		end repeat
		(*
Define the range of cells to copy.
As I don't know what must be copied ,I don't trigger Select All. *)
		tell document 1 to tell sheet 1 to tell table 1
			set selection range to range ("A1 : " & name of last cell)
		end tell
		my raccourci("Numbers", "c", "c") (* Copy *)
		close document 1 without saving
	end tell -- Numbers
	(*
Define the name of the destination file *)
	set nomDestination to "SPECIFIC.numbers"
	(*
Without detailed info, I assume that the target file is in this folder *)
	set fichierDestination to (this_folder as text) & nomDestination
	(*
If the target document doesn't exist, create it. *)
	tell application "System Events"
		if not (exists disk item fichierDestination) then
			set fichierDestination to (my makeNewIworkDoc("Numbers", nomDestination, this_folder as text)) as text
		end if
	end tell
	
	tell application "Numbers"
		if name of documents contains nomDestination then
			(*
The target document is already open, re-open it to put it at front *)
			open fichierDestination
		else
			(*
		The target document wasn't open, open it *)
			set nbd to count of documents
			open fichierDestination
			repeat while nbd = (count of documents)
				--
			end repeat
		end if -- name of documents
		(*
Define the 1st cell of the target area. 
Without any info about that, I paste in B2. *)
		tell document 1 to tell sheet 1 to tell table 1
			set selection range to range "B2"
		end tell
		my raccourci("Numbers", "v", "c") (* Copy *)
		close document 1 with saving
	end tell -- Numbers
	
end adding folder items to
--end main

--=====

on parleAnglais()
	local z
	try
		tell application "Numbers" to set z to localized string "Cancel"
	on error
		set z to "Cancel"
	end try
	return (z is not "Annuler")
end parleAnglais

--=====
(*
Creates a new iWork document from the Blank template,
saves it and returns its pathname.
example:
set myNewDoc to my makeNewIworkDoc(theApp, docName, folderPath)
 *)
on makeNewIworkDoc(the_app, doc_name, doc_folder)
	local maybe, nb_doc, path_to_the_App, path_to_the_doc
	if the_app is "Pages" then
		tell application "Pages"
			set nb_doc to count of documents
			make new document with properties {template name:item 1 of templates}
		end tell
	else if the_app is "Numbers" then
		tell application "System Events" to set maybe to the_app is in title of every application process
		if not maybe then tell application the_app to activate
		tell application "System Events"
			set path_to_the_App to get application file of application process the_app
		end tell
		
		tell application the_app
			set nb_doc to count of documents
			open ((path_to_the_App as text) & "Contents:Resources:Templates:Blank.nmbtemplate:")
		end tell
	else
		if my parleAnglais(the_app) then
			error "The application "" & the_app & "" is not accepted !"
		else
			error "l'application « " & the_app & " » n'est pas gérée !"
		end if
	end if
	
	if doc_name does not end with "." & the_app then set doc_name to doc_name & "." & the_app
	if doc_folder ends with ":" then
		set path_to_the_doc to doc_folder & doc_name
	else
		set path_to_the_doc to doc_folder & ":" & doc_name
	end if
	tell application the_app
		repeat until (count of documents) > nb_doc
			delay 0.1
		end repeat
		save document 1 in file path_to_the_doc
	end tell -- the_App
	return path_to_the_doc as alias
end makeNewIworkDoc

--=====

on activateGUIscripting()
	(* to be sure than GUI scripting will be active *)
	tell application "System Events"
		if not (UI elements enabled) then set (UI elements enabled) to true
	end tell
end activateGUIscripting

--=====
(*
==== Uses GUIscripting ==== 
*)
(*
This handler may be used to 'type' text, invisible characters if the third parameter is an empty string. 
It may be used to 'type' keyboard raccourcis if the third parameter describe the required modifier keys. 

I changed its name « shortcut » to « raccourci » to get rid of a name conflict in Smile. 
*)
on raccourci(a, t, d)
	local k
	tell application a to activate
	tell application "System Events" to tell application process a
		set frontmost to true
		try
			t * 1
			if d is "" then
				key code t
			else if d is "c" then
				key code t using {command down}
			else if d is "a" then
				key code t using {option down}
			else if d is "k" then
				key code t using {control down}
			else if d is "s" then
				key code t using {shift down}
			else if d is in {"ac", "ca"} then
				key code t using {command down, option down}
			else if d is in {"as", "sa"} then
				key code t using {shift down, option down}
			else if d is in {"sc", "cs"} then
				key code t using {command down, shift down}
			else if d is in {"kc", "ck"} then
				key code t using {command down, control down}
			else if d is in {"ks", "sk"} then
				key code t using {shift down, control down}
			else if (d contains "c") and (d contains "s") and d contains "k" then
				key code t using {command down, shift down, control down}
			else if (d contains "c") and (d contains "s") and d contains "a" then
				key code t using {command down, shift down, option down}
			end if
		on error
			repeat with k in t
				if d is "" then
					keystroke (k as text)
				else if d is "c" then
					keystroke (k as text) using {command down}
				else if d is "a" then
					keystroke k using {option down}
				else if d is "k" then
					keystroke (k as text) using {control down}
				else if d is "s" then
					keystroke k using {shift down}
				else if d is in {"ac", "ca"} then
					keystroke (k as text) using {command down, option down}
				else if d is in {"as", "sa"} then
					keystroke (k as text) using {shift down, option down}
				else if d is in {"sc", "cs"} then
					keystroke (k as text) using {command down, shift down}
				else if d is in {"kc", "ck"} then
					keystroke (k as text) using {command down, control down}
				else if d is in {"ks", "sk"} then
					keystroke (k as text) using {shift down, control down}
				else if (d contains "c") and (d contains "s") and d contains "k" then
					keystroke (k as text) using {command down, shift down, control down}
				else if (d contains "c") and (d contains "s") and d contains "a" then
					keystroke (k as text) using {command down, shift down, option down}
				end if
			end repeat
		end try
	end tell
end raccourci

--=====
--[/SCRIPT]

Yvan KOENIG (VALLAURIS, France) mardi 27 septembre 2011 13:53:29

Wow Yvan!

Thank you very much!
I did not expect an entirely new customized script.
I can’t wait to try it out.

Thanks again,

Darjon