Reply to topic  [ 3 posts ] 
AppleScript not ran on each iteration of ForEech 
Author Message

Joined: 2014-02-23 18:54:06
Posts: 1
In my script below, the applescript is only executed for the first table found in the document and then no longer is it performed thereafter for the remaining tables. Any ideas?

(BTW: "Table:Select:Table" after "$error = '' " was put there in an effort to see if that would fix my problem, but it did not.)

Code:
$doc = Document.active
$text = $doc.text
$tables = $text.tables

ForEach $index, $table in $tables
   $columns = $table.columnCount
   $rows = $table.rowCount
   $table_no = $index+1
   Select Table $table_no
   
   #Run AppleScript on each table

   $code = 'tell application "System Events"
         tell process "Nisus Writer Pro"
            click radio button 3 of radio group 1 of drawer 1 of window 1
            select text field 1 of scroll area 1 of drawer 1 of window 1
            click button 1 of incrementor 3 of scroll area 1 of drawer 1 of window 1
            click button 1 of incrementor 3 of scroll area 1 of drawer 1 of window 1
            click button 1 of incrementor 3 of scroll area 1 of drawer 1 of window 1
            click button 1 of incrementor 3 of scroll area 1 of drawer 1 of window 1
            click radio button 2 of radio group 1 of scroll area 1 of drawer 1 of window 1
            click button 1 of list 4 of scroll area 1 of drawer 1 of window 1
         end tell
          end tell'
      
   $error = ""
   Table:Select:Table
   Run AppleScript $code, $error

   #Set the appropriate style for each column
   If $columns == 2
      Select Table $table_no
      Select Table Cell 1, 1
      Table:Select:Columns
      Format:Paragraph Style:German Vocabulary

      Select Table $table_no
      Select Table Cell 1, 2
      Table:Select:Columns
      Format:Paragraph Style:English Gloss
   elseif $columns == 4
      Select Table $table_no
      Select Table Cell 1, 1
      Table:Select:Columns
      Format:Paragraph Style:German Vocabulary

      Select Table $table_no
      Select Table Cell 1, 2
      Table:Select:Columns
      Format:Paragraph Style:English Gloss

      Select Table $table_no
      Select Table Cell 1, 3
      Table:Select:Columns
      Format:Paragraph Style:German Vocabulary

      Select Table $table_no
      Select Table Cell 1, 4
      Table:Select:Columns
      Format:Paragraph Style:English Gloss
   end
End


2014-02-23 18:57:44
Profile
Official Nisus Person
User avatar

Joined: 2002-07-11 17:14:10
Posts: 4251
Location: San Diego, CA
I just did a limited test and your macro seems to be working. All tables in the document are selected and processed in turn.

However, I can't be sure what your AppleScript is designed to do exactly; the layout of palettes in the tooldrawer would affect the results. Honestly using this kind of code, where the triggered commands are dependent on the layout of the GUI, is going to be brittle. What formatting changes are you intending to make via the macro? Perhaps we can suggest a better solution.


2014-02-24 16:47:15
Profile WWW
User avatar

Joined: 2007-02-07 00:58:12
Posts: 876
Location: Japan
martin wrote:
What formatting changes are you intending to make via the macro? Perhaps we can suggest a better solution.

While I generally agree, perhaps beckonmuse is trying to adjust the padding of the table cells? That would be something that unfortunately can't be done with the Nisus macro language. [Hint to Martin: Feature request.]

Even so let me make a number of suggestions for your macro:
First: You have 4 column tables with alternating German and English columns; is that really a good idea? If you want to fully use the page, how about two column tables in a two column section instead?

Instead of running a loop through the tables formatting one table at a time, how about selecting the parts all at once, and formatting in one go? In particular, this would allow you to run the apple script just once, instead of dozens of times.
Consider this type of code. (Could still be adjusted if you want to keep 4 column tables.)
Code:
$doc = Document.active
$tables = $doc.text.tables

$germanColumn = Range.new 0,1
$englishColumn = Range.new 1,1

$german = $english = Array.new

foreach $table in $tables
    $rows = Range.new 0, $table.rowCount
    $sel = TableSelection.new($table, $rows, $germanColumn)
    $german.push $sel
    $sel = TableSelection.new($table, $rows, $englishColumn)
    $english.push $sel
end

$doc.setSelection $german
Format:Paragraph Style:German Vocabulary
$doc.setSelection $english
Format:Paragraph Style:English Gloss

$doc.setSelection $tables
# Run Apple script to adjust padding here

_________________
philip


2014-02-24 18:05:13
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 3 posts ] 

Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software