Pesky AppleScript Problem!

subgeniuszero

Registered
Hey folks, I'm working on a large AppleScript that splits Pages documents based upon their paragraph style, but this particular handler is giving me trouble. It keeps saying, "Apple Event Handler Failed. Error -10000" whenever it gets to the "move" command (at least that's what the AppleScript Editor highlights.) What am I doing wrong?


(* Get the file to operate on and the location to save the files *)

set theFile to choose file of type "pages" with prompt "Please choose a file to operate on:" without invisibles
set saveLocation to (choose folder with prompt "Please choose a place to save the finished files:") as string
set firstLooper to 1
set numberOfFolders to 0

(* Boss Pages around *)

tell application "Pages"
set theDoc to open theFile -- Tell Pages to open the user's selected file and put a reference to it in the variable "theDoc"
tell theDoc -- Boss the document around
set theFileName to name -- set the variable "theFileName" to the name of the docuemnt
if theFileName ends with ".pages" then set theFileName to text 1 thru -7 of theFileName -- remove the file extension from theFileName
set numberOfParagraphs to (count paragraphs) -- Count the paragraphs
repeat until firstLooper > numberOfParagraphs -- loop once for each paragraph
set styleName to name of paragraph style of paragraph firstLooper -- get the name of the paragraph style for the current paragraph
if styleName is "Heading 1" then
(* Create a list containing the results returned by the "createFolder" handler *)
set {newFolder, folderName} to my createFolder((text 1 thru -2 of (get paragraph firstLooper of theDoc)), saveLocation, numberOfFolders)
set paraIndexList to {} -- create a list to hold the indexes of the chosen paragraphs
set heading1Index to firstLooper --variable to hold the index of the found "Heading 1" text
set heading2Index to 0 -- variable to hold the index of the found "Heading 2" text
set numberOfNewDocs to 0 -- the number of new documents
set numberOfFolders to numberOfFolders + 1
(* If the value of 'firstLooper' is less than the numberOfParagraphs,¬
then loop through the paragraphs from firstLooper onwards *)
if firstLooper < numberOfParagraphs then repeat with secondLooper from firstLooper + 1 to numberOfParagraphs
set styleName to name of paragraph style of paragraph secondLooper
set firstLooper to secondLooper
if styleName is "Heading 2" then
if heading2Index = 0 then set heading2Index to secondLooper -- found the first "Heading 2" , not the next
set paraIndexList to {secondLooper} --the first item in the list is this paragraph index
if secondLooper < numberOfParagraphs then
repeat with thirdLooper from secondLooper + 1 to numberOfParagraphs
set styleName to name of paragraph style of paragraph thirdLooper
if styleName is in {"Body", "Normal"} then
set end of paraIndexList to thirdLooper -- add the paragraph index to the list
else if styleName is "Heading 2" then -- found the next "Heading 2"
set numberOfNewDocs to numberOfNewDocs + 1
--***** export this Heading 2 text beneath that Heading 1 text
my copyToNewDocAndExport(paraIndexList, it, theFileName, numberOfNewDocs, newFolder, true)
-- reset the list paraIndexList, the loop continue for the next heading 2
set paraIndexList to {thirdLooper}
else if styleName is "Heading 1" then -- the style of paragraph thirdLooper is the next heading 1
set firstLooper to thirdLooper
exit repeat -- exit the third loop
end if
end repeat -- end of the third loop
end if
end if
if styleName is "Heading 1" then exit repeat -- exit the second loop
end repeat -- end of the second loop
if paraIndexList is not {} then
set numberOfNewDocs to numberOfNewDocs + 1
-- export this Heading 2 text beneath that Heading 1 text
my copyToNewDocAndExport(paraIndexList, it, theFileName, numberOfNewDocs, newFolder, true)
end if
if heading2Index = 0 then set heading2Index to firstLooper + 1
-- export any text directly beneath the heading 1 text
my copyToNewDocAndExport({heading1Index, heading2Index - 1}, it, folderName, numberOfNewDocs, newFolder, false)
if firstLooper = numberOfParagraphs and (styleName is not "Heading 1" or heading1Index = firstLooper) then set firstLooper to firstLooper + 1 -- end of document
else
set firstLooper to firstLooper + 1
end if
end repeat --end of the first loop
end tell
end tell

on createFolder(textForFolderName, destinationFolder, numberOfFolders)
tell application "Finder"
set folderName to ((text -3 thru -1 of ("000" & numberOfFolders)) & "&#8212;" & textForFolderName)
set x to make new folder at folder destinationFolder with properties {name:folderName}
return {x as string, name of x}
end tell
end createFolder

on copyToNewDocAndExport(paragraphIndexList, theDocument, styledParagraph, numberOfDocs, destinationFolder, textStyleSwitch)
tell application "Pages"
set newDoc to make new document
set theIndex to item 1 of paragraphIndexList
if textStyleSwitch then --heading 2 text and any "Body" or "Normal" text
set newName to (text -3 thru -1 of ("000" & numberOfDocs)) & "&#8212;" & (text 1 thru -2 of (get paragraph theIndex of theDocument)) & ".doc"
if newName contains ":" then set newName to my removeColon(newName)
repeat with firstLooper in paragraphIndexList
move (paragraph firstLooper of theDocument) to after last paragraph of newDoc -- copy paragraph from open doc and paste in new doc
select the first paragraph of newDoc
set mySel to (get selection of newDoc)
set paragraph style of mySel to "Title"
end repeat
else -- any text directly beneath the heading 1 text
set theLastIndex to last item of paragraphIndexList
set newName to styledParagraph & ".doc" -- styleParagraph is the folder Name
move (paragraphs theIndex thru theLastIndex of theDocument) to after last paragraph of newDoc -- copy paragraphs from open doc and paste in new doc
select the first paragraph of newDoc
set mySel to (get selection of newDoc)
set paragraph style of mySel to "Title"
end if
save newDoc as "Microsoft Word 97 - 2004 document" in (destinationFolder & newName)
close newDoc saving no
end tell
end copyToNewDocAndExport

on removeColon(textVar)
set oldDelims to text item delimiters
set text item delimiters to ":"
set textVar to text items of textVar
set text item delimiters to " "
set textVar to textVar as string
set text item delimiters to oldDelims
return textVar
end removeColon
 
Last edited:
Back
Top