Hi, I used this script in IDCS2 but things have changed for CS3 in the find/search function.
In CS3 it gives an error at: «event K2 Find»
«event K2 Find» story given «class for »:fileName
I have to alter the find function, but where to begin? I can change small parts in a script but a complete new find function?
Here is the script:
(*
* This script is for automatically inserting EPS MathType equations that have been
* exported from Word, and adjusts the position for that equation to match the
* baseline stored in the equation file. I have not included any error checking so
* a few things need to be assumed.
*
* 1. The file names in the InDesign document are of the format <<.filename.>>
* 2. The equation files are in EPS format, with or without a preview
* 3. There are no other files in the same folder as the equation files
* Note: If there is more than one reference to the same filename, each
* reference will be replaced with the same file.
* I hacked this script together from bits and pieces of other scripts I found on
* the internet. If you see bits of your code in here, thank you for helping me
* learn a little more about Applescript.
*)
-- get the source folder for the equations from the user and compile a list of the enclosed files
set the sourceFolder to choose folder with prompt "Select the directory containing the equations:"
set the fileList to list folder sourceFolder without invisibles
-- step through each file in the list, search for a matching name tag in the active document, and
-- replace with the file and move the baseline of the placed equation
repeat with i from 1 to the number of items in the fileList
set currentItem to item i of the fileList
set currentFilePath to ((sourceFolder as string) & currentItem)
-- test if the filename is not a folder and then place it
if (info for (currentFilePath as alias)) is not folder then
set searchstring to "<<" & currentItem & ">>"
place_file(searchstring, currentFilePath, get_baseline(currentFilePath as alias))
end if
end repeat
display dialog "Finished placing files from the folder " & (sourceFolder as string) & "."
(************************************************************
* place_file():
* Finds all references to the equation in the text, and replaces each reference
* with the placed graphic. It then moves the graphic down the distance
* equivalent to the baseline for that inline graphic.
************************************************************)
on place_file(fileName, filePath, fileBaseline)
tell application "Adobe InDesign CS3"
-- test that a document is open
if (count documents) > 0 then
set myDocument to active document
tell myDocument
-- search for all references of the filename
«event K2 Find» story given «class for »:fileName
set hitz to the result
set lapz to the number of items of hitz
if lapz > 0 then
-- repeat for each reference to the file
repeat until lapz = 0
select item lapz of hitz
-- get a reference to the selected text
set myInsertionPoint to item lapz of hitz
set myRef to myInsertionPoint
tell item 1 of myRef
set myInsertion to index of first character
set myInsertion2 to index of second character
set myStory to parent of first character
-- delete the string at the reference point
set contents to ""
end tell
-- place the file at the insertion point
tell (insertion point myInsertion of myStory)
place filePath
end tell
-- select the inline graphic
select text from insertion point myInsertion to insertion point myInsertion2 of myStory
set graphicLocation to object reference of selection
set myGraphic to item 1 of all graphics of item 1 of graphicLocation
set mySelection to parent of myGraphic
-- move the graphic equivalent to the baseline for that inline graphic
move mySelection by {"0pt", fileBaseline}
set lapz to (lapz - 1)
end repeat
end if
end tell
end if
end tell
end place_file
(************************************************************
* get_baseline():
* Reads in the data from the EPS equation file and extracts the baseline setting.
* The baseline is returned as a point measurment value. i.e. "5pt".
************************************************************)
on get_baseline(myFile)
set matchText to "%%Baseline: "
set theBaseline to ""
-- read in the EPS file
open for access myFile
set fileLines to read myFile as string using delimiter {return}
close access myFile
-- find the line with the baseline
set lapz to the number of items in fileLines
repeat until (lapz = 0) or theBaseline begins with the matchText
set theBaseline to item lapz of fileLines
set lapz to (lapz - 1)
end repeat
-- extract the baseline number
set theBaseline to characters -2 thru -1 of theBaseline as string
-- return the baseline number
return theBaseline & "pt" as string
end get_baseline