I am trying to understand how to use the Dictionary. Specifically I am trying to understand the select command in MS Word. However, searching for this command in the dictionary, I do not find it! I know, however, that it is there and that it does work. Is it simply the case that all available commands are not in the dictionary?
I find it a bit strange that I can e.g. select word 1 of current document, but I cannot select paragraph 1 of current document. Even though a selection object is supposed to be able to contain a paragraph property? As you can probably tell, I am really confused. If someone could please clear this up I would be very thankful.
Learning Applescript has turned out to be a tricky endeavor! There seem to be no good recent books to help. I had a MUCH easer time learning C and C++ so far.
What I tried to do was to select the first paragraph of the document. Something like:
tell application "Microsoft Word"
select first paragraph of active document
end tell
However, I get an error message that “paragraph 1 of active document does not understand the select message” (directly translated from Swedish).
I do not find the select command in the standard suite either. Could it be that they have “forgotten” to add it to the dictionary? I do find the other select commands you mentioned.
Sorry for the late revival of this thread, but I just realized that I did not understand what you mean. If the “select” command is in the standard suite, shouldn’t it appear in the dictionary when I choose the standards suite?
I find no reference what so ever to the command “select”. The only reason I know about it is because I found some other script using it…
Edit: Perhaps I should mention that I am using the latest Office for Mac (2016?). Could it be that the select command has been deprecated, but still works for backwards compatibility?
This is actually very simple but hardly intuitive. (Tested only with Word from Office 2011 on OSX 10.11.5.)
tell application "Microsoft Word"
tell active document
tell paragraph 1
select its text object
end tell
end tell
end tell
The core AppleScript language isn’t tricky at all; it’s small and all too limited for as mature as it is.
What really makes AppleScript tricky is that every application is different and has its quirks.
It just so happens that Microsoft apps are nightmarishly complex to script. Given the object model this should just work:
tell application "Microsoft Word"
tell active document
select paragraph 1
end tell
end tell
But it doesn’t.
Script Debugger makes discovering how apps work much easier, but it can still be anything but a picnic without good working examples (which most developers don’t provide).
There are several very good books, but no one goes into the deep detail of application scripting.
Shane Stanley’s “Everyday AppleScriptObjC” is the only one (so far) that covers AppleScriptObjC.
Just a courtesy FYI - if you’re using Word 2016, the ‘select’ method has, in fact, been removed. It is no longer in the standard library.
You’ll probably need to figure out the respective object’s equivalent mechanism for getting a handle on the start/end and then manually setting a selection object to those points yourself.
Edit: since I just had to handle this myself. If you’re dealing with a ‘field’, be sure to offset your selection (start -1) and (end + 1) to grab the field start/end delimiters. Otherwise you’ll get “odd” (expected, but odd) behavior like double-nesting fields, etc.
I’m using Obj-C, so I wrote a (bad) utility class to handle this - it asks “do you support ‘select’ ?” - if so, use that (for Word 2011). Otherwise, try to figure out if this is a ‘field’ - if so, get the code, offset the selections, then select. Etc. for the various kinds of objects you might need to handle. Because we can’t ask scripting bridge objects about their class (it blows up), I tried to ask if it supported what seemed like a unique selector for that class. Not an ideal solution, but for a limited number of classes, it seems to work (so far…).
Edit 2: I originally wrote that I was using ASOC. Updated to clarify I’m using the scripting bridge. Thank you, Shane
Absolutely. If you know of a way that works, I’d love to know. I get an exception whenever try to request ‘class’.
“Word” is the class prefix I defined when I generated the Obj-C header.
//I have this in a class called “WordHelpers”
+(void)select:(WordBaseObject*)wordObject {
if([wordObject isKindOfClass:[WordField class]]) {
//boom on trying to request the class type
}
}
I don’t want to create an instance of the class - I’d like to simply ask “which class are you?” so I can figure out how to handle selection with the content from the instance sent into the function.
I think I found a better option for my class checking.
I was attempting to use…
[SomeClass class]
to request the class, then using
[wordObjectObject isKindOfClass:[SomeClass class]]
where “SomeClass” was my sdef-generated class name. I think this was the problem. This consistently threw an exception (my understanding is this is something to do with the way the proxy classes work with the scripting bridge?).
Instead, I did the following (which works)
//we have our word object “wordObject”
NSString* wordClassName = NSStringFromClass([wordObject class]);
//this worked fine, so I could get the class name string of the current object
Then I just used the string form to compare.
BUT. What I found interesting was that the string form of the class name was not the name defined in the sdef file (EX: MyCutstomPrefixedWordField) - instead it was something like “MicrosoftWordField”. So - not the class names registered from the emitted sdef header.
The option’s not great, but definitely better than guessing on which selectors are supported and unique to a class.
I am working with MS Word 2016 which does not have the select command. I’m not a C programmer so I’m not able to grasp how the discussions of using C would help.
Could someone point me to a tutorial or information about how to select Word elements such as sections and paragraphs?