I'm trying to write an apple script to close all open Windows. The following is what I tried:
tell application "System Events"
repeat with theProcess in (application processes where visible is true)
tell application theProcess
close
end tell
end repeat
end tell
This does not seem to work. I get:
Script Error: System Events got an error: Can't get application (item 1 of every application process whose visible = true). number -1728
I don't care if applications quit or if just the windows close.
I tried to debug this, but I could not get this script opened in Xcode.
Edit: Thanks to user3439894 I've settled on the following script which just sends Command + Q
to each of the visible applications:
tell application "System Events"
set theVisibleApps to (name of application processes where visible is true)
end tell
repeat with thisApp in theVisibleApps
try
tell application thisApp to activate
tell application "System Events"
keystroke "q" using command down
end tell
on error errMsg
display dialog errMsg
end try
end repeat
This works for me for now. As user3439894 suggested, I need to go through and understand the AppleScript Language Guide