Applescript 将选择设置为文件名

Applescript set selection to filename

我正在构建一项服务以在 finder 中创建新文件。第一步运行以下bash脚本

for f in "$@"
do
    touch ${f}/untitled.txt
done

这会创建一个名为 untitled.txt 的文件,我希望接下来发生的事情是选择该文件,如果可能的话,选择它进行重命名,就像我选择文件并按下重命名一样。就像在 windows 机器上发生的一样。那么最终结果理想情况下是我的取景器 window 看起来像这样

我试过了

tell application "Finder"
    set selection to "untitled.txt"
end tell

但这产生了一个错误。我确信这可以使用 applescript,但我找不到一个很好的例子。

快速而肮脏:

tell application "Finder"
    reveal (path to desktop folder as string) & "untitled.txt"
    activate
    delay 0.5
end tell
tell application "System Events" to keystroke return

当然,您必须获得 untitled.txt 的正确路径。 Finder 在 Finder window 中显示文件并选择它。经过最短延迟后,我们模拟按下 return-Key.

享受吧,迈克尔/汉堡