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

Applescript is not always setting my variable to the clipboard

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

我知道这是一个丑陋的剧本,我只是勉强过得去,但勉强。 关于如何清除剪贴板有什么建议吗?

谢谢, ~大卫


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

这是您脚本的清理版本。您不需要对同一个处理程序进行两次编码...两次您都有权访问该文件,您可以使用同一个处理程序。另外,我颠倒了 append_data 变量的逻辑,因为你的逻辑是倒过来的。 (如果您确实追加,则不要将 eof 设置为 0,因为这会擦除当前内容)。

我不知道您在 InDesign 中调用的是什么函数,但听起来好像是将所选图像的文件 path/link 放到剪贴板上。使用剪贴板获取数据并将数据放入变量时,最好在脚本中放置一些延迟,以避免脚本在您正在使用的应用程序完成工作之前继续执行下一个命令。我敢打赌,您遇到的问题是脚本在 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

试一试并根据需要进行调整,如果仍有任何问题,请告诉我。