Text

Styling Paragraphs (CS2 most likely)

InDesign, surprisingly, cares whether or not the text in a paragraph is actually visible in order to apply a style to it, even though you are giving a command a specific paragraph. To get around this, the style has to be applied to the paragraph in the parent story. Luckily, the paragraph count is exactly the same.

tell myTextFrame
	place (myContent)
	set myTextFrameParentStory to parent story
	set paragraphCount to (get count of paragraphs in myTextFrameParentStory)
	tell myTextFrameParentStory
		repeat with n from 1 to paragraphCount
			tell paragraph n
				set applied paragraph style to "myParagraphStyle"
			end tell
		end repeat
	end tell
end tell

Source: MacScripters

Extract All Text (CS2)

This script extracts all of the stories in an InDesign file, and then combines everything in Word. This is sort of a substitute to TexTractor in Quark as I couldn’t find a viable counterpart for InDesign at the time. It would take quite a bit of work to get this as clean as the plug-ins, but works in a pinch. This also includes a subroutine that combines the output into a Word document, but there were problems associated with Word. The first is that using the copy and paste command via Applescript has a 256 character limit. The second is that the Merge Document command is frustratingly slow.

global masterDocument
global masterText
global newDocument
global newText

global theSaveToFolder
global theDocumentName

--troubleshooting only
global myStory -- to story myCounter
global theInsertionPoint --to insertion point 1 of myStory
global theInsertionPointObj --to object reference of theInsertionPoint
global theParentTextFrame --to item 1 of parent text frames of theInsertionPointObj
global thePage --to parent of theParentTextFrame
global thePageNumber --to name of thePage

global myStory

tell application "Adobe InDesign CS2"

	set documentCount to (get count of documents)

	if documentCount = 0 then
		display dialog "No documents are open. Please open a document and try again."
		quit
	end if

	repeat with d from 1 to documentCount

		set theDocument to document d

		if (count stories of theDocument) > 0 then
			my ExportAllText(theDocument)
		end if

	end repeat

end tell

display dialog "DONE"

on ExportAllText(theDocument)
	tell application "Adobe InDesign CS2"
		set theDocumentName to (get name of theDocument) as string
		set theSaveToFolder to CreateFolder(theDocumentName) of me

		tell theDocument

			repeat with myCounter from 1 to (count stories)
				set myStory to story myCounter
				set thePageNumber to id of myStory

				set myFormat to RTF
				set myExtension to ".rtf"
				set myFileName to "Export_" & thePageNumber & myExtension
				set myFilePath to (theSaveToFolder as string) & myFileName
				tell myStory
					export format myFormat to (myFilePath as string)
				end tell
			end repeat

		end tell --document
	end tell --InDesign

	--my MergeDocumentsInsertFile(theSaveToFolder)

end ExportAllText

(* This works, but it takes a long time. *)
on MergeDocumentsInsertFile(theFolder)

	tell application "Finder" to set alias_list to files of (theFolder as alias)

	tell application "Microsoft Word"

		make new document

		tell active document to repeat with this_doc in alias_list
			insert file at after last character file name (this_doc as Unicode text)
		end repeat

		set theDesktopFolder to path to desktop as Unicode text
		set theFileName to (theDocumentName as string) & ".rtf"

		save as document 1 file name (theDesktopFolder & theFileName) file format format rtf
		close active document saving no

		tell application "Finder" to set theTrashedFolder to delete (theFolder as alias)

	end tell

end MergeDocumentsInsertFile

on CreateFolder(theFolderName)
	set theFolderFinalName to trim_line(theFolderName, ".indd", 1) of me

	tell application "Finder"
		set theFolder to make folder at (get path to desktop folder) with properties {name:theFolderFinalName, owner privileges:read write, group privileges:read write, everyones privileges:read write}
	end tell

	return theFolder
end CreateFolder

on trim_line(this_text, trim_chars, trim_indicator)
	-- 0 = beginning, 1 = end, 2 = both
	set x to the length of the trim_chars
	-- TRIM BEGINNING
	if the trim_indicator is in {0, 2} then
		repeat while this_text begins with the trim_chars
			try
				set this_text to characters (x + 1) thru -1 of this_text as string
			on error
				-- the text contains nothing but the trim characters
				return ""
			end try
		end repeat
	end if
	-- TRIM ENDING
	if the trim_indicator is in {1, 2} then
		repeat while this_text ends with the trim_chars
			try
				set this_text to characters 1 thru -(x + 1) of this_text as string
			on error
				-- the text contains nothing but the trim characters
				return ""
			end try
		end repeat
	end if
	return this_text
end trim_line

on WriteLog(the_text)
	set this_story to the_text & return
	set this_file to (((path to desktop folder) as text) & "Log.txt")
	my write_to_file(this_story, this_file, true)
end WriteLog

on write_to_file(this_data, target_file, append_data)
	try
		set the target_file to the target_file as text
		set the open_target_file to ¬
			open for access file target_file with write permission
		if append_data is false then ¬
			set eof of the open_target_file to 0
		write this_data to the open_target_file starting at eof
		close access the open_target_file
		return true
	on error
		try
			close access file target_file
		end try
		return false
	end try
end write_to_file

Extract All Text 2

This methodically goes through an InDesign document getting the text contained in each text frame, and tries to maintain some semblance of content order.

It does so by looking at each page as a grid and getting the page item that is contained in each square, moving left to right, up to down. Text is placed in an array, joined with returns, and INDD-proprietary tags removed. Faster than the previous version, but still not as clean as a plug-in.

(*
File: Get Text Extraction (InDesign CS3)
Author(s): Philip Regan
Source Code Copyright: Copyright (c) 2010 Philip Regan All Rights Reserved.
Additional Copyright: replace_chars(), write_to_file() Copyright (c) Apple, Inc. Used with permission.
Source:	New source only; no adaptations.
Requirements: Document Opened in InDesign CS3
Notes:	
	This methodically goes through an InDesign document getting the text contained in each text frame.
	It does so by looking at each page as a grid and getting the page item that is contained in each 
	square, moving left to right, up to down. Text is placed in an array, joined with returns, and 
	INDD-proprietary tags removed.
Change History:
    10_04_06_01_00_000: Started source
*)

property kDebugRun : true

property pDocumentName : ""
property pExtractedText : {}

property kTagsToRemove : {"", "<2003>", "", "<00AD>"}

-- InDesign constants

property kPageItem_ContentType_Unassigned : "unassigned"
property kPageItem_ContentType_Text : "text type"

tell application "Adobe InDesign CS3"

	set theDocument to document 1

	my ProcessDocument(theDocument)
	my SaveExtractedText()

end tell -- application

on ProcessDocument(theDocument)
	tell application "Adobe InDesign CS3"
		tell theDocument

			set pDocumentName to name

			set pageCount to get count of every page

			repeat with thisPage from 1 to pageCount

				-- 				if (kDebugRun) then
				-- 					log "Page: " & thisPage
				-- 				end if

				set thePage to page thisPage

				my ProcessPage(thePage)

			end repeat -- pages
		end tell -- document

	end tell
end ProcessDocument

on ProcessPage(thePage)
	tell application "Adobe InDesign CS3"

		tell thePage

			set pageItemList to {}

			set pageItemCount to (get count of every page item)

			repeat with thisPageItem from 1 to pageItemCount

				set thePageItem to page item thisPageItem

				set end of pageItemList to thePageItem

			end repeat -- page items

			if (get count of items in pageItemList) = 1 then
				my ProcessPageItem(item 1 of pageItemList)
			else
				repeat while (get count of items in pageItemList) > 1
					set indexFound to ProcessPageItemListInPage(pageItemList, thePage) of me
					set pageItemList to RemoveItemInListByIndex(pageItemList, indexFound) of me

					-- 					if (kDebugRun) then
					-- 						log "page item list count: " & (get count of items in pageItemList)
					-- 					end if

					-- something odd happened, but if we don't do this, then we'll never get out of here
					if indexFound = 0 then
						-- 						log "ERROR!: indexFound = 0"
						repeat with thisPageItem from 1 to (get count of items in pageItemList)
							my ProcessPageItem(item thisPageItem of pageItemList)
						end repeat
						exit repeat
					end if
				end repeat
				my ProcessPageItem(item 1 of pageItemList)
			end if

		end tell -- page

	end tell
end ProcessPage

on ProcessPageItemListInPage(pageItemList, thePage) -- (list) as integer

	set indexOfItemToRemove to 0

	set kCursorIncrement to 72

	tell application "Adobe InDesign CS3"
		set theBounds to bounds of thePage

		set pageY1 to item 1 of theBounds
		set pageX1 to item 2 of theBounds
		set pageY2 to item 3 of theBounds
		set pageX2 to item 4 of theBounds

		repeat with cursorX from (pageX1 - kCursorIncrement) to pageX2 by kCursorIncrement
			repeat with cursorY from (pageY1 - kCursorIncrement) to pageY2 by kCursorIncrement

				--log "{x, y} = {" & cursorX & ", " & cursorY & "}"

				set cursorY1 to cursorY
				set cursorX1 to cursorX
				set cursorY2 to cursorY1 + kCursorIncrement
				set cursorX2 to cursorX1 + kCursorIncrement

				set cursorCoordinates to {cursorY1, cursorX1, cursorY2, cursorX2}

				set pageItemCount to (get count of items in pageItemList)

				repeat with thisPageItem from 1 to pageItemCount

					set thePageItem to item thisPageItem of pageItemList

					--log "page item: " & thisPageItem

					set pageItemCoordinates to geometric bounds of thePageItem

					set pageItemIsWithinCursor to IsPageItemWithinCursor(pageItemCoordinates, cursorCoordinates) of me

					if (pageItemIsWithinCursor) then
						my ProcessPageItem(thePageItem)
						return thisPageItem
					end if

				end repeat -- page items

			end repeat -- cursorY
		end repeat -- cursorX
	end tell

	return indexOfItemToRemove

end ProcessPageItemListInPage

on IsPageItemWithinCursor(pageItemCoordinates, cursorCoordinates) --(list, list) as boolean

	-- 	if (kDebugRun) then
	-- 		log "cursor:" & cursorCoordinates
	-- 	end if

	set pageItemY1 to item 1 of pageItemCoordinates
	set pageItemX1 to item 2 of pageItemCoordinates
	set pageItemY2 to item 3 of pageItemCoordinates
	set pageItemX2 to item 4 of pageItemCoordinates

	set cursorY1 to item 1 of cursorCoordinates
	set cursorX1 to item 2 of cursorCoordinates
	set cursorY2 to item 3 of cursorCoordinates
	set cursorX2 to item 4 of cursorCoordinates

	set Y1_OK to false
	set X1_OK to false
	set Y2_OK to false
	set X2_OK to false

	if pageItemY1 ≥ cursorY1 then set Y1_OK to true
	if pageItemX1 ≥ cursorX1 then set X1_OK to true
	if pageItemY1 < cursorY2 then set Y2_OK to true
	if pageItemX1 < cursorX2 then set X2_OK to true

	if Y1_OK and X1_OK and Y2_OK and X2_OK then
		return true
	end if

	return false

end IsPageItemWithinCursor

on RemoveItemInListByIndex(theList, theIndex) -- (list, integer) as list

	if theIndex is 0 then return theList

	set newList to {}

	set itemCount to (get count of items in theList)
	repeat with thisItem from 1 to itemCount
		if thisItem is not theIndex then
			set theItem to item thisItem of theList
			set end of newList to theItem
		end if
	end repeat

	return newList
end RemoveItemInListByIndex

on ProcessPageItem(thePageItem)
	tell application "Adobe InDesign CS3"
		set theContentType to ""

		try
			set theContentType to content type of thePageItem as string
		on error
			set theContentType to kPageItem_ContentType_Unassigned
		end try

		if theContentType contains kPageItem_ContentType_Text then

			set theContents to contents of thePageItem

			set end of pExtractedText to (get text of thePageItem as string)

		end if
	end tell
end ProcessPageItem

on SaveExtractedText()

	set AppleScript's text item delimiters to return
	set formattedText to pExtractedText as Unicode text
	set AppleScript's text item delimiters to ""

	set tagCount to (get count of items in kTagsToRemove)

	repeat with thisTag from 1 to tagCount
		set theTag to item thisTag in kTagsToRemove
		set formattedText to replace_chars(formattedText, theTag, "") of me
	end repeat

	my WriteText(formattedText, pDocumentName & ".txt")

end SaveExtractedText

on replace_chars(this_text, search_string, replacement_string)
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to ""
	return this_text
end replace_chars

on WriteText(the_text, file_name)
	set this_story to the_text & return
	set this_file to (((path to desktop folder) as text) & file_name)
	my write_to_file(this_story, this_file, true)
end WriteText

on write_to_file(this_data, target_file, append_data)
	try
		set the target_file to the target_file as text
		set the open_target_file to ¬
			open for access file target_file with write permission
		if append_data is false then ¬
			set eof of the open_target_file to 0
		write this_data to the open_target_file starting at eof
		close access the open_target_file
		return true
	on error
		try
			close access file target_file
		end try
		return false
	end try
end write_to_file