My daughter and her husband don’t like the current Apple mouse, and use a generic bluetooth mouse on their iMac. Lately, they started complaining that they couldn’t clear their desktop to get at icons on it.
So, I wrote the following script to clear, and restore, the desktop. It needs saving as an don't-stay-open Application, and works best residing in the Dock.
It requires Accessibility Access, and will open the preference pane to set it.
Application & Finder windows restore settings work across reboots.
If you can use it, feel free. Distribute freely.
-- A desktop window closer & opener
-- Applescript by Brian Christmas
on run
tell application "Finder"
try
activate
set pathToApp to path to current application as text
try
set pathToAppFile to pathToApp & "Contents:appWindows.dat" as text
set appWindows to read file pathToAppFile
on error
set appWindows to {}
end try
try
set pathToFinderFile to pathToApp & "Contents: finderWindows.dat" as text
set finderWindows to read file pathToFinderFile
on error
set finderWindows to {}
end try
if (count of appWindows) = 0 and (count of finderWindows) = 0 then
if (count of appWindows) = 0 then
set saveWindows to true
set appWindows to name of every process whose visible is true and name is not "Finder" as list
set actualAppWindows to every process whose visible is true and name is not "Finder" as list
repeat with eachWindow in actualAppWindows
try
set visible of eachWindow to false
on error errmsg
display dialog "actualAppWindows " & errmsg
end try
end repeat
set fRef to (open for access file pathToAppFile with write permission)
set eof fRef to 0
repeat with x from 1 to count of appWindows
try
set eachWindow to item x of appWindows as text
write eachWindow & return as text to fRef
on error errmsg
display dialog "appWindows " & errmsg
end try
end repeat
close access fRef
end if
if (count of finderWindows) = 0 then
set finderWindows to {}
set theWindows to every window
repeat with eachWindow in theWindows
try
set end of finderWindows to (target of eachWindow as alias) as text
end try
end repeat
close windows
set fRef to (open for access file pathToFinderFile with write permission)
set eof fRef to 0
repeat with x from 1 to count of finderWindows
try
set eachWindow to item x of finderWindows as text
write eachWindow & return as text to fRef
on error errmsg number errnum
display dialog "Finder windows targets " & errmsg & return & return & errnum
end try
end repeat
close access fRef
end if
else
set DockError to false
tell application "Dock"
activate
end tell
tell application "System Events"
tell process "Dock"
set frontmost to true
activate
tell list 1
repeat with x from 1 to count of paragraphs of appWindows
if not DockError then
set eachWindow to paragraph x of appWindows as text
if eachWindow ≠ "Desktop Clearer" and eachWindow ≠ "" then
try
click UI element (eachWindow as text)
on error errmsg number errnum
if errnum = -1719 then
set DockError to true
display dialog "Desktop Clearer requires access to 'Application Accessibilty'. I will now open the appropriate 'System Preferences' pane so you can set it." giving up after 5 buttons {"OK"}
tell application "System Preferences"
set securityPane to pane id "com.apple.preference.security"
tell securityPane to reveal anchor "Privacy_Accessibility"
activate
end tell
end if
end try
end if
end if
end repeat
end tell
end tell
end tell
repeat with x from 1 to count of paragraphs of finderWindows
set eachWindow to paragraph x of finderWindows as text
try
if eachWindow ≠ "" then open eachWindow as text
on error errmsg number errnum
display dialog "Open finder window " & errmsg & return & return & errnum
end try
end repeat
#
# Now overwrite files to blank
#
set fRef to (open for access file pathToAppFile with write permission)
set eof fRef to 0
try
write "" to fRef
on error errmsg number errnum
display dialog "Overwriting app file " & errmsg & return & return & errnum
try
close access fRef
end try
end try
close access fRef
set fRef to (open for access file pathToFinderFile with write permission)
set eof fRef to 0
try
write "" to fRef
on error errmsg number errnum
display dialog "Overwriting finder file " & errmsg & return & return & errnum
try
close access fRef
end try
end try
close access fRef
end if
on error errmsg number errnum
activate
display dialog errmsg & return & return & errnum
end try
end tell
end run