Skip to content

Instantly share code, notes, and snippets.

@vitorio
Last active August 23, 2025 09:21
Show Gist options
  • Select an option

  • Save vitorio/e7f6ef283bf59bb94e2df2df3319ba89 to your computer and use it in GitHub Desktop.

Select an option

Save vitorio/e7f6ef283bf59bb94e2df2df3319ba89 to your computer and use it in GitHub Desktop.
Automatically save every untitled (Document###) unsaved open Word doc
if application id "com.microsoft.Word" is running then
tell application id "com.microsoft.Word"
set docCount to count of documents
if docCount ≥ 1 then
repeat with i from 1 to docCount
set doc to document i
set docName to name of doc
if docName starts with "Document" then
if docName ends with " AutoSave).docx" then
if saved of doc is false then
save doc
end if
else if docName ends with ".docx" then
-- do nothing, it's just a confusing document name
else if docName does not end with ".docx" then
set currentTimestamp to short date string of (current date) & " " & time string of (current date)
set currentTimestamp to my replaceChars(currentTimestamp, "/", "-")
set currentTimestamp to my replaceChars(currentTimestamp, ":", "-")
set savePath to "/Users/username/Documents/Word autosaves/" & docName & " (" & currentTimestamp & " AutoSave).docx"
save doc in savePath
end if
end if
end repeat
else
-- do nothing, no documents are open
end if
end tell
end if
-- https://apple.stackexchange.com/a/425584
on replaceChars(thisText, searchString, replacementString)
set AppleScript's text item delimiters to the searchString
set the itemList to text items of thisText
set AppleScript's text item delimiters to the replacementString
set thisText to the itemList as string
set AppleScript's text item delimiters to ""
return thisText
end replaceChars
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment