Access recent items content via AppleScript

May 13, '04 10:32:28AM

Contributed by: erickaterman

After reading about dynamic signatures on this site, I thought it might be neat to add other random info about my computer to my signature, and so I looked into accessing the Recent Items menu in the Apple menu. The Recent Items actually reside in a .plist file, so I wrote an applescript to access that file and parse the results, which are returned to a script which gathers info (including what's playing in iTunes, output from fortune, etc.) for my signature.

Read the rest of the hint for the code that processes the com.apple.recentitems.plist file:

on run
 
 set olddelims to my text item delimiters
 set NUM_APPS to 5
 set NUM_DOCS to 3
 
 --first process recent applications...
 set recentapps_string to process_recentitems("app", NUM_APPS)
 
 -- now process recent docs...
 set recentdocs_string to process_recentitems("doc", NUM_DOCS)
 
 set my text item delimiters to olddelims
 
 return "recent apps: " & recentapps_string & return ¬
  & "recent docs: " & recentdocs_string
end run

on process_recentitems(itemtype, num_items)
 
 --first process recent items...
 set recentitems to (do shell script ¬
  "defaults read com.apple.recentitems " & ¬
  itemtype & "s | grep -v '(' | grep -v ')'")
 set my text item delimiters to ","
 set recentitems to (text items 1 through num_items of recentitems)
 
 -- remove any double-quotes
 set my text item delimiters to "\""
 set recentitems_noquotes to {}
 repeat with the_item in recentitems
  set recentitems_noquotes to recentitems_noquotes & ¬
   (text item 2 of the_item)
 end repeat
 
 -- now get just the itemname
 set my text item delimiters to "/"
 set recentitems_names to {}
 repeat with the_item in recentitems_noquotes
  set recentitems_names to recentitems_names & ¬
   (text item -1 of the_item)
 end repeat
 
 if (itemtype is "app") then
  -- now strip off the '.app' extension, if it exists
  
  set my text item delimiters to ".app"
  set recentitems_shortnames to {}
  repeat with the_item in recentitems_names
   set recentitems_shortnames to ¬
    recentitems_shortnames & (text item 1 of the_item)
  end repeat
  
  -- now make a nice formatted recent items string...
  
  set my text item delimiters to "; "
  set recentitems_string to (recentitems_shortnames as string)
  
 else
  -- just make your nice string
  set my text item delimiters to "; "
  set recentitems_string to (recentitems_names as string)
  
 end if
 
 return recentitems_string
end process_recentitems
Anyway, I thought some people might find this useful.

Comments (4)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20040509230240272