Search code examples
applescriptautomatormenubarfrench

I can't tell System Events to click menu items with an apostrophe in it


I recently switched my laptop to French from German.

In German, I used an applescript that clicks "Open Recent" in the menu "File" so I can access recent files with a keyboard shortcut. In German this is "Benutzte Dokumente" in the menu "Ablage".

In French it is called "Ouvrir l'élément récent" in the menu "Fichier", and this gives an error. With "Revenir à" and "Partager", both also in "Fichier", it works fine, so it seems to be the apostrophe (').

I'm using it inside an Automator workflow with a shortcut key. Does anybody have an idea?

on run {input, parameters}
    
    tell application "System Events"
        set theApp to (get name of first process whose frontmost is true)
        click menu bar item "Fichier" of menu bar 1 of process theApp
        click menu item "Partager" of menu 1 of menu bar item "Fichier" of menu bar 1 of process theApp
    end tell
    return input
end run

Works as intended. It clicks "Fichier", then clicks "Partager", and I can select options with my arrow keys. However the following:

on run {input, parameters}
    
    tell application "System Events"
        set theApp to (get name of first process whose frontmost is true)
        click menu bar item "Fichier" of menu bar 1 of process theApp
        click menu item "Ouvrir l'élément récent" of menu 1 of menu bar item "Fichier" of menu bar 1 of process theApp
    end tell
    return input
end run

doesn't work. It clicks "Fichier" just fine, but gives the following error message:

L’action « Exécuter un script AppleScript » a rencontré une erreur : « Erreur dans System Events : Il est impossible d’obtenir menu item "Ouvrir l'élément récent" of menu 1 of menu bar item "Fichier" of menu bar 1 of process "TextEdit". »

It's the same error message as when I use a non-existing name. System Events cannot find the menu item.


Solution

  • You could display dialog the title of the menu item and copy-paste it into the script.

    An alternative is to determine the menu item with a whose clause

    on run {input, parameters}
        
        tell application "System Events"
            set theApp to (get name of first process whose frontmost is true)
            click menu bar item "Fichier" of menu bar 1 of process theApp
            click (1st menu item of menu 1 of menu bar item "Fichier" of menu bar 1 of process theApp whose title ends with "élément récent")
        end tell
        return input
    end run