0

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
dnest007
  • 35
  • 7

1 Answers1

0

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.

jweaks
  • 3,674
  • 1
  • 19
  • 32
  • Still not working. Its strange, it seems as though its one step behind. If I run it on selected image A it will paste my previous clipboard to the file. Then if I run it again on image B it will paste image A information to the file. The delay does not seem to help. Strange, any thoughts on that? – dnest007 Sep 30 '15 at 20:03
  • How are you invoking the script? I really can't help more without having InDesign installed. Try this to see that the flow of the script is sound: change it from: tell application "Adobe InDesign…" to "TextEdit" and then change: keystroke "l"… to: keystroke "c" using {command down}. Then, just highlight a word or phrase in TextEdit and run the script. Is that working as expected? – jweaks Sep 30 '15 at 20:39
  • InDesign has a script panel that links to a script folder in Home/library. This way you can assign function keys to the scripts. However because of your suggestion I tried running it from the script editor and it works. It also works on TextEdit from the script editor. I'm not sure how to run it from within textedit. – dnest007 Sep 30 '15 at 20:52
  • What are you invoking with the Shift Control L in InDesign? A menu item? Is that the builtin key shortcut, or is it custom? You could put the script in the ~/Library/Scripts folder and run it from the system wide script menu. – jweaks Sep 30 '15 at 21:01
  • Its a custom keyboard shortcut from within InDesign. Very frustrating. I will try to relocate it and let you know. Thanks for all your help and explanations. – dnest007 Sep 30 '15 at 21:19
  • If the script works fine from Script Editor, then the problem is with how launching from within InDesign handles it. A solution would be to use the system-wide script menu instead to invoke. Or, wrap it in an Automator Service. – jweaks Sep 30 '15 at 21:52