Tip: View a property record result in Safari

Am I the only person who’s sick of slogging through the lengthy results of a “get properties of” request? After fiddling around with results, I decided that the script editor can’t be made to display this information in a sane and elegant fashion, so I wrote a script that exports said result to HTML and opens it in Safari:

tell application "System Events" to tell process "Script Editor"
    set default_window to name of window 2 as string -- (optional) the stack order of the window you typically use
    set script_windows to name of every window as list
    set source_window to (choose from list script_windows default items default_window) as string
    set the_result to value of text area 1 of scroll area 1 of group 1 of splitter group 1 of window source_window
end tell
set text item delimiters to ","
set the_result to the_result's text items as list
set text item delimiters to "</td></tr><tr><td align=\"right\"><b>"
set the_result to the_result as string
set text item delimiters to ":"
set the_result to the_result's text items as list
set text item delimiters to ":</b></td><td align=\"left\">"
set the_result to the_result as string
set text item delimiters to ""
set the_result to "<table><tr><td align=\"right\">" & the_result & "</td></tr></table>"
set html_result to (path to desktop as string) & "html_result.html"
open for access file html_result with write permission
write the_result to file html_result
close access file html_result
set html_result to html_result as alias
tell application "System Events" to set result_url to URL of html_result
tell application "Safari"
    activate
    make new document at end of documents
    set URL of document 1 to result_url
end tell
tell application "Finder" to move html_result to trash

Important considerations:

  • the script assumes that the results are already sitting around waiting in the result window of an open Script Editor script.
  • the script will somewhat uglify mac-style colon-delimited paths by splitting them up into adjacent table cells.

I think this script could be written more efficiently, but the more obvious approaches I tried weren’t getting me the end result I wanted (for example coercing the properties record to a string through its error message turned all the labels into code guillemets)

Anywho, to test the script I open a Script Editor window and run this script…

tell application "System Events" to get properties of process "Script Editor"

…which gives this ugly result…

{class:application process, entire contents:{}, focused:missing value, displayed name:"Script Editor.app", orientation:missing value, partition space used:0, maximum:missing value, file:alias "hd1:Applications:AppleScript:Script Editor.app:", position:missing value, subrole:missing value, selected:missing value, help:missing value, size:missing value, visible:true, role:"AXApplication", application file:alias "hd1:Applications:AppleScript:Script Editor.app:", background only:false, accepts high level events:true, Classic:false, file type:"APPL", description:"application", has scripting terminology:true, name:"Script Editor", accepts remote events:false, unix id:492, creator type:"ToyS", id:9961473, value:missing value, enabled:missing value, title:"Script Editor", minimum value:missing value, total partition size:0, frontmost:true}

…that my script then formats into this slightly less ugly result:

http://homepage.mac.com/mike.com/property_result.gif

This works really nicely if you run it on a few open scripts, and then it allows you to visually compare and contrast the properties of two different objects. (Speaking of which, I just had a thought, wouldn’t it be nice to have a script that could compare the differences between the properties of two different objects?) and finally if you want to keep the HTML file that was generated by the script, you can go find it in the trash.

A-ha! This is why I like posting to these forums! When I got the comparison idea I went in and started messing around with the script some more and discovered a way to get the error message I originally wanted to parse! Now this takes out the whole weirdness of using a GUI script, instead you can just load properties directly from a variable:

-- get some properties here:
tell application "System Events" to set the_result to properties of process "Script Editor"

-- format the result into html:
try
	get item 1 of the_result
on error error_message
end try

set text item delimiters to "Can't get item 1 of {"
set the_result to item 2 of text items of error_message
set text item delimiters to "}."
set the_result to item 1 of text items of the_result
set text item delimiters to ","
set the_result to the_result's text items as list
set text item delimiters to "</td></tr><tr><td align=\"right\"><b>"
set the_result to the_result as string
set text item delimiters to ":"
set the_result to the_result's text items as list
set text item delimiters to ":</b></td><td align=\"left\">"
set the_result to the_result as string
set text item delimiters to ""
set the_result to "<table><tr><td align=\"right\"><b>" & the_result & "</td></tr></table>"
set html_result to (path to desktop as string) & "html_result.html"
open for access file html_result with write permission
write the_result to file html_result
close access file html_result
set html_result to html_result as alias

tell application "System Events" to set result_url to URL of html_result

tell application "Safari"
	activate
	make new document at end of documents
	set URL of document 1 to result_url
end tell

tell application "Finder" to move html_result to trash