AppleScript not ran on each iteration of ForEech

Get help using and writing Nisus Writer Pro macros.
Post Reply
beckonmuse
Posts: 1
Joined: 2014-02-23 18:54:06

AppleScript not ran on each iteration of ForEech

Post by beckonmuse »

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: Select all

$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
User avatar
martin
Official Nisus Person
Posts: 5228
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: AppleScript not ran on each iteration of ForEech

Post by martin »

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.
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: AppleScript not ran on each iteration of ForEech

Post by phspaelti »

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: Select all

$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
Post Reply