Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Access recent items content via AppleScript Apps
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.
    •    
  • Currently 2.00 / 5
  You rated: 3 / 5 (4 votes cast)
 
[4,925 views]  

Access recent items content via AppleScript | 4 comments | Create New Account
Click here to return to the 'Access recent items content via AppleScript' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Massive potential for embarrassment here
Authored by: PeteVerdon on May 15, '04 09:27:25AM

It's a neat hack, but be careful if you're going to use a sig block that includes the names of files you've accessed recently. How about this as a sig:

Fred Foobar
Recent Items: kinky_goat_sex.jpg, fooCorp_takeover_plan.doc

Beware!



[ Reply to This | # ]
Massive potential for embarrassment here
Authored by: erickaterman on May 15, '04 12:20:04PM

What? You think that would embarrass me?

...Of course I've thought about that, and many others have also reminded me of that possibility, but luckily I don't use my computer for such things. (But, even if I did, I could probably add a filter to exclude any recent item strings including, oh, I don't know, things like "xxx" and "goatsex" and "teenhotslutbag".)

That said, though, I very much agree with you. I'm just hoping that silly hack doesn't get me into too much trouble.



[ Reply to This | # ]
Massive potential for embarrassment here
Authored by: PeteVerdon on May 15, '04 02:54:38PM

Erickater wrote:
> What? You think that would embarrass me?

:-) Even in email to your mother?

> ...Of course I've thought about that

In which case it's no problem. But I thought it might be a good idea to remind anyone else who might use it.

Pete



[ Reply to This | # ]
Massive potential for embarrassment here
Authored by: erickaterman on May 15, '04 07:28:06PM

> But I thought it might be a good idea to remind anyone else who might use it.

Of course. Good point. "Let the user beware..."



[ Reply to This | # ]