Opening OSX menu bar "recent elements" by applescript

Hello

I’m trying to open the recent elements item from the apple menu bar (top left) by applescript, to bind this script to a key.

So far, nothing I tried worked. I don’t even know how to open anything from this OSX menu bar.

My current script :

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

Any help is welcome

Guylhem

Hi Guylhem,

I’m not much of a fan of GUI scripting, but this seems to get at the sub menu. I think you need to open the menu before referring to the submenu item.

tell application "System Events"
	tell process (name of item 1 of (processes whose frontmost is true))
		try
			tell menu bar item "File" of menu bar 1
				click
				tell menu item "Open Recent" of menu "File" to click
			end tell
		end try
	end tell
end tell

Best wishes

John M

Hello

On my OSX 10.6, with a french locale, it doesnt work (generally english labels work - I tried to copy the french labels but it doesn’t help

To debug that, I tried:


tell application "System Events"
	tell process (name of item 1 of (processes whose frontmost is true))
		try
			get menu items
		end try
	end tell
end tell

But it returned an empty list {}

Do you get a non empty list with this code? This may mean a difference between your version and my 10.6

I don’t like GUI scripting either, but I couldn’t find any other way.

I get an empty list with your code.

What do you get with this?

tell application "System Events"
	tell process (name of item 1 of (processes whose frontmost is true))
		try
			get name of menu bar items of menu bar 1
		end try
	end tell
end tell

John M

Hello

I get
{“Apple”, “Éditeur AppleScript”, “Fichier”, “Édition”, “Présentation”, “Script”, “Police”, “Format”, “Fenêtre”, “Aide”}

Apple seems to be what I want (OSX menu bar).

After this fix, the following script works:


tell application "System Events"
	tell process (name of item 1 of (processes whose frontmost is true))
		try
			tell menu bar item "Apple" of menu bar 1
				click
				tell menu item "Éléments récents" of menu "Apple" to click
			end tell
		end try
	end tell
end tell

Thanks a lot !!

Guylhem

Ah, I didn’t appreciate that you wanted that menu.

To remove the localization issue, how about:

tell application "System Events"
	tell process (name of item 1 of (processes whose frontmost is true))
		try
			tell menu bar item 1 of menu bar 1
				click
				tell menu item 9 of menu 1 to click
			end tell
		end try
	end tell
end tell

Best wishes

John M

You may use this one :


set recentItems to my inspect_SubMenu(1, 9)

set whichItem to choose from list recentItems
if whichItem is false then error number -128

my selectSubMenu(1, 9, item 1 of whichItem)

--=====

on selectSubMenu(mt, mi, ms)
	tell application "System Events"
		set theApp to (name of first process whose frontmost is true)
		tell process theApp to tell menu bar 1 to tell menu bar item mt to tell menu 1 to tell menu item mi to tell menu 1 to click menu item ms
	end tell -- 
end selectSubMenu

--=====

on inspect_SubMenu(mt, mi)
	--	tell application "Script Editor" to activate
	tell application "System Events" to tell process (name of first process whose frontmost is true) to tell menu bar 1
		get name of menu bar items
		(*
{"Apple", "Éditeur de scripts", "Fichier", "Édition", "Présentation", "Script", "Police", "Format", "Fenêtre", "Aide"}
*)
		get name of menu bar item mt
		-- {"Apple"}
		tell menu bar item mt to tell menu 1
			get name of menu items
			(*
{"À propos de ce Mac", "Mise à jour de logiciels.", "Logiciels Mac OS X.", missing value, "Préférences Système.", "Dock", "Configuration réseau", missing value, "Éléments récents", missing value, "Forcer à quitter.", "Forcer à quitter Éditeur de scripts", missing value, "Suspendre l'activité", "Redémarrer.", "Redémarrer", "Éteindre.", "Éteindre", missing value, "Fermer la session Yvan KOENIG.", "Fermer la session Yvan KOENIG"}
*)
			get name of menu item mi
			--"Éléments récents"
			tell menu item mi to tell menu 1
				set subMenuItems to get name of menu items
				(*
{"Applications", "Adobe Reader.app", "Aperçu.app", "CocoThumbX.app", "ImageWell.app", "Pages 09English.app", "Pages.app", "Pages09English.app", "récupère_Chemin.app", "TextEdit.app", "Xcode.app", missing value, "Documents", " bug_reports'09.rtfd", " référence à selectSheet.rtf", "2duplicate.applescript", "2Duplicate.numbers", "2duplicate.scpt", "4gradebooks.applescript", "4gradebooks.scpt", "4tests.numbers", "2010_05_03b.jpg", "2010_05_05e.jpg", "2010_05_06j.jpg", "a_library.applescript", "add_date_formats.applescript", "columnsBySpaceAlts.applescript", "com.apple.iWork.Numbers.plist", "com.apple.iWork.Numbers.plist", "createNewTableInHistory.applescript", "DATABASE_TV.numbers", "duplicate Sheet and Table.applescript", "en_US.plist", "five_to_one_alt.applescript", "for_labels.sparseimage", "fr_FR.plist", "getdisplay.sh", "Image 1.jpg", "Image 2.jpg", "Image 3.jpg", "Image 4.jpg", "Image 5.jpg", "Image 6.jpg", "Image 7.jpg", "Image 8.jpg", "Image 9.jpg", "Image 10.jpg", "Image 11.jpg", "Image 12.jpg", "Image 13.jpg", "Image 14.jpg", "Image 15.jpg", "Image 16.jpg", "InfoPlist.strings", "liens_pour_forum.rtfd", "Localizable.strings", "Numbers.sdef", "Pages09_UserGuide.pdf", "Pages09English.dmg", "Perfect_Gradebook YK.numbers", "Perfect_Gradebook.numbers", "prepare_merge_2_labels.applescript", "selection2print_alt.applescript", missing value, "Effacer le menu"}
*)
			end tell
		end tell
	end tell
	return subMenuItems
end inspect_SubMenu

Yvan KOENIG (VALLAURIS, France) samedi 8 mai 2010 19:27:55

Try this,

tell application "System Events"
	set theApp to (get name of first process whose frontmost is true)
	click menu bar item "File" of menu bar 1 of process theApp
	click menu item "Open Recent" of menu 1 of menu bar item "File" of menu bar 1 of process theApp
end tell

Val

Tested this successfully with Word 2008 and Preview

on run
delay 5
try
tell application "System Events"
   set theApp to (get name of first process whose frontmost is true)
   click menu bar item "File" of menu bar 1 of process theApp
   click menu item "Open Recent" of menu 1 of menu bar item "File" of menu bar 1 of process theApp
end tell
end try
end run

Val