Writing output to a text file

Hey guys, could use a little help here. Basically, what im trying to do is have a script that sends me an email whenever a file is added to the LaunchAgents or Launch Daemons folders on our workstations. I had this working all through applescript but it was using the mail.app to send the email and i can’t have that because i want it to be invisible to the user.

So what im trying to do, is have the applescript output the information to a text file rather than email, then i will write a shell script to send me an email so this whole process will be invisible to the user so it will not interrupt their work. I cannot get the output to write to the file. Hopefully you can help, here is what i have:


on adding folder items to this_folder after receiving added_items
	set added_Items_List to {}
	repeat with oneItem in added_items
		set end of added_Items_List to name of (info for oneItem)
	end repeat
	set {TID, text item delimiters} to {text item delimiters, ", "}
	set added_Items_List to added_Items_List as text
	set text item delimiters to TID
	set dateString to (current date) as string
	set theBody to "These items have been added to folder " & name of (info for this_folder) & " on " & computer name of (system info) & ":" & return & return & added_Items_List as string
	
	set outputFile to ((("/Users/david/Desktop/") as string) & "LaunchAgent_Alert.txt") as file specification
	try
		open for access outputFile with write permission
		write theBody to outputFile as string
		close access outputFile
	end try
	
end adding folder items to

Let me know what you guys think. If you know of a way for this to send an email without using the mail.app too, that would be great, just can’t have mail opening when they are in the middle of their work.

thanks!!

Hi,

your write-to-file code has a few problems.
Try this


.
set outputFile to ((path to desktop as text) & "LaunchAgent_Alert.txt") -- represents "MacHD:Users:david:Desktop:LaunchAgent_Alert.txt"
try
	set fileReference to open for access file outputFile with write permission
	write theBody to fileReference
	close access fileReference
on error
	try
		close access file outputFile
	end try
end try

awesome thank you so much! that worked! One last question if you have time. Since im not going to be actually putting this file on their desktop. Prob in MacintoshHD/Scripts/ folder. But when i am putting the path into in the script instead of Desktop, its not working. Any idea why?

set outputFile to ((path to "Macintosh HD:Scripts" as text) & "LaunchAgent_Alert.txt") 

tried with both having no : after Scripts and with a :.

also tried

set outputFile to ((path to "Volumes:Macintosh HD:Scripts:" as text) & "LaunchAgent_Alert.txt")

thanks again!

It’s not recommended to put user data on the top level of the hard disk.

path to scripts folder as text

represents the (HFS) path to the Scripts folder in the Library of the current user

cool. thanks for all your help!