(contritely) Thanks, Kai, but my mail was actually me shooting myself in the foot by putting in wrong code. The actual handler has a whole bunch of other, unrelated messy (and ineffective) code that I didn't want to include, including the fact that the excerpt I sent is actually in a repeat loop that steps through a number of paragraphs.
However, those commands ARE actually within a tell statement to MS Word that I failed to include. If it wasn't, I think I would have gotten a different, VERY puzzled error message from AppleScript!
Not very good to be so sloppy when seeking help, but putting the whole code in seemed like it would not have been more helpful. To test the code in question, I extract the relevant section, similar to what I posted but within a tell statement and with something a bit more useful than "beep" in the "if" statement, and run that as a separate script, so as to eliminate all of the other mess from the test.
I will now post the whole handler as it currently exists. It's purpose is to try and suss out whether a Word document received from a contractor conforms to our desired structure and, if not, to fix it. However, as I have noted, it is a first and very flawed attempt; I am currently contemplating either a more efficient approach, or whether it is even really possible to anticipate all the ways our contractors could goof it up. However, for the sake of getting help, here it is:
on getStoryParts(wholeStory)
set lineCheck to {1, 2, 3, 4, 5, 6}
set formatWrong to false
tell application "Microsoft Word"
--now get the text itself to work on
set wholeStoryText to content of wholeStory
repeat with z from 1 to 6
--check if line is blank
if paragraph z of wholeStoryText is "" then
set item z of lineCheck to "B"
else
--if not, check whether it's the word count line
if ((paragraph 2 of wholeStoryText contains "About") and (paragraph 2 of wholeStoryText contains "words")) then
set item z of lineCheck to "W"
else
--if it's not blank and not the word count, then it's text: either the title or the story.
set item z of lineCheck to "T"
end if
end if
end repeat
-- check for word count
if (not (lineCheck contains "W")) then
-- if no WC and first line is blank, check for BTBT and then insert WC at top
if (item 1 of lineCheck is "B") and (item 2 of lineCheck is "T") and (item 3 of lineCheck is "B") and (item 4 of lineCheck is "T") then
insert text "About xxx words" & return at text object of paragraph 1 of active document
else
--if first line is not blank, check for TBT and insert WC and blank line
if (item 1 of lineCheck is "T") and (item 2 of lineCheck is "B") and (item 3 of lineCheck is "T") then
insert text "About xxx words" & return & return at text object of paragraph 1 of active document
else
if errorList is "" then
set errorList to "header format problems: WC/title/story lines wrong"
else
set errorList to errorList & "header format problems: WC/title/story lines wrong"
end if
end if
end if
else
-- check for correct format
if not (lineCheck is {"W", "B", "T", "B", "T", "T"}) then
-- if WC is there but the first line is blank, delete the first line
if lineCheck is {"B", "W", "B", "T", "B", "T"} then
delete paragraph 1 of wholeStory
else
set errorList to errorList & "header format problems: WC/title/story lines wrong"
set formatWrong to true
end if
end if
end if
--make title line bold unless the format is just hosed
if formatWrong is false then
set titleLine to create range active document start (start of content of text object of paragraph 3 of active document) end (end of content of text object of paragraph 3 of active document)
set bold of font object of titleLine to true
end if
end tell
end getStoryParts
On Mar 20, 2007, at 8:11 AM, kai wrote:
On 20 Mar 2007, at 00:56, Larry the O wrote:
on getStoryParts(wholeStory)
set wholeStoryText to content of wholeStory
if paragraph 1 of wholeStoryText is "" then
beep
end if
end getStoryParts
The script crashes in getStoryParts with a message saying "Can't get paragraph 1 of missing value."
The last command in the log before the crash message is where it tries to get the content of wholeStory, for which the result is "missing value."
I also tried getting the content of the text object of wholeStory, with identical results.
So, it would seem that the problem is not wholeStory getting passed correctly, since the other routines seem to be getting it, but, on the other hand, this works fine:
tell application "Microsoft Word"
set wholeStory to (create range active document start 0 end (count of characters in active document))
set wholeText to content of wholeStory
if paragraph 2 of wholeText is "" then --paragraph 2 is blank
display dialog "we got nothin!" --this dialog comes up as expected
else
display dialog "We got text!"
end if
end tell
I'm SO confused.
Perfectly understandable, Larry - but there's one significant difference between the code within your 'getStoryParts' handler and the in-line version immediately above. In the latter, the entire code is within an application tell statement, while that in the separate handler isn't.
When the handler code is compiled, the word 'content' is interpreted as a variable - which causes problems at runtime. In the in-line version, the same word is interpreted, this time correctly, as a Word-specific property of the referenced text range. (When you compile the code in Script Editor, check out the way in which the word is visibly formatted in each case; you should see a difference.)
You should be able to fix this in a number of ways, including...
Adding a tell statement:
-----------------
on getStoryParts(wholeStory)
tell application "Microsoft Word" to ¬
set wholeStoryText to content of wholeStory
if paragraph 1 of wholeStoryText is "" then
beep
end if
end getStoryParts
-----------------
Inserting a 'using terms from' statement (so the code is compiled correctly):
-----------------
on getStoryParts(wholeStory)
using terms from application "Microsoft Word"
set wholeStoryText to content of wholeStory
end using terms from
if paragraph 1 of wholeStoryText is "" then
beep
end if
end getStoryParts
-----------------
Using the raw code for any application-specific term(s):
-----------------
on getStoryParts(wholeStory)
set wholeStoryText to «class 1650» of wholeStory
if paragraph 1 of wholeStoryText is "" then
beep
end if
end getStoryParts
-----------------
---
kai
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Help/Unsubscribe/Update your Subscription: