Last active
September 18, 2024 19:12
-
-
Save crkrenn/aa7aceb34adbe1fb9328923af7b9c5d6 to your computer and use it in GitHub Desktop.
MacOS apple script to automatically save open Microsoft Office files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This script saves all open and modified Excel, Word, and PowerPoint documents | |
// It can be run manually with "osascript -l JavaScript /path/to/your/saved/script.js" | |
// or scheduled to run periodically using a tool like cron. | |
// type `crontab -e` to edit the cron jobs | |
// crontab: */5 * * * * /usr/bin/osascript -l JavaScript /path/to/your/saved/script.js | |
function run() { | |
try { | |
console.log("Script started"); | |
// Function to save Excel workbooks | |
if (isApplicationRunning("Microsoft Excel")) { | |
var excelApp = Application("Microsoft Excel"); | |
console.log("Microsoft Excel is running"); | |
if (excelApp.workbooks.length > 0) { | |
console.log("Workbooks are open in Excel"); | |
excelApp.workbooks().forEach(function (workbook) { | |
try { | |
workbook.save(); | |
console.log("Saved workbook: " + workbook.name()); | |
} catch (error) { | |
console.log("Error saving workbook: " + workbook.name() + ". Error: " + error.message); | |
} | |
}); | |
} else { | |
console.log("No open workbooks found in Excel"); | |
} | |
} else { | |
console.log("Microsoft Excel is not running."); | |
} | |
// Function to save Word documents | |
if (isApplicationRunning("Microsoft Word")) { | |
var wordApp = Application("Microsoft Word"); | |
console.log("Microsoft Word is running"); | |
if (wordApp.documents.length > 0) { | |
console.log("Documents are open in Word"); | |
wordApp.documents().forEach(function (document) { | |
try { | |
document.save(); | |
console.log("Saved document: " + document.name()); | |
} catch (error) { | |
console.log("Error saving document: " + document.name() + ". Error: " + error.message); | |
} | |
}); | |
} else { | |
console.log("No open documents found in Word"); | |
} | |
} else { | |
console.log("Microsoft Word is not running."); | |
} | |
// Function to save PowerPoint presentations | |
if (isApplicationRunning("Microsoft PowerPoint")) { | |
var powerpointApp = Application("Microsoft PowerPoint"); | |
console.log("Microsoft PowerPoint is running"); | |
if (powerpointApp.presentations.length > 0) { | |
console.log("Presentations are open in PowerPoint"); | |
powerpointApp.presentations().forEach(function (presentation) { | |
try { | |
presentation.save(); | |
console.log("Saved presentation: " + presentation.name()); | |
} catch (error) { | |
console.log("Error saving presentation: " + presentation.name() + ". Error: " + error.message); | |
} | |
}); | |
} else { | |
console.log("No open presentations found in PowerPoint"); | |
} | |
} else { | |
console.log("Microsoft PowerPoint is not running."); | |
} | |
console.log("Script ended"); | |
} catch (error) { | |
console.log("An error occurred: " + error.message); | |
} | |
} | |
// Helper function to check if an application is running | |
function isApplicationRunning(appName) { | |
var app = Application('System Events').applicationProcesses.byName(appName); | |
return app.exists(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment