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

An AppleScript to open the Show Recent menu System
I wanted a quick way to show the 'Open Recent' menu of the current application. The following AppleScript was the solution.
set app_name to name of (info for (path to frontmost application))

tell application "System Events"
  tell process app_name
    -- Focus on the Apple menu upper left on your screen
    click menu bar item "Apple" of menu bar 1
  end tell
  
  set myRight to ASCII character 29 -- Right arrow
  set myDown to ASCII character 31 -- Down arrow
  
  -- Go to file menu
  repeat 2 times
    keystroke myRight
  end repeat
  
  -- Highlight "Open Recent" menu 
  keystroke "Open Recent"
  
  -- Show it
  keystroke myRight
end tell
It won't work from within the (buggy) Script Editor, but works for me very well from Terminal:
$ cd /path/to/script/directory
$ sleep 5; osascript ./RecentDoc.scpt
(Switch to the app you wish to test during the sleep 5 bit.) You can also call it from QuickSilver, Butler, etc.

[robg adds: Something I'm running on my machine, probably one of my input managers, is preventing this script from working on my machines. However, it's working for others based on my request for feedback on the forum site, so I thought I'd go ahead and post it. It will only work for those programs that have such a menu, and you'll need to make sure that Enable access for assistive devices is enabled in the Universal Access System Preferences panel.]
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[11,099 views]  

An AppleScript to open the Show Recent menu | 20 comments | Create New Account
Click here to return to the 'An AppleScript to open the Show Recent menu' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
An AppleScript to open the Show Recent menu
Authored by: dogboy on Jun 20, '06 08:36:32AM

If you have Quicksilver (yes, Quicksilver again :-)) then you can hit the right-arrow key when you have an application selected in the bezel and it'll list the recent items.

I tried to ad some pics, but it looks like I'm not allowed. Bah!



[ Reply to This | # ]
Thanks
Authored by: mike-c on Jun 20, '06 11:01:05AM

Holy cow, that is useful. Thanks again.



[ Reply to This | # ]
An AppleScript to open the Show Recent menu
Authored by: PancakeMan on Jun 22, '06 07:46:06AM

Forward-slash (in place of right-arrow) also works.



[ Reply to This | # ]
An AppleScript to open the Show Recent menu
Authored by: ScottTFrazer on Jun 20, '06 03:08:42PM

Something ate all the linebreaks for the code block in this post in the rss feed. Wow did that look ugly.



[ Reply to This | # ]
An AppleScript to open the Show Recent menu
Authored by: cougar718 on Jun 20, '06 07:26:46PM
How bout this instead? This code is more concise and does not require key codes. Most importantly it will check whether or not the "Open Recent" item exists before trying to 'show' it.
To adjust the delay, or remove it, simply do so :P

on run
	delay 5
	set strAppName to name of (info for (path to frontmost application))
	tell application "System Events"
		tell process strAppName
			tell menu bar 1
				if (exists menu item "Open Recent" of menu "File") then
					click menu "File"
					click menu item "Open Recent" of menu "File"
				end if
			end tell
		end tell
	end tell
end run

I've tested this code on my machine and there were no problems. But in the event there is a problem, let me know by either replying to this hint or emailing me at cougar@bat-country.net.

---
Rick alias cougar

[ Reply to This | # ]

An AppleScript to open the Show Recent menu
Authored by: Lutin on Jun 23, '06 04:12:40AM
My first try was something along those lines, following Apple examples.
But, that didn't work, and I couldn't figure out why.
Testing your suggestion, I understood: if you run it from Script Editor, it doesn't work.
It's why I came with the much uglier version, but I admit I prefer a lot yours.
I adapted your version so it works from within the finder as well.

on run
	set strAppName to name of (info for (path to frontmost application))
	tell application "System Events"
		tell process strAppName
			tell menu bar 1
				if (strAppName is equal to "Finder.app") then
					if (exists menu item "Recent Folders" of menu "Go") then
						click menu "Go"
						click menu item "Recent Folders" of menu "Go"
					end if			
				else					
					if (exists menu item "Open Recent" of menu "File") then
						click menu "File"
						click menu item "Open Recent" of menu "File"
					end if
				end if
			end tell
		end tell
	end tell
end run


[ Reply to This | # ]
An AppleScript to open the Show Recent menu
Authored by: cougar718 on Jun 23, '06 08:55:56AM

Awesome! Glad it works for you and I'm glad you edited it. Love to see that! It seems that there are some Input Manager plugins that break the communication to 'System Events' which in turn break your/our script.

Now I cannot believe Apple does not have a standard shortcut for the 'Open Recent' menu. I guess it's the Developer's and ultimately Apple for making it a Aqua Guide rule because in any nib, I'm sure you could use <modifier> + O to show the recent menu.

Real quick: Your idea makes the 'Open Recent' menu like a window which you can choose the document from which why others find it so convenient.

---
Rick alias cougar



[ Reply to This | # ]
An AppleScript to open the Show Recent menu
Authored by: TvE on Jun 24, '06 12:33:50AM

Yes - a feature that should be implemented in the OS.

I can recommend the way Zend Studio (a PHP IDE) implemented it.

Type CTRL-SHIFT-R and you get (a seperate window) with a list of the (10?) most recently opened files…

CMD-SHIFT-R is probably a better shortcut for a Mac user - Zend Studio is X-platform



[ Reply to This | # ]
An AppleScript to open the Show Recent menu
Authored by: Lutin on Jun 24, '06 02:08:16PM
Awesome! Glad it works for you and I'm glad you edited it. Love to see that!
I like it as well. That's why I would like an option to get an email every time a comment is posted.
I'm sure sharing and exchange like that would happen more often, and be of better quality.
Currently, after a few months, the comments of an hint get lost far in the site.
I know there is now RSS for the comments, but there is too much traffic. I just want to be able to watch a few threads.
robg?

[ Reply to This | # ]
An AppleScript to open the Show Recent menu
Authored by: ehouwink on Jun 26, '06 01:06:21PM

I tried to work around the problem that some apps do not have a "Recent Files" menu item but "Recent Documents" (like Ragtime)
The following script tries to find out what the "Recent" menu title is and addresses further action to that.

<code>
tell application "System Events"
set strAppName to name of some application process whose frontmost is true
tell process strAppName
tell menu bar 1
if (strAppName is equal to "Finder.app") then
if (exists menu item "Recent Folders" of menu "Go") then
click menu "Go"
click menu item "Recent Folders" of menu "Go"
end if
else
set recentmenu to (every menu item of menu "File" whose name contains "Recent")
if recentmenu = {} then return
set recentmenu to item 1 of recentmenu
tell me to set rr to name of menu items of menu 1 of recentmenu
beep
end if
end tell
end tell
end tell

tell application "System Events" to ¬
set fApp to some application process whose frontmost is true
tell fApp
activate
set theItem to choose from list rr
end tell
if theItem is false then return
set theItem to theItem as string

using terms from application "System Events"
tell application "System Events" to set frontmost of process strAppName to true
click menu item theItem of menu 1 of recentmenu
return theItem
end using terms from
</code>



[ Reply to This | # ]
An AppleScript to open the Show Recent menu
Authored by: ehouwink on Jun 26, '06 01:26:44PM

Pfff, in TextEdit, the lists created with the previous script appear to contain mini-icons that get translated as question marks, so I had to filter that out:

[code]
tell application "TextEdit" to activate

tell application "System Events"
set strAppName to name of some application process whose frontmost is true
tell process strAppName
tell menu bar 1
if (strAppName is "Finder") then
if (exists menu item "Recent Folders" of menu "Go") then
click menu "Go"
click menu item "Recent Folders" of menu "Go"
end if
else
set recentmenu to (every menu item of menu "File" whose name contains "Recent")
if recentmenu = {} then return
set recentmenu to item 1 of recentmenu
tell me to set rr to name of menu items of menu 1 of recentmenu
beep
end if
end tell
end tell
end tell

tell application "System Events" to ¬
set fApp to some application process whose frontmost is true
tell fApp
activate
set theItem to choose from list rr
end tell
if theItem is false then return
set theItem to theItem as string

-- filteren van "?" uit TextEdit's mini-icons in dialoog
if character 1 of theItem contains "?" then set theName to text from character 2 to -1 of theItem

set theItem to theName as string

using terms from application "System Events"
tell application "System Events" to set frontmost of process strAppName to true
set rr to every menu item of menu 1 of recentmenu whose name contains theItem
if rr ≠ {} then click item 1 of rr
return theItem
end using terms from
[/code]



[ Reply to This | # ]
Updated for 10.6
Authored by: troelssj on Nov 02, '10 02:17:27PM
I could not get any of the posted solutions to work in 10.6, but the following modified AppleScript Service works for me. Start Automator, choose Service and set it to receive "no input" in "any application". Paste the following into the script editor:

on run {input, parameters}
	tell application "System Events"
		set frontApp to name of first application process whose frontmost is true
		if (frontApp is equal to "Finder") then
			tell process frontApp
				key code 120 using control down
				key code 124
				key code 124
				key code 124
				key code 124
				key code 124
				key code 125
				tell menu bar 1
					click menu item "Recent Folders" of menu "Go"
				end tell
			end tell
		else
			tell process frontApp
				key code 120 using control down
				key code 124
				key code 124
				key code 125
				tell menu bar 1
					if (exists menu item "Open Recent" of menu "File") then
						click menu item "Open Recent" of menu "File"
					end if
				end tell
			end tell
		end if
	end tell
	return input
end run
It should be obvious how to modify the script to deal with programs where the menu item is not called "Open Recent", or where it is not located in the "File" menu.

Add a shortcut for the service and you are good to go!

[ Reply to This | # ]
Updated for 10.6
Authored by: tedw on Nov 02, '10 03:52:33PM
In 10.6, the following more congenial script seems to work:
tell application "System Events"
	set frontApp to name of first application process whose frontmost is true
	tell process frontApp
		set itemList to every menu item of every menu of first menu bar whose name contains "Recent"
		repeat with idx from 1 to count of itemList
			if item idx of itemList is not {} then
				tell first menu bar's menu idx
					click
					tell item 1 of item idx of itemList
						click
					end tell
				end tell
				return
			end if
		end repeat
	end tell
	say "Silly rabbit, Trix are for kids!"
end tell


[ Reply to This | # ]
Updated for 10.6
Authored by: troelssj on Nov 03, '10 02:47:08PM
Thanks for the reply, yours is a much more elegant solution. But I had to use

set itemList to every menu item of every menu of first menu bar whose name contains "Open Recent"
instead of

set itemList to every menu item of every menu of first menu bar whose name contains "Recent"
Otherwise the script just activates "Recent Items" in all programs. Unfortunately, this breaks the desired behavior in Finder (and also in a different application I use often, which has two different open recent items). But this should be easily fixed with an exception for these two programs.

[ Reply to This | # ]
Updated for 10.6
Authored by: tedw on Nov 03, '10 03:10:40PM

I'm confused - every app I've tried this on returns only one item (either 'open recent', 'recent items', or in the Finder's case 'recent folders'). what app has more than one of these?



[ Reply to This | # ]
Updated for 10.6
Authored by: troelssj on Nov 04, '10 01:27:10AM

When I execute the script as you wrote it, it just activates "Recent Items" (located under the menu with the  logo). Maybe this menu item has a different name on your machine? If you use a non-english localized this might be the explanation.

Regarding the program I mentioned which had multiple menu items containing "Recent", I was referring to a statistical program called Stata, which has both a "Open Recent" and a "Open Recent Do-files" menu item. The "Open Recent" is located higher up in the menu, so this one gets activated (but I prefer it to activate "Open Recent Do-files" instead). It is probably impossible to deal with this without adding an exception for this specific program.



[ Reply to This | # ]
Updated for 10.6
Authored by: tedw on Nov 04, '10 01:42:55AM
Ah, I get it. I have Recent Items disabled in my Apple menu, so I forgot it was there. That's easily fixed, however. just change line 5 to say:

repeat with idx from 2 to count of itemList

The '2' causes the script to skip over the Apple menu and start with the first application-specific menu. You could even use '3' if you wanted (a recent items list is not likely to occur before the File menu).

[ Reply to This | # ]

Updated for 10.6
Authored by: troelssj on Nov 04, '10 02:49:58AM
Thanks, it works great now!

In the script I use, I will probably also add an exception to TextMate, since for some strange reason it takes the script about 5 seconds to locate the open recent item in the menu bar (I had this issue with my original script as well). So for users of TextMate, I would suggest adding the following

if (frontApp is equal to "TextMate") then
	tell process frontApp
		key code 120 using control down
		key code 124
		key code 124
		key code 125
		key code 125
		key code 125
		key code 125
		key code 125
		key code 124
	end tell
end if
It ins't pretty, but beats waiting for 5 seconds.

[ Reply to This | # ]
Updated for 10.6
Authored by: tedw on Nov 04, '10 08:21:56AM

That's interesting: I wonder what causes that delay? you might try adding the line <tt>set quit delay to 0</tt> somewhere in the System Events tell block - that tells System Events to stay open all the time so that you don't get startup delays on the next invocation.



[ Reply to This | # ]
Updated for 10.6
Authored by: troelssj on Nov 04, '10 03:08:23PM

Adding "set quit delay to 0" didn't change the behavior.

I have noticed that after a few seconds, a rotating gear appears in the right side of the menu bar - where the wi-fi, bluetooth and speaker icons are located. I am guessing it indicates that the script is waiting for something to complete. Now that I think of it, TextMate has a rather complicated menu structure - one menu item has more than 40 subitems, all of which have many subsubitems. To make things worse, these items are related to various bundles, which can be changed by the user, so TextMate might have to rebuild the menu bar when the script asks for items containing the word "recent".



[ Reply to This | # ]