Solved

Passing an argument to AppleScript handler results in error

Posted on 2010-08-13
7
660 Views
Last Modified: 2012-05-10
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
Comment
Question by:et01267
  • 4
  • 3
7 Comments
 
LVL 18

Expert Comment

by:SimonAdept
Comment Utility
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
 
LVL 8

Author Comment

by:et01267
Comment Utility
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
 
LVL 18

Expert Comment

by:SimonAdept
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 8

Author Comment

by:et01267
Comment Utility
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
 
LVL 18

Accepted Solution

by:
SimonAdept earned 500 total points
Comment Utility
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
 
LVL 18

Expert Comment

by:SimonAdept
Comment Utility
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
 
LVL 8

Author Closing Comment

by:et01267
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

In this article we will discuss some EI Capitan Mail app issues and provide some manual process to resolve them.
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now