我有以下 Applescript 可以切换显示/隐藏隐藏文件,我想重新打开用户的 Finder 窗口,该窗口将在 Finder 重新启动时关闭。
tell application "Finder" to quit
set OnOff to do shell script "defaults read com.apple.finder AppleShowAllFiles"
if OnOff = "NO" or OnOff = "OFF" then
set OnOffCommand to "defaults write com.apple.finder AppleShowAllFiles ON"
else
set OnOffCommand to "defaults write com.apple.finder AppleShowAllFiles OFF"
end if
do shell script OnOffCommand
delay 1
tell application "Finder" to launch
tell application "Finder"
try
target of window 1
make new Finder window to result
on error
make new Finder window to home
end try
end tell
有人能给我指出正确的方向吗?
答案1
您可能只是在退出 Finder 时禁用关闭窗口:
defaults write com.apple.finder NSQuitAlwaysKeepsWindows -bool true
我使用这个脚本来切换显示隐藏文件:
do shell script "[[ $(defaults read com.apple.finder AppleShowAllFiles) = 1 ]] && b=false || b=true
defaults write com.apple.finder AppleShowAllFiles -bool $b"
tell application "Finder"
quit
delay 0.1 -- without this delay Finder was not made frontmost
launch
delay 0.5 -- without this delay there was sometimes a "connection is invalid" error
activate -- make Finder frontmost
reopen -- open a new default window if there are no open windows
end tell
答案2
尝试:
tell application "Finder"
set windowTargets to target of Finder windows
quit
end tell
set OnOff to do shell script "defaults read com.apple.finder AppleShowAllFiles"
if OnOff = "NO" or OnOff = "OFF" then
set OnOffCommand to "defaults write com.apple.finder AppleShowAllFiles ON"
else
set OnOffCommand to "defaults write com.apple.finder AppleShowAllFiles OFF"
end if
do shell script OnOffCommand
delay 1
tell application "Finder" to launch
tell application "Finder"
repeat with aTarget in windowTargets
make new Finder window at aTarget
end repeat
end tell