InDesign CS5.5 not running scripts

I am having some frustrating issues right now with a few of my scripts. The issue is that InDesign doesn’t seem to want to run some of my scripts the same way AppleScript Editor runs them. Every time I run a script in AppleScript Editor it works great, no problems. I then launch the same script through InDesign and get errors.

These are scripts that have been working, whether through InDesign or AppleScript Editor, for the past couple days and are just now flaking out on me.

I know this is a little vague, but it is driving me crazy. Any ideas would be greatly appreciated.

On a side note, I have a script that will not open with AppleScript Editor any more. I worked on it on Friday and was able to open the file no problem. I come into work this morning and it just crashes AppleScript Editor.

The following script is just one that won’t run correctly through InDesign but does natively in Applescript Editor. It is also the one that crashes AppleScript Editor if I try to open it.


global versionFolderList
global allDestinationSubFolders
global allPrintedFolders
global makeFolders
global printedFolderList
global userPDFsize
global finalFolderCounter
global foldersCreated
global finalFoldersToOutput
global finalEndFolders
global errorList
global sourceDepth
global createdDepth
global possibleError
global finalPageCount
global isAFM
global shortVersionName
global pageNumber
global theVersionName

set theVersionName to ""
set shortVersionName to ""
set pageNumber to ""
set isAFM to false
set finalPageCount to 0
set finalFolderCounter to 0
set foldersCreated to 0
set makeFolders to false
set sourceDepth to 1
set createdDepth to 0
set finalFoldersToOutput to {}
set finalEndFolders to {}
set errorList to {}



--Go to TO OUTPUT Folder and start getting the files
tell application "Adobe InDesign CS5.5"
	
	--set resourceFolder to choose file with prompt "Choose source file"
	set originalDoc to active document
	set myDocumentName to name of active document
	set pageNumber to (characters -7 thru -6 of myDocumentName) as string
	
	--display dialog pageNumber
	
	my getVersionName(myDocumentName)
	set versionName to theVersionName
	--display dialog versionName
	set shortVersionName to characters 1 through 3 of versionName as string
	--display dialog shortVersionName
	if shortVersionName = "AFM" then
		if pageNumber = "p1" then
			--display dialog "Yes"
			--return
			set isAFM to true
		end if
	end if
	--set versionNameEnding to characters 4 through 6 of versionName as string
	
	--set firstofPageNum to character 1 of pageNumber as string
	
	--set lastPageNum to character -1 of pageNumber as string
	
	set allLayers to layers of active document
	set layersLockedState to {}
	repeat with aLayer in allLayers
		set end of layersLockedState to locked of aLayer
		set locked of aLayer to false
	end repeat
	
	
	set resourceFolder to active document
	my getPageDimensions(resourceFolder)
	
	
	tell originalDoc
		if finalPageCount = 1 then
			undo
		end if
		if finalPageCount = 4 then
			undo
			undo
			if isAFM = true then
				undo
			end if
		end if
		if finalPageCount = 6 then
			undo
			undo
			undo
		end if
	end tell
	
	repeat with i from 1 to count of allLayers
		set thisLockedState to item i of layersLockedState
		set locked of item i of allLayers to thisLockedState
	end repeat
	
	--display dialog "Are you going to print your base? Don't forget to track this on the webpage."
	if pageNumber = "p1" then
		set myDialog to make dialog
		tell myDialog
			set name to "Pick Your Version"
			set myDialogColumn to make dialog column
			tell myDialogColumn
				--Create a drop down menu 
				set myBorderPanel to make border panel
				tell myBorderPanel
					make static text with properties {static label:"Pick Your Version:"}
					set userVersionChoice to make radiobutton group
					tell userVersionChoice
						set myGYMRadioButton to make radiobutton control with properties {static label:"GYM"}
						set myYHGRadioButton to make radiobutton control with properties {static label:"YHG"}
						set myBVHRadioButton to make radiobutton control with properties {static label:"BVH"}
						set myARSRadioButton to make radiobutton control with properties {static label:"ARS"}
						set myAFMRadioButton to make radiobutton control with properties {static label:"AFM"}
						set myRidleysRadioButton to make radiobutton control with properties {static label:"Ridleys"}
						set myMaceysRadioButton to make radiobutton control with properties {static label:"Maceys"}
						set myNoneRadioButton to make radiobutton control with properties {static label:"Not Tracking", checked state:true}
					end tell
				end tell
			end tell
		end tell
		set myResult to show myDialog
		if myResult is true then
			get properties of userVersionChoice
			if selected button of userVersionChoice is 0 then
				set myUserVersionChoice to "GYM"
				my openWebPage(myUserVersionChoice)
			else if selected button of userVersionChoice is 1 then
				set myUserVersionChoice to "YHG"
				my openWebPage(myUserVersionChoice)
			else if selected button of userVersionChoice is 2 then
				set myUserVersionChoice to "BVH"
				my openWebPage(myUserVersionChoice)
			else if selected button of userVersionChoice is 3 then
				set myUserVersionChoice to "ARS"
				my openWebPage(myUserVersionChoice)
			else if selected button of userVersionChoice is 4 then
				set myUserVersionChoice to "AFM"
				my openWebPage(myUserVersionChoice)
			else if selected button of userVersionChoice is 5 then
				set myUserVersionChoice to "Ridleys"
				my openWebPage(myUserVersionChoice)
			else if selected button of userVersionChoice is 6 then
				set myUserVersionChoice to "Maceys"
				my openWebPage(myUserVersionChoice)
			else if selected button of userVersionChoice is 7 then
				set myUserVersionChoice to "None"
			end if
		else
			return
		end if
		
		destroy myDialog
	end if
end tell

on openWebPage(theVersion)
	--display dialog "This will open the " & theVersion & " web page."
	
	if theVersion = "GYM" then
		tell application "Safari"
			set safariDoc1 to make new document at end of documents
			activate
			--set URL of tab 1 of safariDoc1 to "http://switooth.com/Tracking/GYM.html"
			tell safariDoc1
				set URL to "http://switooth.com/Tracking/GYM.html"
			end tell
		end tell
	else if theVersion = "YHG" then
		tell application "Safari"
			set safariDoc1 to make new document at end of documents
			activate
			tell safariDoc1
				set URL of safariDoc1 to "http://switooth.com/Tracking/YHG.html"
			end tell
		end tell
	else if theVersion = "BVH" then
		tell application "Safari"
			set safariDoc1 to make new document at end of documents
			activate
			tell safariDoc1
				set URL of safariDoc1 to "http://switooth.com/Tracking/BVH.html"
			end tell
		end tell
	else if theVersion = "ARS" then
		tell application "Safari"
			set safariDoc1 to make new document at end of documents
			activate
			tell safariDoc1
				set URL of safariDoc1 to "http://switooth.com/Tracking/ARS.html"
			end tell
		end tell
	else if theVersion = "AFM" then
		tell application "Safari"
			set safariDoc1 to make new document at end of documents
			activate
			tell safariDoc1
				set URL of safariDoc1 to "http://switooth.com/Tracking/AFM.html"
			end tell
		end tell
	else if theVersion = "Ridleys" then
		tell application "Safari"
			set safariDoc1 to make new document at end of documents
			activate
			tell safariDoc1
				set URL of safariDoc1 to "http://switooth.com/Tracking/Ridleys.html"
			end tell
		end tell
	else if theVersion = "Maceys" then
		tell application "Safari"
			set safariDoc1 to make new document at end of documents
			activate
			tell safariDoc1
				set URL of safariDoc1 to "http://switooth.com/Tracking/Maceys.html"
			end tell
		end tell
	end if
	
	
end openWebPage
--Open the File
on getPageDimensions(myStartFolder)
	tell application "Adobe InDesign CS5.5"
		
		try
			--open myStartFolder
			set originalDocument to active document
			set myPage to page 1 of active document
			
			set fullPath to full name of originalDocument
			set fullPathAlias to fullPath as alias
			
			tell text preferences of active document
				set subscriptPosition to subscript position
				set subscriptSize to subscript size
				set superscriptPosition to superscript position
				set superscriptSize to superscript size
				
			end tell
			
			tell active document to set basicPP to paragraph style "[Basic Paragraph]"
			set baseFont to applied font of basicPP
			set baseFontSize to point size of basicPP
			set baseFontLeading to leading of basicPP
			set baseFontTracking to tracking of basicPP
			
			--Determine if the file is a front flat or inside flat
			set myDocumentName to name of active document
			set pageNumber to (characters -7 thru -6 of myDocumentName) as string
			
			set myPage to page 1 of active document
			set myPageHeight to page height of document preferences of active document
			
			
			tell myPage
				set allPageDimensions to (page items whose label is "page divider")
			end tell
			
			
			--display dialog allPageDimensions
			if (count of items of allPageDimensions) = 3 then
				set finalPageCount to 6
				set box1myX1 to item 2 of geometric bounds of item 1 of allPageDimensions
				set box2myX1 to item 2 of geometric bounds of item 2 of allPageDimensions
				set box3myX1 to item 2 of geometric bounds of item 3 of allPageDimensions
				
				--get page 1 by geometric bounds
				if box1myX1 < box2myX1 then
					if box1myX1 < box3myX1 then
						set myPage1 to item 1 of allPageDimensions
						--set fill color of myPage1 to "Black"
						set remainingPages to {item 2 of allPageDimensions, item 3 of allPageDimensions}
					end if
				else if box2myX1 < box3myX1 then
					set myPage1 to item 2 of allPageDimensions
					--set fill color of myPage1 to "Black"
					set remainingPages to {item 1 of allPageDimensions, item 3 of allPageDimensions}
				else
					set myPage1 to item 3 of allPageDimensions
					--set fill color of myPage1 to "Black"
					set remainingPages to {item 1 of allPageDimensions, item 2 of allPageDimensions}
				end if
				
				--determine page 2 and 3 by geometric bounds
				set box1myX1 to item 2 of geometric bounds of item 1 of remainingPages
				set box2myX1 to item 2 of geometric bounds of item 2 of remainingPages
				
				
				if box1myX1 < box2myX1 then
					set myPage2 to item 1 of remainingPages
					--set fill color of myPage2 to myColorA
					set myPage3 to item 2 of remainingPages
					--set fill color of myPage3 to myColorB
				else if box2myX1 < box1myX1 then
					set myPage2 to item 2 of remainingPages
					--set fill color of myPage2 to myColorA
					set myPage3 to item 1 of remainingPages
					--set fill color of myPage3 to myColorB
				end if
			else if (count of items of allPageDimensions) = 2 then
				set finalPageCount to 4
				set box1myX1 to item 2 of geometric bounds of item 1 of allPageDimensions
				set box2myX1 to item 2 of geometric bounds of item 2 of allPageDimensions
				
				--get page 1 by geometric bounds
				if box1myX1 < box2myX1 then
					set myPage1 to item 1 of allPageDimensions
					set myPage2 to item 2 of allPageDimensions
					--set fill color of myPage1 to "Black"
				else
					set myPage1 to item 2 of allPageDimensions
					set myPage2 to item 1 of allPageDimensions
					--set fill color of myPage1 to "Black"
				end if
			else if (count of items of allPageDimensions) = 1 then
				set finalPageCount to 1
				set myPage1 to item 1 of allPageDimensions
				
			end if
			
			--Get the Dimensions of the different pages within the document
			
			
			
			
			--Get the Dimensions of the different pages within the document
			--Get Dimensions of page 1
			
			
			set page1X1 to (item 4 of geometric bounds of myPage1)
			set page1x2 to (item 2 of geometric bounds of myPage1)
			set page1y1 to (item 1 of geometric bounds of myPage1)
			set page1y2 to (item 3 of geometric bounds of myPage1)
			set page1Width to (page1X1 - page1x2)
			
			
			if page1X1 < 0 or page1x2 < 0 or page1y1 < 0 or page1y2 < 0 then
				set possibleError to "Rulers are set in the top right hand corner"
			end if
			
			
			if finalPageCount = 4 or finalPageCount = 6 then
				--Get Dimensions of page 2
				set page2X1 to (item 4 of geometric bounds of myPage2)
				set page2x2 to (item 2 of geometric bounds of myPage2)
				set page2Width to (page2X1 - page2x2)
				if shortVersionName = "AFM" and pageNumber = "p1" then
					set page2Width to page2Width - 2.75
					set page2X1 to page2X1 - 2.75
					
					set popPageWidth to 12
					set page4x2 to page2X1
					set page4X1 to page2X1 + 12
				end if
			end if
			--Get Dimensions of page 3
			if finalPageCount = 6 then
				set page3X1 to (item 4 of geometric bounds of myPage3)
				set page3x2 to (item 2 of geometric bounds of myPage3)
				set page3Width to (page3X1 - page3x2)
			end if
			
			
			--Select text that falls in page 1
			
			tell active document
				set thespread to active spread of layout window 1
				set selectForPage1 to (every item of all page items of spread 1 where item 1 of its geometric bounds is greater than page1x2 and item 1 of its geometric bounds is less than page1y2 and item 2 of its geometric bounds is greater than page1y1 and item 2 of its geometric bounds is less than page1X1 and parent's class is spread)
				
				set page1Group to make group at thespread with data {group items:selectForPage1}
				
				--display dialog page2X1
				if finalPageCount = 6 or finalPageCount = 4 then
					set selectForPage2 to (every item of page items of spread 1 where item 1 of its geometric bounds is greater than page1y1 and item 1 of its geometric bounds is less than page1y2 and item 2 of its geometric bounds is greater than page2x2 and item 2 of its geometric bounds is less than page2X1 and parent's class is spread)
					set page2Group to make group at thespread with data {group items:selectForPage2}
				end if
				
				
				
				if finalPageCount = 6 then
					set selectForPage3 to (every item of all page items of spread 1 where item 1 of its geometric bounds is greater than page1y1 and item 1 of its geometric bounds is less than page1y2 and item 2 of its geometric bounds is greater than page3x2 and item 2 of its geometric bounds is less than page3X1 and parent's class is spread)
					set page3Group to make group at thespread with data {group items:selectForPage3}
				end if
				
				if isAFM = true then
					set selectForAFMPop to (every item of all page items of spread 1 where item 1 of its geometric bounds is greater than page1y1 and item 1 of its geometric bounds is less than page1y2 and item 2 of its geometric bounds is greater than page4x2 and item 2 of its geometric bounds is less than page4X1 and parent's class is spread)
					set pageAFMPop to make group at thespread with data {group items:selectForAFMPop}
				end if
				
				--reverse the selection of page 1
				
				
				
			end tell
			
			
			
			set newPage1 to make document with properties {document preferences:{page width:page1Width, page height:(myPageHeight + 0.25), facing pages:false}}
			
			tell text preferences of active document
				set subscript position to subscriptPosition
				set subscript size to subscriptSize
				set superscript position to superscriptPosition
				set superscript size to superscriptSize
			end tell
			
			tell active document
				set basicPPnew to paragraph style "[Basic Paragraph]"
				set applied font of basicPPnew to baseFont
				set point size of basicPPnew to baseFontSize
				set leading of basicPPnew to baseFontLeading
				set tracking of basicPPnew to baseFontTracking
				import styles format object styles format from fullPathAlias global strategy load all with overwrite
				set adamsParaStyle to make paragraph style with properties {name:"Adam's Style", justification:center align, applied font:"Helvetica (TT)", font style:"Bold", point size:"16"}
			end tell
			
			tell margin preferences of newPage1
				set top to 0
				set left to 0
				set bottom to 0
				set right to 0
			end tell
			
			tell view preferences of newPage1
				set horizontal measurement units to inches
				set vertical measurement units to inches
			end tell
			
			--display dialog 
			
			if finalPageCount = 6 or finalPageCount = 4 then
				
				tell newPage1
					set newPage2 to make page
					
					
					tell master spread 1
						tell margin preferences of pages
							set top to 0
							set left to 0
							set bottom to 0
							set right to 0
						end tell
					end tell
					--display dialog myPageHeight
					--display dialog page2Width
					tell margin preferences of newPage2
						set top to 0
						set left to 0
						set bottom to 0
						set right to 0
					end tell
					
				end tell
				--tell newPage2
				set newPage2Width to page2Width * 72
				set myNewPageHeight to myPageHeight * 72
				--display dialog "myPageHeight: " & myPageHeight & return & "myNewPageHeight: " & myNewPageHeight
				
				
				tell pages of active document
					resize newPage2 in inner coordinates from top left anchor by replacing current dimensions with values {newPage2Width, (myNewPageHeight + 18)} with considering ruler units
				end tell
				
				--end tell
			end if
			
			if finalPageCount = 6 then
				tell newPage1
					set newPage3 to make page
					
					set newPage3Width to page3Width * 72
					set myNewPageHeight1 to myPageHeight * 72
					
					
					
					tell pages of active document
						resize newPage3 in inner coordinates from top left anchor by replacing current dimensions with values {newPage3Width, (myNewPageHeight1 + 18)} with considering ruler units
					end tell
				end tell
				
				
			end if
			
			if isAFM = true then
				tell newPage1
					set newPopPage to make page
					
					set newPopPageWidth to popPageWidth * 72
					set myNewPageHeight2 to myPageHeight * 72
					tell pages of active document
						resize newPopPage in inner coordinates from top left anchor by replacing current dimensions with values {newPopPageWidth, (myNewPageHeight2 + 18)} with considering ruler units
					end tell
				end tell
			end if
			
			
			tell originalDocument
				set myDocumentName to name of originalDocument
				
				try
					set myVersion to my getVersionName(myDocumentName)
				on error
					set myVersion to myDocumentName
				end try
				
				
				tell page1Group
					set dupedItem to duplicate to page 1 of newPage1
					move dupedItem by {0, 0.25}
					tell page 1 of newPage1
						set boxForVersionName to make rectangle with properties {geometric bounds:{0.25, 5, 0.5, 9}, content type:text type}
						tell insertion point -1 of boxForVersionName to set contents to myVersion
						set applied paragraph style of paragraph 1 of boxForVersionName to adamsParaStyle
					end tell
				end tell
				
				
				
				if finalPageCount = 6 or finalPageCount = 4 then
					
					
					
					tell page2Group
						set dupedItem to duplicate to page 2 of newPage1
						move dupedItem by {-page1Width, 0.25}
						tell page 2 of newPage1
							set boxForVersionName to make rectangle with properties {geometric bounds:{0.25, 5, 0.5, 9}, content type:text type}
							tell insertion point -1 of boxForVersionName to set contents to myVersion
							set applied paragraph style of paragraph 1 of boxForVersionName to adamsParaStyle
						end tell
					end tell
					
				end if
				
				if finalPageCount = 6 then
					
					tell page3Group
						set dupedItem to duplicate to page 3 of newPage1
						move dupedItem by {-(page2Width + page1Width), 0.25}
						tell page 3 of newPage1
							set boxForVersionName to make rectangle with properties {geometric bounds:{0.25, 5, 0.5, 9}, content type:text type}
							tell insertion point -1 of boxForVersionName to set contents to myVersion
							set applied paragraph style of paragraph 1 of boxForVersionName to adamsParaStyle
						end tell
					end tell
				end if
				
				if isAFM = true then
					tell pageAFMPop
						set dupedItem to duplicate to page 3 of newPage1
						move dupedItem by {-(page2Width + page1Width), 0.25}
						tell page 3 of newPage1
							set boxForVersionName to make rectangle with properties {geometric bounds:{0.25, 5, 0.5, 9}, content type:text type}
							tell insertion point -1 of boxForVersionName to set contents to myVersion
							set applied paragraph style of paragraph 1 of boxForVersionName to adamsParaStyle
						end tell
					end tell
				end if
				
				
			end tell
		on error
			
		end try
	end tell
	
	
end getPageDimensions


on myReplace(myString, myFindString, myChangeString)
	set AppleScript's text item delimiters to myFindString
	set myTextList to every text item of (myString as text)
	set AppleScript's text item delimiters to myChangeString
	set myString to myTextList as string
	set AppleScript's text item delimiters to ""
	return myString
end myReplace


on getVersionName(myDocumentName)
	
	set AppleScript's text item delimiters to "_"
	set docNameComponents to every text item of myDocumentName
	set theVersionName to item 1 of docNameComponents
	--display dialog theVersionName
	set AppleScript's text item delimiters to ""
	set myVersionNameDate to theVersionName & " " & item 2 of docNameComponents & " Base"
	return myVersionNameDate as string
	
end getVersionName