这是我的情况。我创建了一个用于清理桌面的AppleScript。它实际上选择了我桌面上的所有文件和文件夹,创建了一个具有特定名称和日期格式的新文件夹,并将所有文件从我的桌面移动到新文件夹。这个脚本运行得很漂亮,直到我意识到偶尔会有我希望保留在桌面上的文件或文件夹。我的解决方案是打开我希望保留在桌面上的每个文件或文件夹的获取信息窗口,并在获取信息窗口中选择“锁定”选项。
我的问题现在是因为锁定的文件,脚本无法完全执行而不显示错误消息。如果我在错误消息上单击“确定”,脚本将完成并移动每个文件,但锁定的文件除外。
我宁愿不必通过单击“okay”按钮来完成这一额外步骤。我尝试将一些系统事件操作添加到脚本中以自动单击“确定”按钮,但这不起作用。
我开始认为唯一真正的解决方案是首先不选择那些锁定的文件或文件夹。这是我迷失的地方。任何人都可以帮我指导如何避免首先选择锁定的文件?
这是完整的脚本。
set scriptPath to (load script file "Macintosh HD:Users:Smokestack:Library:Mobile Documents:com~apple~ScriptEditor2:Documents:Cleanup Desktop.scptd:Contents:Resources:Scripts:Current Time A.M. P.M. And Short Date.scpt")
tell scriptPath
timeandDate() -- This will get the time and date in this format "05/31/2017 @ 9:10:48 PM" called from the loaded script file above
end tell
set timeandDate to the result -- This will copy the time and date results from the previous step and and set it as this new variable
tell application "Finder"
if running then
close every window
activate
make new Finder window
set target of Finder window 1 to folder "Desktop" of folder "Smokestack" of folder "Users" of startup disk
end if
open Finder window 1
activate
end tell
delay 1
tell application "System Events"
key code 0 using (command down) -- This will select all files and folders on the desktop in the active finder window
end tell
tell application "Finder"
set these_items to the selection
set destinationFolder to make new folder at POSIX file "/Users/Smokestack/Jimz_Important_Stuff/Desktop_Cleanups/" with properties {name:timeandDate}
move these_items to destinationFolder
reveal destinationFolder
end tell
这是脚本的修订版本,无需从外部文件调用处理程序
on timeandDate()
set CurrentTime to (time string of (current date))
set AppleScript's text item delimiters to ","
set theLongDate to (current date)
set theLongDate to (date string of theLongDate)
set currentMonth to (word 1 of text item 2 of theLongDate)
set currentDay to (word 2 of text item 2 of theLongDate)
set currentYear to (word 1 of text item 3 of theLongDate)
set monthList to {January, February, March, April, May, June, July, August, September, October, November, December}
repeat with x from 1 to 12
if currentMonth = ((item x of monthList) as string) then
set theRequestNumber to (text -2 thru -1 of ("0" & x))
exit repeat
end if
end repeat
set currentMonth to theRequestNumber
set currentDay to (text -2 thru -1 of ("0" & currentDay))
set theShortDate to (currentMonth & "/" & currentDay & "/" & currentYear) as string
set CurrentTime to (time string of (current date))
set CurrentTimeandShortDate to (theShortDate & " @ " & CurrentTime)
set AppleScript's text item delimiters to ""
end timeandDate
timeandDate()
set timeandDate to the result
tell application "Finder"
close every window
activate
make new Finder window
set target of Finder window 1 to folder "Desktop" of folder "Smokestack" of folder "Users" of startup disk
select every file of the front Finder window
delay 1
set these_items to the selection
set destinationFolder to make new folder at POSIX file "/Users/Smokestack/Jimz_Important_Stuff/Desktop_Cleanups/" with properties {name:timeandDate}
try
move these_items to destinationFolder
end try
reveal destinationFolder
end tell
在对脚本进行一些修改之后,只要桌面上锁定的项目只是文件夹而不是文件,现在这个脚本就能很好地工作。但是,如果存在任何单独的锁定文件,它仍会生成错误。