Search code examples
applescriptclipboardadobe-indesign

Applescript is not always setting my variable to the clipboard


I'm trying to set up an Indesign script that copies the link information of a selected image and appends it to a text file to the desktop, if the text file does not exist it creates one. The link information is copied to the clipboard using a custom key command in inDesign. The problem is if I copy some text or an image beforehand, that text remains on the clipboard. Even though I copy new link info and set that clipboard to a variable.

I know this is an ugly script, I'm just good enough to get by, but barely. Any suggestions on how to clear out the clipboard?

Thanks, ~David


--dialog box

display dialog "What do you need done" default answer "Remove Background"
set theAnswer to (text returned of result)



set this_story to "

------------------- " & theAnswer & " -------------------

"
set this_file to (((path to desktop folder) as string) & "Corrections.txt")
my write_to_file(this_story, this_file, false)


on write_to_file(this_data, target_file, append_data)
    try
        set the target_file to the target_file as string
        set the open_target_file to open for access file target_file with write permission
        if append_data is true 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


--copy link information using keyboard short

activate application "Adobe InDesign CC 2015"

tell application "System Events" to keystroke "l" using {shift down, control down}

end

---

set myVar to the clipboard
my write_clip_file(myVar, this_file, false)

on write_clip_file(myVar, target_file, append_data)
    try
        set the target_file to the target_file as string
        set the open_target_file to open for access file target_file with write permission
        if append_data is true then set eof of the open_target_file to 0
        write myVar 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_clip_file

Solution

  • Here is a cleaned up version of your script. You don't need to code the same handler twice... both times you right to the file, you can use the same handler. Also, I reversed the logic on the append_data variable, because you had the logic backwards. (if you DO append, then you DON'T set eof to 0 cause that erases the current content).

    I don't know what function you're calling in InDesign, but sounds like it's placing the file path/link of a selected image onto the clipboard. When using the clipboard to get data and to place the data into a variable, it's a good practice to put some delays in the script, to avoid the script moving on to the next commands before the app you're working with finishes it's work. I'm betting that the problem you're having is that the script is proceeding before InDesign has finished copying to the clipboard. So, in this script, you can see I placed three delays surrounding the work with the clipboard. You can play with reducing the delay times to see what will still work reliably.

    --dialog box
    display dialog "What do you need done" default answer "Remove Background"
    set theAnswer to (text returned of result)
    set this_story to "
    
    ------------------- " & theAnswer & " -------------------
    
    "
    
    set this_file to (((path to desktop folder) as string) & "Corrections.txt")
    my write_to_file(this_story, this_file, true)
    
    --copy link information using keyboard short
    tell application "Adobe InDesign CC 2015"
    -- tell application "TextEdit" -- for testing purposes
        activate
        delay 0.9 -- give app time to come to the front before copying to clipboard
        tell application "System Events" to keystroke "l" using {shift down, control down}
    -- tell application "System Events" to keystroke "c" using {command down} -- for testing purposes
        delay 0.9 -- wait til clipboard is copied before continuing with the script
    end tell
    
    set myVar to the clipboard
    delay 0.1 -- wait til variable is pulled from clipboard before moving on
    
    my write_to_file(myVar, this_file, true)
    
    --
    
    on write_to_file(this_data, target_file, append_data)
        try
            set the target_file to the target_file as string
            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
    

    Try this out and tweak it as you need to, and then let me know if anything is still not working.