我一直试图写一个脚本来回放"放回去#34;垃圾箱中的文件。现在的代码如下:
property file_list : {}
property cur_file : ""
# Using system events because we need to use mouse clicks
# First hide everything but the Finder
tell application "System Events"
tell process "Finder"
click menu item "Hide Others" of menu 1 of menu bar item "Finder" of menu bar 1
end tell
# The "Put Back" menu item is only available when the special Dock-launched Trash
# window is active. It's not available when the Trash is accessed from a
# regular Finder window (i.e. Go To Folder..., etc.)
tell process "Dock"
click UI element "Trash" of list 1
end tell
get the POSIX path of every disk item of trash
# discard the first item, which is (should be?) ".DS_Store"
set file_list to items 2 thru -1 of result
repeat until length of file_list is 0
set cur_file to first item of file_list
tell process "Finder"
# the next line is where the error occurs
select (file where path is cur_file) # this is where the error occurs
# error "Can't get file whose path = "/Full/path/to/file" number -1728
click menu item "Put Back" of menu 1 of menu bar item "File" of menu bar 1
end tell
end repeat
end tell
如脚本注释中所述,“废纸篓”窗口必须是最重要的,并且在尝试在该窗口中查找某些内容时脚本会失败。我也试过了
get the name of every disk item of trash
与
click file where name is cur_file
但得到的结果基本相同 - "无法获取名称为" nameOfFile"编号-1728
我会继续关注此事,但可以使用一些帮助;我对AppleScript并不熟悉。
[编辑] 所以这就是我最终想出来的:
property file_list : {}
property cur_file : ""
property loop_counter : 0
# Using system events because we need to use mouse clicks
# First hide everything but the Finder
tell application "Finder" to close every window
tell application "System Events"
tell process "Finder"
click menu item "Hide Others" of menu 1 of menu bar item "Finder" of menu bar 1
end tell
# Get all files in Trash except for the first, which is probably ".DS_Store"
set file_list to items 2 thru -1 of (get the POSIX path of every file of trash)
repeat until loop_counter is equal to length of file_list
set loop_counter to loop_counter + 1
# Pause for a few seconds every so often to let the computer chill out.
# Can be changed if necessary.
if (loop_counter mod 200) = 0 then delay 20
# Try to get file from list of names
try
set cur_file to item loop_counter of file_list
end try
# Make the Trash window active. Must be done with mouse click on
# Dock icon in order to make the "Put Back" menu item available.
tell process "Dock" to click UI element "Trash" of list 1
# Select the next item to put back
tell application "Finder"
activate
try
set target of window "Trash" to (POSIX file cur_file)
end try
end tell
# Delay to allow UI to catch up with script.
delay 0.25
# Try to put back the selected item. For some reason doesn't always work.
try
tell process "Finder" to click menu item "Put Back" of menu "File" of menu bar 1
end try
# Delay to allow UI to catch up with script.
delay 0.25
tell application "Finder" to close every window
end repeat
end tell
不漂亮,但它确实有效。尝试块是脚本有时会超出UI并失败的地方。使用它们,脚本将忽略该文件并继续下一个文件。然而,0.25秒的延迟似乎有助于防止这种情况发生。
我对使用命令行的解决方案感兴趣,因为它运行的方式会占用UI,直到完成或停止。
答案 0 :(得分:0)
我认为,你的方法过于复杂......我在Applescript中尝试了一些简短而干净的东西,它就像一个魅力:
-- preparing an empty list for later use
set selectedFiles to {}
-- asking the Finder for files in trash
tell application "Finder"
activate
-- check if the trash contains files
if (count of (trash's files)) > 0 then
-- open Finder's trash window
open trash
-- select all files in Finder's trash window
set selectedFiles to (select files of front window)
end if
end tell
-- if files are selected, press backspace and command together
if (count of selectedFiles) > 0 then
tell application "System Events"
tell process "Finder"
-- Press backspace using Command down
key code 51 using command down
end tell
end tell
end if
享受,迈克尔/汉堡
答案 1 :(得分:0)
这对我有用。 macOS High Sierra
tell application "System Events"
tell process "Finder"
repeat 100 times
tell application "Finder" to open trash
tell application "Finder" to activate
key code 126
key down command
key code 51
key up command
delay 0.2 -- adjust delay as needed
end repeat
end tell
end tell
tell application "Finder" to close every window
答案 2 :(得分:0)
这对我在macOS Catalina上有用
tell application "Finder"
activate
set filesInTrash to (files of trash)
repeat with f in filesInTrash
select f
tell application "System Events" to key code 51 using {command down}
-- Workaround: Give Finder time to show file on new location
select f
end repeat
end tell
答案 3 :(得分:0)
只需添加到混合中(并且因为我绝对不喜欢使用Finder,所以不喜欢它):
tell application "System Events"
set trash_folder to open trash
set trash_items to disk items of trash_folder
repeat with this_item in trash_items
tell application "Finder" to select (this_item as alias)
tell process "Finder"
tell first menu bar's menu "File"'s menu item "Put Back"
click
end tell
tell window 1 to cancel
end tell
end repeat
end tell
我不知道为什么“告诉窗口1取消”成功地将新打开的窗口发送到后台,但是它似乎可以工作...