我尝试使用AppleScript在桌面上的文件夹中创建文件。
set blankPage to (((path to desktop) as string) & "text.txt")
我知道path to desktop
指向桌面,但如何让它指向桌面上的文件夹ex。 ("文本文件")?
答案 0 :(得分:1)
如果文件夹存在,您可以使用Finder获取它的路径:
tell application "Finder"
set blankPage to (folder "Text files" of (path to desktop) as text) & ¬
"text.txt" as alias
end tell
要创建一个空文本文件,您可以像这样使用Finder:
tell application "Finder"
set blankPage to make new file at folder "Text files" of (path to desktop) ¬
with properties {name:"text.txt"}
end tell
使用shell是一种更快捷的方法:
do shell script "> $HOME'/Desktop/Text files/text.txt'"
答案 1 :(得分:0)
这会将上下文菜单添加到文件/文件夹以创建空文本文件,(如果存在文件,它将尝试创建无标题(1).txt等)
tell application "Finder" set fname to "untitled.txt" set i to 0 repeat while i < 999 if i > 0 then set fname to "untitled (" & i & ").txt" end if if not (file (target of Finder window 1 as string & fname) exists) then tell application "Finder" to make new file at (the target of the front window) as alias with properties {name:fname} set i to 1000 return end if set i to i + 1 end repeat end tell