I have tried to include in a script a section that quits running applications and saves any open files and doesn’t bring up a dialog. I have tried three different commands:
Unfotunately, all of these cause the dialog box to appear asking if I want to save the open file. I have tried this with three different application with the same result.
Am I missing something or does the “saving yes” qualifier not function?
The command simply needs to be in an appliction tell block:
tell application "xyz"
quit saving yes
end tell
-- or simply:
tell application "xyz" to quit saving yes
Oh, also, while all applications must support the “quit” Apple Event, I don’t think that they all have to support the “saving” parameter, even when the app is document based, (I could be wrong about that). The app’s dictionary should indicate whether or not it supports the “saving” parameter.
I should have been more complete. Tried all three in “tell” blocks and they don’t work. Tested them with GraphicConverter, Word and Excel. Possibly these apps don’t support the “with save” feature.
Hm… well, I’m at home at the moment with no access to either of those 3 apps, but I have to say that I would be very surprised that they wouldn’t support the save parameter.
If you encounter an application that doesn’t use the “save” parameter with quit, but DOES allow the saving and closing of individual windows, then you could save each one manually, something along these lines:
tell application "Quits But No Save Param "
repeat with i from 1 to count windows
save window i
close window i
end repeat
quit
end tell
-- or something like that
Almost certainly not. An application is only as scriptable as the application’s programmers make it. If they didn’t want a script to be able to automatically save opened documents upon quiting, then you can’t do anything about it.
Er… correction: The System Events application allows you to act like a user in most apps, which might allow you to create a hack for dismissing dialogs when they pop up.
Turns out I was testing this by creating a new document and adding something to it. It appears that since the file only existed in RAM and not yet on disk, none of the saving options would work. If the file was an existing file opened from disk, then all the previous techniques worked.
Now all I need to do is figure out how to make it work for a document that has never been saved.
MS Word is a little tricky with new documents. You can look at this if you want. I just played around with the app until it worked.
set desk_path to (path to desktop) as string
set doc_count to 0
tell application “Microsoft Word”
activate
repeat
try
set front_doc to front document
on error
exit repeat
end try
try
set is_modified to (modified of front_doc)
if is_modified then
save front_doc
end if
close front window
beep 1
on error
beep 2
set doc_count to (doc_count + 1)
set doc_name to (“NewDoc” & doc_count & “.doc”)
set file_spec to (desk_path & doc_name) as file specification
save front document in file_spec
close front window
end try
end repeat
quit
end tell
Word seems to be confusing to some people but I don’t find it too bad. The following code is how I would close and save open documents in Word. First, it checks for documents, then it gets the path to the open documents (if found) by using the name property. If it contains a “:” then the script knows that the document has already been saved and just saves in the previous path. Otherwise the script figures it is a new file and checks for a unique name on the desktop and then saves the document there. Then it closes all documents and quits Word:
The script from jonn8 works to solve the problem with Word. Actually I have a different larger problem I’m trying to solve and have been experimenting using Word. Let me explain.
I bought a UPS with a USB connection to the computer. With Windows OS’s the UPS will institute a graceful shutdown when the battery gets low. Unfortunitley with the Mac their info states that it will be a forced shutdown.
What I am trying to do is write a script that will
1 - Detect all running visible processes (easy)
2 - Detect all open files (easy)
3 - Save all open files
4 - Quit all running visible processes
5 - Shut down the computer
The two significant requirements are that no dialogs pop up (must perform with no one at the computer) and work with app’s that are not scriptable. (I know you can tell nonscriptable app’s to do some simple things like quit. I don’t know what else you can do without resorting to the shell and I don’t know if that will work).
Obviously I still need to figure out how to intercept the force shutdown command from the UPS.
Well, again, I don’t think the shell can play any role here. The shell doesn’t provide “magic” access to an application’s compiled code. You want to be able to work with any application. Each developer is free to program their app in any way they want, with the exception of a few Apple Events they are required to handle. I think that most applications that are document-based do implement a “saving” parameter, but you can’t rely on it for every application that’s out there in Mac-land.
There is something wrong with my Mac at work. It crashes from 2 to 10 times a day. This has forced me into the habit of hitting command-s in my apps after every little change I make.
From some other sources I read, it looks like OS X intentionally left out the “Shutdown Items” folder from system 9 for security reasons. Hmm… what happens if you have classic running, with a script in the classic Shutdown Items folder? Does it get run? I’m going to chech it out…
In poking around on the web site of the UPS supplier (APC), I found the following which seems to solve part of the problem.
Prior to shutdown, PowerChute Personal Edition looks for an executable file with the following pathname:
/etc/comapcpcpe/PowerChute.pre_shutdown
This file is optional, and is not present by default. Most users will never need to create this file or the directory, as PowerChute Personal Edition does not require this file in order to shut down.
The only reason you might want to create this file is if some application has special pre-shutdown needs. The file and directory can only be created by a user with root access.
Please note that this runs as a root process, so the application launched will run with root privileges. The “/etc” directory is generally write protected to non-administrators. APC recommends that you verify the protection on this folder. You will need to create the “/etc/comapcpcpe” folder, and manually add the file named “PowerChute.pre_shutdown”, as the install does not create this file.
The code in the post works for Word. My next step was to try to generalize it to all running viewable processes. Got that going and started testing it with various apps. Then hit a snag. For Excel the following simple statement
The application is returning an object reference. In the case of 0 docs, the reference happens to point to, well, nothing. Er… I forget what you’re trying to do, but you should be able to simply “count documents” to determine the actual number that are open.
This appears to rule out AppleScript as a way of checking for open files in an app and
saving them
closing them
before quiting the app (WITHOUT ACTION BY THE USER).
Right. In fact, you can rule out everything as a way of doing that. Application developers aren’t required to have an automatic save and quit function. However, try the following, it should work pretty well with most applications that offer a modicum of scriptability:
tell application "xyz"
try
set numberOfDocs to count every document
on error e number n from f to t partial result p
-- this app isn't sufficiently scriptable
return {e, n, f, t, p}
end try
repeat with docIndex from 1 to numberOfDocs
close (document docIndex) saving yes
end repeat
end tell
Are you sure that is what it does? In an earlier posting, you mentioned a “graceful shutdown.” I just did some checking on the Internet: so far as I can tell, a “graceful shutdown” is simply a normal shutdown, meaning that the system tells each app to go quit itself, which gives the apps time to ask the user if they want to save their open documents. This is opposed to a forced shutdown, where applications aren’t given time to ask users what to do about unsaved documents.
Do you have a link to the UPS software company? I’d like to check it out.