AppleScript: "System Events" from Terminal not working

49 Views Asked by At

TASK: I'm using AppleScript to open a Numbers file and hide a column. After selecting the column that I want to hide I'm using "System Events" to go in the Numbers menu and hide the column.

ISSUE: When I launch the script from the AS Editor the script works but when I launch the same script from terminal using osascript the "System Events" portion of the script doesn't work (the column is selected but doesn't get hidden).

WHAT I TRIED: In Settings -> Privacy and Security -> Automation: the Terminal App has both Numbers and System Events enabled In Settings -> Privacy and Security -> Accessibility: the Terminal App is enabled.

Here is the code (the System Events portion)

tell application "System Events"
    tell process "Numbers"
        click menu item "Hide Column" of menu 1 of menu bar item "Table" of menu bar 1
    end tell
end tell
2

There are 2 best solutions below

0
Knyq On

My "System Events" call was enveloped by "ignoring application responses - end ignoring".

The actual code was:

ignoring application responses
    tell application "System Events"
        tell process "Numbers"
            click menu item "Hide Column" of menu 1 of menu bar item "Table" of menu bar 1
        end tell
    end tell
end ignoring

The part within the "ignoring" is actually ignored when running the code through terminal, while it works if running through Script Editor.

0
Mockman On

Glad you got it working. FWIW, here is what I meant about using 'perform'

tell application "Numbers"
    activate
    tell application "System Events"
        tell process "Numbers"
            
            set menuTable to menu "Table" of menu bar item "Table" of menu bar 1
            -- open Table menu
            perform action "AXPress" of menu bar item "Table" of menu bar 1
            delay 1 / 30
            -- perform Hide Column
            perform action "AXPress" of menu item "Hide Column" of menuTable
            
        end tell
    end tell
end tell

A note on the ignoring application responses command. It's really intended for situations where you don't need (or want) to deal with extraneous dialogues, for example, if your script has an empty the trash command. You can put commands in your script that should ensure that the items moved to the trash are correct and therefore, the 'are you sure' dialogue is unwanted. Maybe all of the 'process' stuff is inherently an 'application response'… thus the ignore.