Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Passing an argument to AppleScript handler results in error

Posted on 2010-08-13
7
Medium Priority
?
686 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Create the perfect environment for any meeting

You might have a modern environment with all sorts of high-tech equipment, but what makes it worthwhile is how you seamlessly bring together the presentation with audio, video and lighting. The ATEN Control System provides integrated control and system automation.

 
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Do you come here a lot? Are you lazy like me and don't want to go through the "trouble" of having to click your Dock's Safari icon and then having to click your Experts Exchange Favorites bookmark to get here? Well then this article is for you.
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.
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

721 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