如何使用AppleScript从剪贴板复制文件?

问题描述:

我有这个shell脚本解析符号链接(符号链接)到一个文件夹,并将其复制到当前的工作目录:如何使用AppleScript从剪贴板复制文件?

cp -R `grealpath /some/dir/symlink_to_folder` . 

命令grealpath只是解决了一个符号链接时的coreutils在Mac安装OS X.

我的问题是:什么是作为AppleScript的等价物?工作流程将如下所示:将Finder中的一个或多个符号链接文件夹复制到剪贴板,然后右键单击Finder中的另一个文件夹 - >服务 - >运行新创建的脚本以复制剪贴板中的所有符号链接到其他文件夹。

+0

当您运行服务脚本,你想复制到新目录的符号链接,或者它指向的文件夹的内容? –

+0

我想复制这些内容。例如。复制符号链接“.../some_dir_1”和“.../some_dir_2”应该创建文件夹“some_dir_1”和“some_dir_2”,并在文件夹中包含文件的真实副本(包含引用的符号链接(resloved))。 – Pwdr

你可以这样创建一个服务:

cp -R "$(/usr/local/bin/grealpath "$(osascript -e 'POSIX path of «class furl» of (the clipboard as record)')")" "$1"

如果剪贴板中包含文件的引用,«class furl» of (the clipboard as record)返回的第一个文件一个文件对象。您可能还会使用cp -RH source target而不是cp -R `grealpath source` target。如果符号链接和它的目标名称不同,它将使用符号链接的名称。

-H If the -R option is specified, symbolic links on the command line 
     are followed. (Symbolic links encountered in the tree traversal 
     are not followed.) 
+0

运行此代码时出现错误。你能以纯文本的形式发布命令吗?也许错误是因为引号不同!? – Pwdr

+0

我编辑了答案。在使用该服务之前,您是否将一个符号链接复制到Finder中的文件夹? '/ usr/local/bin/grealpath'中的'grealpath'?您是否将“通过输入”设置为“作为参数”?或者,如果您尝试从Automator运行该服务,则它不起作用,因为没有输入。 – user495470

+0

太棒了,现在它可以与第一个选择一起使用!你知道要改变它以使它与多个项目一起工作吗? – Pwdr

Applescript会将一个符号链接视为另一个文件夹,因此您只需复制这些内容。

Tell application "Finder" 
    repeat with i from 1 to (count symlinks) 
    set newFolder to make new folder at destFolder with properties {name:item i of symlinks) 
    duplicate entire contents of item i of symlinks to newFolder 
end