Rename and Relinking Images in QuarkXPress using AppleScript

set piclist to {}
set datlist to {}
global missed
set missed to {}
global dat, datlist, fileID
global textFile, available
set SourceFile to (choose file with prompt "Choose any quark document file...")
set textFile to (choose file with prompt "Choose text file ...")
set thepath to choose folder with prompt "Select folder to images in..."

tell application "Finder"
    if (file textFile exists) then
        set fileID to open for access textFile
        try
            set myfilecontents to read textFile
        on error
            display dialog "Check the text file..."
            return
        end try
        close access textFile
    end if
end tell

tell application "QuarkXPress"
    try
        open SourceFile use doc prefs yes remap fonts no do auto picture import no
    on error
        display dialog "check the file is already opened..."
        return
    end try

    tell document 1
        set pb to count of images

        set the Tmppara_list to every paragraph of myfilecontents
        repeat with i from 1 to count of paragraph of myfilecontents
            set Tmpln to i
            set Tmp_p1 to paragraph i of myfilecontents
            set TmpTextpara to Tmp_p1
            set prevTIDs to text item delimiters of AppleScript
            set text item delimiters of AppleScript to tab
            --set subject to text items of "A   B"
            set subject to text items of TmpTextpara
            set srcImg to item 1 of subject
            set desImg to item 2 of subject
            set text item delimiters of AppleScript to prevTIDs
            --end repeat
            set available to false
            repeat with i from 1 to count of images
                tell image i
                    set ImageBox to object reference of picture box 1
                    -- Copy writable properties
                    set Img_file_path to (file path) as text
                    if (Img_file_path ? "null" and Img_file_path ? "no disk file") then
                        set theImageName to my NameFromPath(Img_file_path)
                        if (theImageName ? "null" and theImageName ? "no disk file") then
                            if theImageName = srcImg then
                                set available to true
                                if not missing then
                                    set Img_angle to angle
                                    set Img_color to color
                                    set Img_greek_pictures to greek pictures
                                    set Img_image_trap to image trap
                                    set Img_invert_runaround to invert runaround
                                    --set Img_model to model
                                    set Img_name to name
                                    --set Img_negative to negative
                                    set Img_offset to offset
                                    set Img_scale to scale
                                    set Img_screen to screen
                                    set Img_screen_angle to screen angle
                                    set Img_screen_frequency to screen frequency
                                    set Img_screen_function to screen function
                                    set Img_shade to shade
                                    set Img_show_half_tone to show half tone
                                    set Img_skew to skew
                                    set Img_suppress_printing to suppress printing

                                    set Img_file_path to (file path) as text

                                    tell application "Finder"
                                        try
                                            set the datlist to files of thepath
                                        end try
                                        repeat with dat in datlist
                                            set fname to name of dat as string
                                            if fname = srcImg then
                                                set name of dat to desImg
                                                exit repeat
                                            end if
                                        end repeat
                                    end tell

                                    -- Get new image path
                                    set Img_file_path to (file path) as text
                                    set Img_file_path to my FindReplace(srcImg, desImg, Img_file_path)

                                    -- Re-import picture
                                    try
                                        set image 1 of ImageBox to file Img_file_path

                                        -- Re-set image properties
                                        tell image 1 of ImageBox
                                            set angle to Img_angle
                                            set color to name of Img_color
                                            set greek pictures to Img_greek_pictures
                                            set image trap to Img_image_trap
                                            set invert runaround to Img_invert_runaround
                                            --set model to Img_model
                                            set name to Img_name
                                            --set negative to Img_negative
                                            set offset to Img_offset
                                            set scale to Img_scale
                                            set screen to Img_screen
                                            set screen angle to Img_screen_angle
                                            try
                                                set screen frequency to Img_screen_frequency
                                            end try
                                            set screen function to Img_screen_function
                                            set shade to Img_shade
                                            set show half tone to Img_show_half_tone
                                            set skew to Img_skew
                                            set suppress printing to Img_suppress_printing
                                        end tell
                                    end try
                                end if
                                --else
                                --available = false
                            end if
                        end if
                    end if
                end tell

            end repeat
            if available = false then
                --display dialog srcImg
                set missed to missed & srcImg & return
            end if
        end repeat
    end tell
    save document 1
    --copy (choose file name default name "missed" & ".txt") as string to dataFile
    set SourceFile to SourceFile as string
    set dataFile to my FindReplace(".qxd", ".txt", SourceFile)
    try
        open for access file (dataFile) with write permission
        set eof of file (dataFile) to 0
        write item 1 of missed to file (dataFile) starting at 0
        repeat with i from 2 to length of missed
            try
                write item i of missed to file (dataFile)
            end try
        end repeat
        close access file (dataFile)
    on error errtext number errNum
        close access file (dataFile)
        tell application "QuarkXPress"
            activate
            --          display dialog errtext
            display dialog "No missed files..."
        end tell
    end try

    display dialog "completed"
    return
end tell

on FindReplace(FindWhat, ReplaceBy, ThisString)
    copy the text item delimiters to OldDelims
    set the text item delimiters to {FindWhat}
    set TempList to every text item of ThisString
    set the text item delimiters to {ReplaceBy}
    set NewString to TempList as text
    set the text item delimiters to OldDelims
    return NewString
end FindReplace

on NameFromPath(theImagePath)
    set theOffset to the offset of ":" in (the reverse of every character of theImagePath) as string
    set theImageName to (characters -(theOffset - 1) thru -1 of theImagePath) as string
    return theImageName
end NameFromPath