Different results of running the same AppleScript

applescriptmenu bar

The script is:

tell application "DEVONthink Pro"
    activate
end tell

tell application "System Events"
    tell process "DEVONthink Pro Office"
        tell menu bar 1
            tell menu bar item "Data"
                tell menu "Data"
                    tell menu item "Open With"
                        tell menu "Open With"
                            click menu item "Preview (default)"
                        end tell
                    end tell
                end tell
            end tell
        end tell
    end tell
end tell

If run direct from the AppleScript Editor, it would work as expected, i.e. open the selected file with Preview. But if run from DEVONthink itself, it would do nothing except pause the application for some seconds.

Why is this happening? How to get it work as expected?

Best Answer

The problem may be quite simply that your compiled applescript is not privileged to access DEVONthink-Pro's menus - but ScriptEditor is ...

Have a look into:   System-Preferences-app / Security & Privacy / Privacy
... and select "Accessibility" in the list on your left.

Now you can add your compiled app to the "privileged list" on the right side.