• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 712
  • Last Modified:

Passing an argument to AppleScript handler results in error

I have a very simple script that is behaving weirdly.  
tell application "Microsoft Word"
	set props to properties of formatted text of text object of form field "Check1" of active document
	log props
	my displayrRecord(props)
end tell

on displayRecord(r)
	log r
end displayRecord

Open in new window


If I run this I get an error dialog:
Can’t make «class 1653» of «class 1653» of «class wTxR» of «class w177» "Check1" of «class 1003» of application "Microsoft Word" into the expected type.

But if I just comment out the call to my handler, the log statement in the main body works with no error.  The log window shows the output of the log call.  

A test word doc is attached.

My intermediate goal is to modify my handler to detect any properties that have "color" in them, and print out where they occur, and then call itself recursively to dive down to dig out color-related properties of subobjects.

My ultimate goal is to solve another question I've posed here about trying to alter the color of a checkbox: http://www.experts-exchange.com/Apple/Operating_Systems/Q_26399551.html
checkbox.docx
0
et01267
Asked:
et01267
  • 4
  • 3
1 Solution
 
SimonCommented:
Without running it in the editor I'm guessing you need to change line 3 to "tell me to log props" (The word application doesn't know what the log window is), or just comment line 3 out and let the DisplayRecord function do the logging.


Also you appear to have a typo on line 4:
my displayrRecord(props)  <-- too many 'r's in the function name.

0
 
et01267Author Commented:
Actually, the "log props" on line 3 works.  It's the call to the handler that fails.  Thanks for misspelling catch. However, even when I correct the misspelling, I get an error.  That is,

This works
[code]
tell application "Microsoft Word"
    set props to properties of formatted text of text object of form field "Check1" of active document
    log props
end tell
[/code]

but this gives me an error:
[code]
tell application "Microsoft Word"
    set props to properties of formatted text of text object of form field "Check1" of active document
    my displayRecord(props)
end tell

on displayRecord(r)
    log r
end displayRecord
[/code]
0
 
SimonCommented:
OK It could be because Applescript doesn't understand the Word classes and it's trying to log a record.

You could try
my displayrecord(props as list) --coerce the properties record to a list first, not sure if this will work.

or try

on displayRecord(r)
using terms from "Microsoft Word"
    log r
end using terms
end displayRecord

I'm not in front of a Mac at the moment, so can't test these. I'll fire up the script editor when I get home, about 2 hours from now if you're still stuck.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
et01267Author Commented:
Thanks, I'll probably still be stuck in a couple of hours, but this is mostly a lark trying to overcome the various idiocies of both MS and Apple (quite a hill to climb).

I guess you actually need EVEN MORE syntactic sugar in order for this to compile:

on displayRecord(r)
      using terms from application "Microsoft Word"
            
            log r
      end using terms from
end displayRecord

And it still gives the error.  

I think there must be something fishy about the way Applescript passes arguments.  

Also, coercing props to a list would result in only the values being passed, not the keys -- and doing this still gives the error because, I guess, some value in the record (now list) is unhappy.
0
 
SimonCommented:
Hi,

The code below works ( using 'tell  application' block within handler).

These are the results I get in the log window for your test document:

tell application "Microsoft Word"
    get properties of formatted text of text object of form field "Check1" of active document
        {class:text range, content:"", formatted text:formatted text of formatted text of text object of form field "Check1" of active document, start of content:0, end of content:17, font object:font object of formatted text of text object of form field "Check1" of active document, story type:main text story, footnote options:footnote options of formatted text of text object of form field "Check1" of active document, endnote options:endnote options of formatted text of text object of form field "Check1" of active document, show Word comments by:"All Reviewers", border options:border options of formatted text of text object of form field "Check1" of active document, shading:shading of formatted text of text object of form field "Check1" of active document, text retrieval mode:text retrieval mode of formatted text of text object of form field "Check1" of active document, field options:field options of formatted text of text object of form field "Check1" of active document, paragraph format:paragraph format of formatted text of text object of form field "Check1" of active document, list format:list format of formatted text of text object of form field "Check1" of active document, show hidden bookmarks:false, bold:false, italic:false, underline:underline none, emphasis mark:emphasis mark none, disable character space grid:false, style:Word style "Normal" of text object of form field "Check1" of active document, story length:18, language ID:english us, subdocuments expanded:false, grammar checked:true, spelling checked:true, highlight color index:no highlight, column options:missing value, row options:missing value, is end of row mark:false, bookmark id:0, previous bookmark id:0, find object:find id «class #s…¿» of formatted text of text object of form field "Check1" of active document, page setup:page setup of formatted text of text object of form field "Check1" of active document, case:next case, orientation:text orientation horizontal, next story range:missing value, language ID east asian:english us, supplemental language ID:english us, fit text width:0.0, no proofing:false, horizontal in vertical:horizontal in vertical none, two lines in one:two lines in one none, combine characters:false, range footnote options:range footnote options of formatted text of text object of form field "Check1" of active document, range endnote options:range endnote options of formatted text of text object of form field "Check1" of active document}
    (*class:text range, content:, formatted text:formatted text, start of content:0, end of content:17, font object:font object, story type:main text story, footnote options:footnote options, endnote options:endnote options, show Word comments by:All Reviewers, border options:border options, shading:shading, text retrieval mode:text retrieval mode, field options:field options, paragraph format:paragraph format, list format:list format, show hidden bookmarks:false, bold:false, italic:false, underline:underline none, emphasis mark:emphasis mark none, disable character space grid:false, style:Normal, story length:18, language ID:english us, subdocuments expanded:false, grammar checked:true, spelling checked:true, highlight color index:no highlight, column options:missing value, row options:missing value, is end of row mark:false, bookmark id:0, previous bookmark id:0, find object:find id «class #s…¿», page setup:page setup, case:next case, orientation:text orientation horizontal, next story range:missing value, language ID east asian:english us, supplemental language ID:english us, fit text width:0.0, no proofing:false, horizontal in vertical:horizontal in vertical none, two lines in one:two lines in one none, combine characters:false, range footnote options:range footnote options, range endnote options:range endnote options*)
    (*Running displayRecord(r) handler*)
    (*class:text range, content:, formatted text:formatted text, start of content:0, end of content:17, font object:font object, story type:main text story, footnote options:footnote options, endnote options:endnote options, show Word comments by:All Reviewers, border options:border options, shading:shading, text retrieval mode:text retrieval mode, field options:field options, paragraph format:paragraph format, list format:list format, show hidden bookmarks:false, bold:false, italic:false, underline:underline none, emphasis mark:emphasis mark none, disable character space grid:false, style:Normal, story length:18, language ID:english us, subdocuments expanded:false, grammar checked:true, spelling checked:true, highlight color index:no highlight, column options:missing value, row options:missing value, is end of row mark:false, bookmark id:0, previous bookmark id:0, find object:find id «class #s…¿», page setup:page setup, case:next case, orientation:text orientation horizontal, next story range:missing value, language ID east asian:english us, supplemental language ID:english us, fit text width:0.0, no proofing:false, horizontal in vertical:horizontal in vertical none, two lines in one:two lines in one none, combine characters:false, range footnote options:range footnote options, range endnote options:range endnote options*)
end tell

tell application "Microsoft Word"
	set props to properties of formatted text of text object of form field "Check1" of active document
	log props
	my displayRecord(props)
end tell

on displayRecord(r)
	log "Running displayRecord(r) handler"
	tell application "Microsoft Word"
		log r
	end tell
end displayRecord

Open in new window

0
 
SimonCommented:
BTW, regarding your other question, I can't replicate the behaviour on the test document you attached to this thread. I copied and pasted that checkbox and made some default to checked and others default to unchecked. I couldn't get any of them to turn red when I clicked them after protecting the document for forms.

0
 
et01267Author Commented:
Wow.  Applescript is pretty mysterious.  I wouldn't have guessed that you'd need a tell inside the handler -- I guess Applescript has no notion of a dynamic binding context.

Anyway, full marks.

Oh, and I'm not sure why the box turned red for me.  But I figured out that you can alter the color of a checkbox by selecting it and then changing the font (!) color.
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now