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

Applescript并不总是将我的变量设置到剪贴板,applescript,clipboard,adobe-indesign,Applescript,Clipboard,Adobe Indesign,我正在尝试设置一个Indesign脚本,该脚本复制选定图像的链接信息并将其附加到桌面的文本文件中,如果文本文件不存在,它将创建一个。使用inDesign中的自定义键命令将链接信息复制到剪贴板。问题是,如果我事先复制了一些文本或图像,这些文本将保留在剪贴板上。即使我复制了新的链接信息并将剪贴板设置为变量 我知道这是一个丑陋的剧本,我只是过得去,但勉强过得去。 关于如何清除剪贴板有什么建议吗 谢谢, ~z~大卫 这是您的脚本的清理版本。您不需要对同一个处理程序编写两次代码。。。两次对文件进行右键操作

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

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

谢谢, ~z~大卫


这是您的脚本的清理版本。您不需要对同一个处理程序编写两次代码。。。两次对文件进行右键操作时,都可以使用相同的处理程序。另外,我颠倒了append_数据变量的逻辑,因为逻辑是反向的。如果添加,则不会将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

试试这个,根据需要进行调整,然后告诉我是否有任何东西仍然不起作用。

仍然不起作用。奇怪的是,它似乎落后了一步。如果我在选定的图像上运行它,它会将我以前的剪贴板粘贴到文件中。然后,如果我在图像B上再次运行它,它会将图像A信息粘贴到文件中。拖延似乎没有什么帮助。奇怪,有什么想法吗?你是怎么调用脚本的?如果没有安装InDesign,我真的帮不上什么忙。试着这样做,看看脚本的流程是否合理:将其从:告诉应用程序Adobe InDesign…更改为TextEdit,然后使用{command down}将:keystroke l…更改为:keystroke c。然后,只需在TextEdit中突出显示一个单词或短语并运行脚本。InDesign有一个脚本面板,链接到Home/library中的脚本文件夹。通过这种方式,您可以为脚本分配功能键。然而,由于您的建议,我尝试从脚本编辑器运行它,它工作正常。它也适用于脚本编辑器中的文本编辑。我不知道如何在textedit中运行它。您在InDesign中使用Shift控件L调用什么?菜单项?这是内置的快捷键,还是自定义的?您可以将脚本放在~/Library/Scripts文件夹中,然后从系统范围的脚本菜单运行它。这是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