Thursday, July 9, 2020

#1 2009-11-26 10:08:09 pm

gdavis0007
Member
Registered: 2009-11-20
Posts: 2

Select a Cell in iWork Numbers to Paste a Photo Using System Events

How do you select a cell in Numbers using Applescript.

I wish to paste several hundred image files into cells in Numbers '09.  I believe this cannot be done directly using commands from the Numbers Library but it it is possible using System Events keystroke-paste a photo from the Clipboard. I can get the images from the photo files to clipboard. I can paste them to Numbers but I cannot figure out how to select a given cell in Numbers to paste a photo.

In the script below I kludged a solution where I manually select a cell, thereafter the System Event command Keystroke Return will choose the next cell in the column

Applescript:


--before running select the top cell in the column for the photos. This will not work unless a cell in manually selected

try
   set the clipboard to (read (choose file with prompt "Select an image file:" without invisibles) as TIFF picture)
end try

tell application "Numbers"
   activate
   tell document 1 to tell sheet 1
       tell table "Photos"
           repeat with i from 1 to 5
               set value of cell i of column 1 to "test3"
               tell application "System Events"
                   keystroke "v" using {command down}
                   keystroke return
               end tell
           end repeat
       end tell
   end tell
end tell



Filed under: keystroke, System, numbers, iWorks, '09

Offline

 

#2 2009-11-27 05:05:25 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4544

Re: Select a Cell in iWork Numbers to Paste a Photo Using System Events

This one will do the trick:

Applescript:



--before running select the top cell in the column for the photos. This will not work unless a cell in manually selected

try
   set the clipboard to (read (choose file with prompt "Select an image file:" without invisibles) as TIFF picture)
end try

tell application "Numbers"
   activate
   tell document 1 to tell sheet 1
       tell table "Photos"
           repeat with i from 1 to 5
               set value of cell i of column 1 to "test3"
               set nameOfCell to name of cell i of column 1 -- ADDED
               set selection range to range nameOfCell -- ADDED
               tell application "System Events"
                   keystroke "v" using {command down}
                   keystroke return
               end tell
           end repeat
       end tell
   end tell
end tell

Yvan KOENIG (VALLAURIS, France) vendredi 27 novembre 2009 12:05:12


Filed under: System, numbers

Offline

 

#3 2009-11-27 09:38:27 am

gdavis0007
Member
Registered: 2009-11-20
Posts: 2

Re: Select a Cell in iWork Numbers to Paste a Photo Using System Events

Fantastic.  Thanks much. Also thanks for you many posts and your script samples on mobileme. I think there is a bit of yours herein.    Below is the completed script. Not elegant but it works ok.

Applescript:


(*
This is a basic script that copies selected filenames and images/photos as cell backgrounds into two columns within iWorks Numbers '09. Be careful as it does not limit the file type chosen. It does not check to see if a table named "photos" already exists. *)


--====== Choose Files ======
set theFiles to (choose file with prompt "Select the image files:" with multiple selections allowed)
set theCount to count theFiles

--==== Make the Table =====
tell application "Numbers"
   activate
   
   if not (exists document 1) then
       display dialog "There is no document open." buttons {"Cancel"} default button 1
   end if
   
   -- make the table
   tell document 1
       tell sheet 1
           set this_table to make new table with properties {name:"Photos", column count:5, row count:(theCount + 1)}
           tell this_table
               -- set any global cell properties
               set the height of every row to 50
               set the width of every column to 75
               set the vertical alignment of every row to center
               set the alignment of every row to center
           end tell
       end tell
   end tell
end tell

--===== Main Routine - select the cell, copy file image to clipboard, activate system events to paste the clipboard into the selected cell =====

tell application "Numbers"
   activate
   
   repeat with i from 1 to theCount
       -- gets the file information into variables and clipboard
       set thisFile to item i of theFiles
       tell application "Finder" to set file_name to (name of thisFile)
       set the clipboard to (read (thisFile) as TIFF picture)
       -- pastes information into Numbers
       tell this_table
           set value of cell (i + 1) of column 1 to i
           set value of cell (i + 1) of column 2 to file_name
           set nameOfCell to name of cell (i + 1) of column 3
           set selection range to range nameOfCell
           tell application "System Events"
               keystroke "v" using {command down}
           end tell
       end tell
   end repeat
end tell



--===== Makes sure UI Elements is enabled to use System Events

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

--=====



Filed under: Finder, System, numbers

Offline

 

#4 2009-11-27 10:30:51 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4544

Re: Select a Cell in iWork Numbers to Paste a Photo Using System Events

Thanks for the feedback.

The scripts which I uploaded on my iDisk are all mine (except perhaps one or two).

Yvan KOENIG (VALLAURIS, France) vendredi 27 novembre 2009 17:30:21

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)