Applescript - 如果文件夹不存在则创建一个文件夹 [英] Applescript - creating a folder if it doesn't exist
问题描述
有人可以指出为什么 Applescript 的这一点不起作用吗?谢谢!
Can someone please point out why this bit of Applescript isn't working? Thanks!
on open droppedItems
tell application "Finder"
set inputFolder to (container of first item of droppedItems) as Unicode text
if (exists folder ("converted") of inputFolder) then
set outputFolder to (inputFolder & "/converted/") as text
else
make new folder at inputFolder with properties {name:"converted"}
set outputFolder to the result as text
end if
end tell
end
推荐答案
我使用了以下版本.我留下了说"命令.使用 say 命令是一种很好的调试技术.
I got the following version to work. I left in the "say" commands. Using say commands is a good debugging technique.
on open droppedItems
say "on open"
tell application "Finder"
set inputFolder to (container of first item of droppedItems) as Unicode text
set convertedFolderPath to inputFolder & "converted:"
if (exists (folder convertedFolderPath)) then
say "converted folder exists"
set outputFolder to (inputFolder & "/converted/") as text
else
say "converted folder does not exist"
make new folder at inputFolder with properties {name:"converted"}
set outputFolder to the result as text
end if
end tell
say "end open"
end open
---编辑---
哦,这是带有Automator"标签的.如果您的代码在运行 AppleScript"的 Automator 操作中,那么它不应该有打开的dropItems".在 Automator 中,脚本应如下所示:
Oh, this is tagged with an "Automator" tag. If your code is within an Automator action of "Run AppleScript", then it shouldn't have the "on open droppedItems". In Automator, the script should look like the following:
on run {input, parameters}
-- Enter your scripting here (without the "on open droppedItems" part)
return input
end run
---编辑2---
好的.我知道该路径部分是 HFS,部分是 POSIX.有趣的是,它确实在我的计算机上工作,用于创建新文件夹和检测文件夹已经存在,但这里是我的代码,它被固定为具有 HFS 路径,没有任何部分是 POSIX pah:>
OK. I understand that the path was part HFS and part POSIX. The funny thing is that it did work on my computer for both creating a new folder and for detecting that a folder already existed, but here is my code that is fixed to have an HFS path without any part ofis being a POSIX pah:
on open droppedItems
say "on open"
tell application "Finder"
set inputFolder to (container of first item of droppedItems) as Unicode text
set convertedFolderPath to inputFolder & "converted:" ---- changed this ----
if (exists (folder convertedFolderPath)) then
say "converted folder exists"
set outputFolder to convertedFolderPath
else
say "converted folder does not exist"
make new folder at inputFolder with properties {name:"converted"}
set outputFolder to the result as text
say "created folder"
end if
end tell
say "end open"
end open
这篇关于Applescript - 如果文件夹不存在则创建一个文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!