Applescript 如何防止密码提示多次弹出
我的代码如下所示:Applescript 如何防止密码提示多次弹出,applescript,Applescript,我的代码如下所示: set sourceFolder1 to (path to library folder as text) & "Frameworks:SDL.framework" set sourceFolder2 to (path to library folder as text) & "Frameworks:SDL_ttf.framework" set sourceFolder3 to (path to library folder as text) &
set sourceFolder1 to (path to library folder as text) & "Frameworks:SDL.framework"
set sourceFolder2 to (path to library folder as text) & "Frameworks:SDL_ttf.framework"
set sourceFolder3 to (path to library folder as text) & "Frameworks:SDL_image.framework"
tell application "Finder"
if exists folder sourceFolder1 then
delete folder sourceFolder1
end if
if exists folder sourceFolder2 then
delete folder sourceFolder2
end if
if exists folder sourceFolder3 then
delete folder sourceFolder3
end if
end tell
当我测试它时,密码提示会弹出3次以询问密码。有没有办法防止密码提示多次弹出?谢谢
LJ查找程序询问密码,因为您试图从用户没有删除权限的位置删除文件 解决这个问题的一种方法是通过shell命令进行删除。将
删除文件夹sourcefolder1
替换为:
do shell script ("rm -rf '" & posix path of sourcefolder1 & "'") ¬
with administrator privileges user name "username" password "password"
那它就不会问了。如果您想让它询问一次,请删除用户名
之后的所有内容。它将记住后续命令的条目
更新:根据下面的评论,需要能够将项目移动到垃圾箱,而不是像rm
那样直接删除它。我想不出任何方法可以直接在AppleScript中执行,因为管理员提示来自Finder,而AppleScript(据我所知)只能通过do shell script
命令以编程方式执行特权操作。因此,我编写了一个例程,它应该相当于Finder中的“移动到垃圾桶”:
to deleteItem(thisItem)
set thisItemPath to (POSIX path of thisItem)
set uid to (do shell script "id -u")
if thisItemPath starts with "/Volumes" then
set AppleScript's text item delimiters to "/"
set volName to text item 3 of thisItemPath
set AppleScript's text item delimiters to ""
set targetPath to ¬
("/Volumes/" & volName & "/.Trashes/" & uid)
else
set targetPath to ((POSIX path of (path to home folder)) & ".Trash")
end if
set moveToTrashCmd to ¬
("mkdir -p '" & targetPath & "'; " & ¬
"mv '" & thisItemPath & "' '" & targetPath & "'")
-- set global vars first for userName and password, or omit for prompt
do shell script moveToTrashCmd ¬
with administrator privileges user name userName password userPw
end if
end deleteItem
下面是一个使用例程的示例程序:
global fileItems, userName, userPw
on run
set userPw to "password"
set AppleScript's text item delimiters to ":"
set userName to text item -2 of (path to home folder as string)
set AppleScript's text item delimiters to ""
try
fileItems
on error
set fileItems to ¬
(choose folder with prompt ¬
"Select folders to delete:" with multiple selections allowed)
end try
repeat with thisItem in fileItems
deleteItem(thisItem)
end repeat
end run
-- drag 'n' dropped files
on open docs
set fileItems to docs
run
end open
要防止密码提示多次弹出:请将所有文件夹或文件放入列表中,仅使用
delete
命令一次
像这样:
set frmWKsList to {}
set librF to path to library folder as text
set end of frmWKsList to librF & "Frameworks:SDL.framework"
set end of frmWKsList to librF & "Frameworks:SDL_ttf.framework"
set end of frmWKsList to librF & "Frameworks:SDL_image.framework"
set foldersToDelete to {}
tell application "Finder"
repeat with i in frmWKsList
if exists folder i then set end of foldersToDelete to i as alias
end repeat
delete foldersToDelete
end tell
谢谢你提供的信息。但是,如果我使用rm-rf,文件夹将被删除,而不是像删除文件夹那样移动到垃圾箱文件夹。有没有办法用applescript做类似的事情?我使用applescript的原因是,我可以将文件夹移动到垃圾箱,然后,如果用户希望放回文件夹,他们可以选择放回。如果我使用shell脚本,则没有可用的返回选项。谢谢。我更新了命令,提供了一种无需管理员提示即可移动到垃圾箱的方法。