[Date Prev][Date Next] [Thread Prev][Thread Next]
[Date Index] [Thread Index] [New search]

Re: set contents of cells by AppleScript



At 4:35 PM -0500 13/02/01, Larry Kollar wrote:
>The following snippet appends " hi" to the text of each cell in
>the selection -- I just tried it, and it works. You would probably
>want to wrap the "tell document" in a "try/on error" construct for
>error checking.
>
>(* begin code snippet - MacOS 8.5.1 *)
>
>set AppendText to " hi"
>
>tell application "FrameMaker+SGML 6.0"
>
>	tell active document
>		set myCells to selection
>	end tell
>
>	-- Append text to end of each cell
>	set myData to (get text of myCells)
>	set n to (count myData) -- get # of cells
>
>	repeat with i from 1 to n
>	   set text of (cell i of myCells) to (item i of myData) & AppendText
>	end repeat
>
>end tell

  I think your code works fine. But it was not clear from the original 
post if the person wanted to delete the original text and replace it, 
or just add to it. And if it's multi-paragraph text in the cell, what 
he wanted to do with that.


>Perhaps Bill Briggs can weigh in with a better way of doing this, too.

  Sorry for the long delay. I've been swamped and just had a chance to 
look at this now.

  I don't think there's a "better" way. A few different ways though. 
Here's another one that takes into account that you may want to place 
the text precisely (beginning or end of a paragraph), and has a 
commented out line that shows how to delete the entire contents of 
the cell.

  One other oddity that is exhibited in this script. Once you make a 
change to the first cell of the selection, the selection disappears. 
If you select the range manually as was assumed in Larry's script, 
then you need to use the reference form he did to assign the 
selection to a variable. If you make the selection in the body of the 
script based on some calculation, then you can use this kind of 
approach to "retain" (actually re-select) the cells within the 
script. It's a bit goofy, but it works with no problem. Here are 
three variations on the theme.

tell application "FrameMaker 6.0"
   tell document 1
    select last column of rows 2 through 4 of second table of text flow "A"
    set myData to (get text of every cell of selection)
    set cellCount to count of myData
    repeat with j from 1 to (cellCount)
    select last column of rows 2 through 4 of second table of text flow "A"
    --delete text of cell j of selection -- or not
    set end of paragraph 1 of cell j of selection to "x"
   end repeat
  end tell
end tell

----- or -----

tell application "FrameMaker 6.0"
   tell document 1
    select last column of rows 2 through 4 of second table of text flow "A"
    set sel to selection
    set myData to (get text of every cell of selection)
    set cellCount to count of myData
    repeat with j from 1 to (cellCount)
     select sel
     --delete text of cell j of selection -- or not
    set end of paragraph 1 of cell j of selection to "x"
    end repeat
   end tell
end tell

----- or -----

tell application "FrameMaker 6.0"
   tell document 1
    select last column of rows 2 through 4 of second table of text flow "A"
       set sel to selection
       set myData to (get text of every cell of selection)
       set cellCount to count of myData
      repeat with j from 1 to (cellCount)
     --delete text of cell j of selection -- or not
    set end of paragraph 1 of cell j of sel to "x"
   end repeat
   end tell
end tell

** To unsubscribe, send a message to majordomo@omsys.com **
** with "unsubscribe framers" (no quotes) in the body.   **