Create log file when script runs

Hello!

I have a small script for example like the following:

tell application “iTunes”
activate
end tell
tell application “System Events” to tell process “iTunes”
click menu item “Play Space bar” of menu 1 of menu bar item “Controls” of menu bar 1
end tell

I need a log file to be created everytime this script runs.

Do I have to do it inside AppleScript Editor?

Best regards

Kostas

Hello.

Use the handler below. The log file will show up in the left side-bar of Console.app. By the example, the log will get the name MyAppEvent.log and reside in /Users/You/Library/Logs.

to logit(log_string, log_file)
	do shell script ¬
		"echo `date '+%Y-%m-%d %T: '`\"" & log_string & ¬
		"\" >> $HOME/Library/Logs/" & log_file & ".log"
end logit

# Example Call

my logit("The event occured","MyAppEvent")

Thank you very much! I guess this is fine for the moment.

Best regards and thanks again

Kostas

Just a detail :
The name of the triggered menu item is localizable so your script would fail if used on a non-English system.
It’s the same for the name of the menu.

It would be fine to edit the code as :


tell application "iTunes"
	activate
end tell
tell application "System Events" to tell process "iTunes"
	click menu item 1 of menu 1 of menu bar item 6 of menu bar 1
end tell

KOENIG Yvan (VALLAURIS, France) lundi 22 juillet 2013 19:33:48

or just


tell application "iTunes" to play

actually


tell application "iTunes"
	if player state is playing then
		stop
	else
		play
	end if
end tell

;):wink:

Well seen Stefan.

KOENIG Yvan (VALLAURIS, France) lundi 22 juillet 2013 19:45:05

Thank you all,

iTunes is used as example for this thread. The real app is a Nikon provided app, in which I do not have access now (I will have in the client’s site).

I had to test the app’s ability to start up and continue a time lapse, if the client machine restarts after a long power outage.

I had to bring the app window to frontmost for the script to work.

I need now to include e-mail notification (maybe via osascript and sendmail or regular Mail client).

Thank you all for your help.

Here is my final working script:

--Define log
to logit(log_string, log_file)
	do shell script ¬
		"echo `date '+%Y-%m-%d %T: '`\"" & log_string & ¬
		"\" >> $HOME/Library/Logs/" & log_file & ".log"
end logit
--Start application and wait until camera is recognized
activate application "Camera Control Pro"
delay 10
tell application "System Events"
	tell process "Camera Control Pro 2"
		set frontmost to true
		click menu item "Interval Timer Shooting..." of menu 1 of menu bar item "Camera" of menu bar 1
		--Start Time Lapse shooting		
		click button "Start" of window "Interval Timer Shooting"
	end tell
end tell
--Write to log file
my logit("The script ran!", "Camera_started")

Best regards

Kostas