Applescript并不总是将我的变量设置到剪贴板

时间:2015-09-29 16:01:57

标签: applescript clipboard adobe-indesign

我尝试设置Indesign脚本,复制所选图像的链接信息,并将其附加到桌面的文本文件中,如果文本文件不存在,则创建一个。使用inDesign中的自定义键命令将链接信息复制到剪贴板。问题是如果我事先复制一些文本或图像,该文本仍保留在剪贴板上。即使我复制新的链接信息并将该剪贴板设置为变量。

我知道这是一个丑陋的剧本,我已经足够好了,但几乎没有。 有关如何清除剪贴板的任何建议吗?

谢谢, 〜大卫

--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

1 个答案:

答案 0 :(得分:0)

以下是您脚本的清理版本。您不需要对同一个处理程序进行两次编码...无论您何时对文件进行编码,都可以使用相同的处理程序。另外,我颠倒了append_data变量的逻辑,因为你有向后的逻辑。 (如果你要追加,那么你不要将eof设置为0,因为它会删除当前内容。)

我不知道你在InDesign中调用了什么函数,但听起来好像是将所选图像的文件路径/链接放在剪贴板上。使用剪贴板获取数据并将数据放入变量时,最好在脚本中加入一些延迟,以避免脚本在您正在使用的应用程序完成之前继续执行下一个命令。我打赌你遇到的问题是在InDesign完成复制到剪贴板之前脚本正在进行。因此,在这个脚本中,您可以看到我在剪贴板工作周围放置了三个延迟。您可以减少延迟时间,看看哪些仍然可靠。

--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

尝试这一点并根据需要进行调整,然后让我知道是否还有任何问题。