此问题与AppleScript set directory path in Finder类似,但的答案对我不起作用!
local destination, libraryName, f
set destination to "/Users/bryandunphy/Music/Result"
set libraryName to "Testing"
if not ((libraryName ends with ".xml") or (libraryName ends with ".XML")) then set libraryName to libraryName & ".xml"
menuClick({"iTunes", "File", "Library", "Export Library…"})
set f to "/Users/bryandunphy/Music/Result/Testing.xml" -- (destination & "/" & libraryName) as string
tell application "Finder"
if POSIX file f exists then
delete f
beep
end if
end tell
tell application "System Events" to tell process "iTunes" to tell its front window to set the value of its text field "Save As:" to libraryName
if switchDir(destination, "iTunes", true, true) then return destination & "/" & libraryName -- first true = click default (Save) button, last = create path if needed
" if switchDir" line导致"文件存在,替换?"对话框出现。
答案 0 :(得分:0)
通过将答案与其他两个“相关”问题相结合,找到答案!
“tell block”应替换为
if f begins with "~" then tell application "System Events" to set f to ((home directory of current user) & items 2 through -1 of (get f))
tell application "System Events" to if exists disk item (POSIX file f as string) then delete disk item (POSIX file f as string)
答案 1 :(得分:0)
最简单的解决方案是在alias
上下文中使用"System Events"
个对象:
set f to "/Users/bryandunphy/Music/Result/Testing.xml" # sample POSIX path
tell alias f of application "System Events"
if it exists then delete it
end tell
警告:这会立即删除目标文件,而不是将其移动到垃圾文件夹。
要执行后者,请参阅下面基于Finder
的解决方案。
在"System Events"
上下文中,alias
个对象可以直接使用POSIX路径,也可以执行文件操作。
出于性能原因,在"System Events"
上下文而不是"Finder"
上下文中工作通常是首选,但请注意,某些操作不仅可以在"Finder"
上下文中使用 - 将文件移动到垃圾而不是立即删除它就是一个例子。
至于为什么"Finder"
- 上下文方法不起作用:
AppleScript的方式很神秘:POSIX file f exists
不工作 - 它不会检测文件的存在 - 但f as POSIX file exists
会这样做:
set f to "/Users/bryandunphy/Music/Result/Testing.xml" # sample POSIX path
tell application "Finder"
if f as POSIX file exists then delete f as POSIX file
end tell
另一个怪癖:tell f as POSIX file ... end tell
块中包含if it exists then delete it
- 类似于上面的"System Events"
解决方案 - 不工作。
与顶部的"System Events"
解决方案不同,这是:
file
对象(已在新位置,垃圾箱文件夹中)。