How to script console application on MAC?

Hi,
I want to automatically hide log list on console app instead of manually clicking on the icon in the tool bar of console.
Is there a way to do it automatically? User can then show log list manually if he wants, but I want it to hide the log list by default.

Thanks for your help in advance.

Hello.

This works in Snow Leopard. Don’t try it on anything but a copy of your app that you should rename in Lion, and Mountain Lion: find the propertylist file info.plist and make sure there is a property there which turns on scriptability.

For your special needs, you don’t really need to script Console.app.

Just command click the tool bar of the console wndow. Click customize, and choose the one button with hide loglist.
Add it, from then onwards, you can just click that button to display or hide it.

It doesn’t get much simpler than that You will have to either click something, or click something that makes a new window, and turn off the loglist by uiscripting the menu-item that displays/hides the loglist,If that doesn’t help you, then you’ll have to look into the dictionary of Console.app and see if there is something there.

I have mashed together a script that turns the loglist on or off for you for the active window, for the case that you should want this in some workflow, which may be use ful.

It works with Snow Leopard and only uses UI scripting, so no hijacking is necessary. :slight_smile:


tell application "System Events"
	set UI elements enabled to true
end tell
try
	tell application "Console"
		activate
	end tell
	tell application "System Events"
		tell process "Console"
			tell menu bar 1
				tell menu bar item 5
					tell menu 1
						tell menu item 8
							--	tell menu menu_item
							click --  menu item 1
							--	end tell
						end tell
					end tell
				end tell
			end tell
		end tell
	end tell
	return true
on error error_message
	return error_message
end try
-- end do_submenu

Look for the application’s preferences file and look for LogOutlineViewVisible. To set this behaviour off you have to change the preferences of the application. Make sure console is not running, then run the code below (change the path to the log file you wish to open.

do shell script "defaults write com.apple.console LogOutlineViewVisible 0"
do shell script "open -a console /var/log/system.log"

Hello.

Many a time, I have wanted to filter the front log with some search string. A wrote a handler to do just that.
This will work in the understanding that I open the log by either open or finder, so the wanted log “pops up” in Console. Then I use the handler below to filter it. The other handler is the one from above: toggleConsoleFileList

Edit

DJ Bazzie Wazzie had a much better way of doing this, which I have implemented.
The handler returns 0 if no windows are open, −1 if the toolbar is collapsed, and 1 if it succeded.


to filterFrontLog by searchString
	set n to 0
	tell application "System Events"
		set n to count windows of application process "Console"
		if (n < 1) then return n
		set UI elements enabled to true
	end tell
	set n to -1
	tell application "System Events"
		tell application process "Console"
			try
				repeat with thisGroup in groups of tool bar 1 of front window
					if "filter" is in name of every text field in thisGroup then
						set value of text field "filter" of thisGroup to searchString
						perform action "AXConfirm" of text field "filter" of thisGroup
						set n to 1
						exit repeat
					end if
				end repeat
			on error
				return -1
			end try
		end tell
	end tell
	return n
end filterFrontLog

to clickConsoleMenu for aNumber
	tell application "System Events"
		set UI elements enabled to true
	end tell
	try
		tell application "Console"
			activate
		end tell
		tell application "System Events"
			tell process "Console"
				tell menu bar 1
					tell menu bar item 5
						tell menu 1
							tell menu item aNumber
								--	tell menu menu_item
								click --  menu item 1
								--	end tell
							end tell
						end tell
					end tell
				end tell
			end tell
		end tell
		return true
	on error error_message
		return error_message
	end try
end clickConsoleMenu

to toggleConsoleWinFileList()
	clickConsoleMenu for 8
end toggleConsoleWinFileList

to toggleConsoleWinToolbar()
	clickConsoleMenu for 9
	-- end do_submenu
end toggleConsoleWinToolbar

to allMessagesWindow()
	tell application "Console"
		activate
		tell application "System Events"
			set UI elements enabled to true
			tell application process "Console"
				keystroke "n" using command down
			end tell
		end tell
	end tell
	tell application "System Events"
		tell application process "Console"
			set selected of row 2 of outline 1 of scroll area 1 of splitter group 1 of front window to true
			# allways opens a new window with file-list
			my toggleConsoleWinFileList()
			my toggleConsoleWinToolbar()
		end tell
	end tell
end allMessagesWindow

to assertFilterFrontLog by aSearchString
	set n to 0
	repeat while n ≥ 0
		try
			set n to filterFrontLog by aSearchString
		on error
			set n to -1
		end try
		if n = 0 then tell application "Console" to open "/var/log/system.log"
		# no window was open
		if n ≤ 0 then
			if n < 0 then toggleConsoleWinToolbar()
			# trouble was that the toolbar was collapsed.
			delay 0.5
			set n to filterFrontLog by aSearchString
			# se we'll do it right this time
			exit repeat
		else
			exit repeat
		end if
	end repeat
end assertFilterFrontLog

assertFilterFrontLog by "dylib"


By default the ‘expression filter’ is not shown and because tool bars are dynamic I won’t use index number but call them by identifier. Also changing the value isn’t enough to trigger the ‘textDidChange’ notification because the focus isn’t set to it. Therefore I need to perform the confirm action afterwards to update the window.


tell application "System Events"
	tell application process "Console"
		repeat with thisGroup in groups of tool bar 1 of front window
			if "filter" is in name of every text field in thisGroup then
				set value of text field "filter" of thisGroup to "Mail"
				perform action "AXConfirm" of text field "filter" of thisGroup
				exit repeat
			end if
		end repeat
	end tell
end tell

Hello. :slight_smile:

I stand corrected, It went a little bit fast about this, your solution is much better!
I snagged it and implemented it in the handler above. Lets hope this field is always called filter!

Edit

I harnessed it a little further, as I at least got some run time errors from within Script Debugger, everything is now collected in the post above, the two high-level handlers for toggling the file list in the Console window, and one for searching. The two others are just sub routines, for the sake of factoring stuff out while I was at it.

Hello.

I added an “allMessagesWindow()” handler among the ones above as I felt that one to be missed the most, for my part at least, since I alwyas redirect output to a log, and seldom to stderr" (Console has good choices for opening different logs though, on its menu.

It opens a new window, selects all messages, and closes the sidebar, and the toolbar in a very few. :slight_smile:

Hi Thanks. I modified your script from tell menu item 8 to tell menu item 16 for my mountain lion machine and then it worked as desired.
Though, I also figured DJ’s method and that works well too. I was preferably looking for a non-UI scripting method, so that if a user interacts while the script is running then it can break the process.

Also, I found another way of translating your UI scripting method through AppleScript Editor’s Replies field as below. I think, it may work on any OS even though the menu item number may change from OS to OS as long as the menu name doesn’t change. :slight_smile:

try
	tell application "Console"
		activate
	end tell
	tell application "System Events"
		tell process "Console"
			click menu item "Hide Log List" of menu "View" of menu bar item "View" of menu bar 1 of application process "Console" of application "System Events"
		end tell
	end tell
end try

Hello.

It is hard to set focus to something when you are dealing with menus, but if you use ui scripting and set the focus to the front window, right before you start clicking the menu, then the user will have a hard time managing to intersperse before the click is done. I don’t know if using the mouse at the same time will interrupt anything. There are no loops here, delaying for a sheet or anything, so this time, I believe the UI scripting to be of the more robust kind.

Console.app is localized, which means that the menus appear with the local language, so your approach will work for english only.

I COULD have had used some more explanatory constants, instead of just menu number 8 and 9. :slight_smile: I’ll do so for the future, and I am glad you pointed it out for me.

I am glad that it works for you.

Hello.

This snippet lets you prune obsolete log files, I have made a list of logfiles accessed earlier than the 31 last days for you to choose from. The idea is to clean up the sidebar of the Console.app a little

Enjoy

set theFiles to paragraphs of (do shell script "cd ~/Library/Logs; find . -name \\*.log -atime +31d -depth 1 |sed  's_^\\./__g'")
set filesToPrune to choose from list theFiles default items item 1 of theFiles with prompt "Choose logfiles to prune" with title "Prune Console Log files" with multiple selections allowed
if filesToPrune is false or filesToPrune is {} then
	error number -128
end if
repeat with afilename in filesToPrune
	set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, " "}
	set tmpfn to text items of afilename
	set AppleScript's text item delimiters to "\\ "
	set contents of afilename to (tmpfn as text)
end repeat
set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, " "}
set filesToRemove to filesToPrune as text
set AppleScript's text item delimiters to tids
do shell script "cd ~/Library/Logs; for i in " & quoted form of filesToRemove & "; do rm $i ; done "