Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Passing an argument to AppleScript handler results in error

Posted on 2010-08-13
7
Medium Priority
?
702 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:Simon
ID: 33430544
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
ID: 33431020
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:Simon
ID: 33431136
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 8

Author Comment

by:et01267
ID: 33431296
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:
Simon earned 2000 total points
ID: 33435783
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:Simon
ID: 33435794
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
ID: 33436367
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
Article by: Justin
In light of the WannaCry ransomware attack that affected millions of Windows machines, you might wonder if your Mac needs protecting. Yes, it does and here is how to do it.
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

971 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