How do I use a variable in a regex?

Trying to have select a document under the “Open Recent” menu in Word.

It works if I use the menu select command with File -> Open Recent ->

However, if I do the same thing but try to replace “myFile” with a variable myTemp, thus writing:

…it doesn’t work. Syntax issue?

The Select Menu Item action does not process text tokens. There are a number of dubious options:

  • If it is a small set of different menus, use a sequence If Then Else actions to select the desired hard coded Select Menu Item action.
  • If it is a small set of different menus, use a regular expression that matches all of them (eg ^(One|Two|Three)$).
  • Use AppleScript UI scripting instead of the Select Menu Item action.
  • Hand craft the Select Menu Item action XML, and use AppleScript do script to the Keyboard Maestro Engine to execute the resulting action.

Hey ajg23,

What you're doing doesn't make obvious sense, since the recent menu rolls over a lot. But perhaps I'm missing something

Did you know you can actually cause a menu to open and stay open?

From there you can type-select to get to the one you want.

On the other hand if you're trying to manage specific documents it makes more obvious sense to track the actual document path.


tell application "Microsoft Word"
  set hfsPath to full name of active document
end tell
if hfsPath contains ":" then
  return POSIX path of hfsPath
  return "false"
end if

Now then. If you genuinely have a need to involve a variable with the Recent Menu it can be done:


set docName to "Goofy.dotx"
tell application "System Events"
  set quit delay to 0
  tell application process "Microsoft Word"
    set frontmost to true
    click menu item docName of menu 1 of menu item "Open Recent" of menu 1 of menu bar item "File" of menu bar 1
  end tell
end tell