MacOSX autosave

From WikiHistedOrg

Jump to: navigation, search

The iWork applications -- Numbers, Pages, Keynote -- have no autosave function.

This is a script that saves all open files in each of the applications, and can be extended to other scriptable MacOS applications.

To get this to work

  1. Cut and paste the script below into ScriptEditor
  2. Edit the backup path location, and create a directory to point it at
  3. Save it as a script; I did this in ~/Library/Scripts/auto-save-apps.app
  4. Create a ~/crontab file as listed below
  5. At the terminal type 'crontab crontab' to install it

changes

  1. 1/5/08: updated to only save if modified. Also does not save if Untitled or if imported (e.g. from .doc or .ppt file)

Note that Numbers 08 does not have scripting support so this works only for Pages and Keynote

  1. 1/8/08: save it as a script rather than as an application; this is because if you are holding down the Control key when a script application is run, it pops up a dialog asking you if you want to run the script. That requires using osascript in your crontab


Open the following script in your editor

set backupDirectory to "/Users/histed/backups" -- no trailing slash

set timeStamp to do shell script "date -n +%Y%m%d-%H%M%S"
set saveRootName to backupDirectory & "/autosave-" & timeStamp

-- Define function that does the backup for each app
on doSaveToBackupDir(appName, saveRootName)
	
	tell application "System Events" to set isRunning to (name of processes) contains appName
	if isRunning then
		tell application appName
			set sCount to count documents
			--display dialog (appName & sCount) as string
			
			-- Export each open document 
			repeat with iSC from 1 to sCount
				set documentName to name of document iSC
				(*
				display dialog ("App: " & appName & return ¬
					& "doc: " & sCount & "," & documentName & return ¬
					& "is modified?: " & (document iSC is modified)) as string
				*)
				-- path exists only if the document has ever been saved.
				-- only save if modified and has been saved before (excludes untitled and imported documents)
				if (path of document iSC exists) and (document iSC is modified) then
					save document iSC in (POSIX file (saveRootName & "-" & appName & "-doc" & iSC))
					log "Saved: " & documentName
				else
					log "Unsaved: " & documentName
				end if
			end repeat
		end tell
	end if
end doSaveToBackupDir

-- Call save function for all apps
doSaveToBackupDir("Keynote", saveRootName)
doSaveToBackupDir("Pages", saveRootName)
--doSaveToBackupDir("Numbers", saveRootName)
--no applescript dictionary for Numbers yet

Make a crontab that looks like this

# this runs it every 10 minutes
*/10 * * * * /usr/bin/osascript /Users/histed/Library/Scripts/auto-save-apps.app