on writeTextToFile(theText, theFile, overwriteExistingContent)
try
-- Convert the file to a string
set theFile to theFile as string
-- Open the file for writing
set theOpenedFile to open for access file theFile with write permission
-- Clear the file if content should be overwritten
if overwriteExistingContent is true then set eof of theOpenedFile to 0
-- Write the new content to the file
write theText to theOpenedFile starting at eof
-- Close the file
close access theOpenedFile
-- Return a boolean indicating that writing was successful
return true
-- Handle a write error
on error
-- Close the file
try
close access file theFile
end try
-- Return a boolean indicating that writing failed
return false
end try
end writeTextToFile
var app = Application.currentApplication()
app.includeStandardAdditions = true
function writeTextToFile(text, file, overwriteExistingContent) {
try {
// Convert the file to a string
var fileString = file.toString()
// Open the file for writing
var openedFile = app.openForAccess(Path(fileString), { writePermission: true })
// Clear the file if content should be overwritten
if (overwriteExistingContent) {
app.setEof(openedFile, { to: 0 })
}
// Write the new content to the file
app.write(text, { to: openedFile, startingAt: app.getEof(openedFile) })
// Close the file
app.closeAccess(openedFile)
// Return a boolean indicating that writing was successful
return true
}
catch(error) {
try {
// Close the file
app.closeAccess(file)
}
catch(error) {
// Report the error is closing failed
console.log(`Couldn't close file: ${error}`)
}
// Return a boolean indicating that writing was successful
return false
}
}
set this_story to 'Once upon a time in Silicon Valley..'
set theFile to (((path to desktop folder) as string) & 'MY STORY.txt')
writeTextToFile(this_story, theFile, true)
var story = 'Once upon a time in Silicon Valley..'
var desktopString = app.pathTo('desktop').toString()
var file = `${desktopString}/MY STORY.txt`
writeTextToFile(story, file, true)
set theText to ((current date) as string) & space & 'STATUS OK' & return
set theFile to (((path to desktop folder) as string) & 'MY LOG FILE.log')
writeTextToFile(theText, theFile, false)
var dateString = Date().toString()
var desktopString = app.pathTo('desktop').toString()
var text = `${dateString} STATUS OKnn`
var file = `${desktopString}/MY LOG FILE.log`
writeTextToFile(text, file, false)
~/Library/Logs/
folder of the current user’s home directory.try
-- Your custom script code goes here
on error theErrorMessage number theErrorNumber
set theError to 'Error: ' & theErrorNumber & '. ' & theErrorMessage & return
set theLogFile to ((path to library folder from user domain) as string) & 'Logs:Script Error Log.log'
my writeTextToFile(theError, theLogFile, false)
end try
try {
// Your custom script code goes here
}
catch (error) {
var errorString = `Error: ${error.message}nn`
var logFile = app.pathTo('library folder', { from: 'user domain' }).toString() + '/Logs/Script Error Log.log'
writeTextToFile(errorString, logFile, false)
}
on readFile(theFile)
-- Convert the file to a string
set theFile to theFile as string
-- Read the file and return its contents
return read file theFile
end readFile
var app = Application.currentApplication()
app.includeStandardAdditions = true
function readFile(file) {
// Convert the file to a string
var fileString = file.toString()
// Read the file and return its contents
return app.read(Path(fileString))
}
set theFile to choose file of type 'txt' with prompt 'Please select a text file to read:'
readFile(theFile)
--> Result: 'Contents of the chosen file.'
var file = app.chooseFile({
ofType: 'txt',
withPrompt: 'Please select a text file to read:'
})
readFile(file)
// Result: 'Contents of the chosen file.'
on readAndSplitFile(theFile, theDelimiter)
-- Convert the file to a string
set theFile to theFile as string
-- Read the file using a specific delimiter and return the results
return read file theFile using delimiter {theDelimiter}
end readAndSplitFile
var app = Application.currentApplication()
app.includeStandardAdditions = true
function readAndSplitFile(file, delimiter) {
// Convert the file to a string
var fileString = file.toString()
// Read the file using a specific delimiter and return the results
return app.read(Path(fileString), { usingDelimiter: delimiter })
}
set theFile to choose file of type 'log' with prompt 'Please select a log file:'
readAndSplitFile(theFile, return)
--> Result: {'Log entry 1', 'Log entry 2', .. }
var file = app.chooseFile({
ofType: 'log',
withPrompt: 'Please select a log file:'
})
readAndSplitFile(file, 'n')
// Result: ['Log entry 1', 'Log entry 2', ..]