我不确定你到底想做什么,但我有个猜测。您是否希望获取掉在脚本上的每个文件,并为桌面上的每个文件创建一个符号链接?所以如果我放弃
~/look/at/me
和
~/an/example
你将会拥有
~/Desktop/me
和
~/Desktop/example
?如果这就是你想要的,那么你就走运了:
ln -s <file1> <file2> ... <directory>
就是这样。(
编辑:
尽管您必须注意两个参数的情况。)因此,您的代码可能如下所示:
-- EDITED: Added the conditional setting of `dest` to prevent errors in the
-- two-arguments-to-ln case (see my comment).
on quoted(f)
return quoted form of POSIX path of f
end quoted
on open filelist
if filelist is {} then return
set dest to missing value
if (count of filelist) is 1 then
tell application "System Events" to set n to the name of item 1 of filelist
set dest to (path to desktop as string) & n
else
set dest to path to desktop
end if
set cmd to "ln -s"
repeat with f in filelist & dest
set cmd to cmd & " " & quoted(f)
end repeat
do shell script cmd
end open
注意使用
quoted form of
它用单引号括住它的参数,所以在shell中执行不会有任何有趣的事情。
如果出于其他原因想获取文件名,则无需向查找程序发出调用;您可以使用系统事件代替:
tell application "System Events" to get name of myAlias
将返回存储在
myAlias
.
编辑:
如果你想对一个文件做点什么,这很容易。而不是使用
repeat
要遍历每个文件,只需对第一个文件执行相同的操作,由
item 1 of theList
. 所以在这种情况下,您可能需要这样的东西:
-- EDITED: Fixed the "linking a directory" case (see my comment).
on quoted(f)
return quoted form of POSIX path of f
end quoted
on open filelist
if filelist is {} then return
set f to item 1 of filelist
tell application "System Events" to set n to the name of f
do shell script "ln -s " & ¬
quoted(f) & " " & quoted((path to desktop as string) & n)
end open
基本上是一样的,但我们把第一件东西
filelist
忽略其余的。另外,在最后,我们显示一个包含符号链接名称的对话框,这样用户就知道刚才发生了什么。