Applescript write to log file

I'd like to create a applescript that during the process can write to a log file so I can ensure it's functioning correctly on a users machine. I'm a unix guy so I know how to do it in there, but can't seem to find a good example in Applescript. Ideally, I'd like to have it write to a log file in /var/log.

Posted on Mar 23, 2013 8:51 AM

Reply
Question marked as Top-ranking reply

Posted on Mar 23, 2013 9:16 AM

Writing to a file in AppleScript is a three-part process.


First, open the log file:


set myLogFile to open for accessPOSIX file "/var/tmp/myScript.log" with write permission


Now you can write some data to it:


write "some data" & return to myLogFile


When you're done, close the file:


close accessmyLogFile


You can have multiple write statements - they'll just append to the existing data.

9 replies
Question marked as Top-ranking reply

Mar 23, 2013 9:16 AM in response to Roie Gat

Writing to a file in AppleScript is a three-part process.


First, open the log file:


set myLogFile to open for accessPOSIX file "/var/tmp/myScript.log" with write permission


Now you can write some data to it:


write "some data" & return to myLogFile


When you're done, close the file:


close accessmyLogFile


You can have multiple write statements - they'll just append to the existing data.

Mar 23, 2013 12:35 PM in response to Roie Gat

I believe so.




--------------------------------------------------------------


You can invoke unix commands from AppleScript.



(*


It is easier to diagnose problems with debug information. I suggest adding log statements to your script to see what is going on. Here is an example.



Author: rccharles

For testing, run in the Script Editor.

1) Click on the Event Log tab to see the output from the log statement

2) Click on Run

For running shell commands see:

http://developer.apple.com/mac/library/technotes/tn2002/tn2065.html



*)



on run

-- Write a message into the event log.

log " --- Starting on " & ((current date) as string) & " --- "

-- debug lines

set desktopPath to (path to desktop) as string

log "desktopPath = " & desktopPath

set unixDesktopPath to POSIX path of desktopPath

log "unixDesktopPath = " & unixDesktopPath

set quotedUnixDesktopPath to quoted form of unixDesktopPath

log "quoted form is " & quotedUnixDesktopPath

try

set fromUnix to do shell script "ls -l " & quotedUnixDesktopPath

on error errMsg

log "ls -l error..." & errMsg

end try

display dialog "ls -l of " & quotedUnixDesktopPath & return & fromUnix

end run


Nov 23, 2013 10:43 AM in response to Camelot

Hi Camelot,


If you don't mind me posting a follow-up question, how would I nest the code below in your code, so as to have the results of my script written to a log file?


Any suggestions you may have will be warmly welcomed!


Cheers,


Simon


property searchReplace : {{"SearchValue_1", "ReplaceValue_1"}, {"SearchValue_2", "ReplaceValue_2"}, {"SearchValue_3", "ReplaceValue_3"}}


tell application "Finder"

set thisFolder to "Macintosh HD:Users:Quokka_61:Desktop:Excel-test"

open thisFolder

set theseFiles to every file of folder thisFolder

end tell


repeat with i from 1 to the count of theseFiles

set thisFile to (item i of theseFiles)

tell application "Microsoft Excel"

open thisFile

set thisFile to active workbook

set targetRange to used range of active sheet of active workbook

tell used range of worksheet "Sheet1" of active workbook

repeat with thisSearchReplace in searchReplace

set searchVar to item 1 of thisSearchReplace

set replaceVar to item 2 of thisSearchReplace

replace targetRange what searchVar replacement replaceVar

end repeat

end tell

end tell

close thisFile saving yes

end repeat

Nov 24, 2013 11:09 PM in response to Quokka_61

how would I nest the code below in your code, so as to have the results of my script written to a log file?

I have no idea, since there's no specific 'result' of your script


Your script appears to open a series of files in Excel (or, at least, attempts to since there's no check that they are valid Excel-readable files, but that's another issue). Once open it performs a series of search/replace actions and saves the file.


What, exactly, do you consider the 'result' of this script, and therefore what do you expect to get logged?

Nov 27, 2013 1:35 AM in response to Camelot

Hi Camelot,


Thank you for your reply. I could indeed have been more specific in the formulation of my question, but what I was referring to as ‘results’ actually is the output shown in the bottom pane of the Applescript Editor window when code is run in its upper pane. It is this output that I need to write to a log file as an audit trail. Although I could of course do this by copying and pasting the output from the pane to a log file, this no longer works when an applet has been created from the code, since then I do no longer have access to the output.


As to your remark regarding the issue whether the files in the directory I am accessing in my script are valid Excel-readable files, they indeed all are. I agree that it would be more elegant to have a check built in, but being the newbie to Applescript I am, I wouldn’t know how to do this, and like I said, care is taken that no files occur in the target directory that shouldn’t be accessed and processed.


Cheers,


Simon

Nov 27, 2013 11:10 AM in response to Quokka_61

"If a handler does not include a return statement, AppleScript returns the value returned by the last statement ..." pate 214 applescript reference.


Your getting whatever is returned from

close thisFile saving yes


You can get this value by"

set theResult to close thisFile saving yes


This may not be the best value to return. You might establish your own return code and set it as appropriate.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Applescript write to log file

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.