0

I am trying to write an AppleScript to export an annotated PDF to a PDF/A file to deal with the long-standing problem of Preview in saving annotations.

I have seen scripts like the following to select menu bar items:

tell application "System Events"
    tell process "Preview"
        click menu item "Export…" of menu 1 of menu bar item "File" of menu bar 1
    end tell
end tell

However, I get the following error regardless of which menu bar item I choose:

System Events got an error: Can’t get menu bar 1 of process "Preview". Invalid index.

How can I resolve this issue?

Running macOS Ventura (13.3).

1
  • This question should be closed as a duplicate. As far as your specific problem is concerned, Preview must be the active app for your click to work. Put this line at the top: tell application "Preview" to activate
    – Mockman
    Apr 5, 2023 at 13:42

1 Answer 1

1

Why not just add a key command to Export…? This will look different in Ventura but the action is the same

enter image description here

then you can replace your existing script snippet with

tell process "Preview" to activate
tell application "System Events" to keystroke "e" with command, shift down

Note: an ellipsis … is not the same as three stops/periods ... You can generate one with Opt ⌥ ; on an English layout.

Or if you're already in Preview… you could just press Cmd ⌘ Shift ⇧ E ;)

7
  • Thank you for your answer. There is an issue with this approach. The keyboard shortcut does not activate unless I click on File once with my mouse. Before that, I keep hearing the "Boop" sound. I was about to post another question about this problem. The proposed solution helps with selecting Export…, which is what my question asked for. But to automate the whole process, I need to select the save dialog and run some operations there, which show a similar kind of error about the window not existing or having incorrect index.
    – Matt
    Apr 5, 2023 at 7:02
  • On a different note, macOS (Ventura) seems to work fine with both Export... and Export…, i.e. three dots or ellipsis.
    – Matt
    Apr 5, 2023 at 7:05
  • 1
    I can't test Ventura but yes, I can confirm ... seems to work as well as … [it never used to]. I can't repro the having to click the menu behaviour. Adding key commands to existing menu items is one of those basic functions that has worked for two decades. Are you using the same cmd/shift/E, which I chose because there's no potential for conflict as far as I can tell.
    – Tetsujin
    Apr 5, 2023 at 7:12
  • Yes, I tried both CMD/SHIFT/E and CMD/CTRL/SHIFT/E, but they both have this issue. If I click on File after I open Preview, the shortcut starts working from that point on.
    – Matt
    Apr 5, 2023 at 7:15
  • 1
    I cannot repro that. We'll have to see if someone on Ventura can - maybe they've broken something. It wouldn't surprise me these days, every OS seems worse than the last, the past few years. Add one new shiny you don't need; break something that's worked well for a decade:\
    – Tetsujin
    Apr 5, 2023 at 7:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .