Applescript Service fails because of missing constraints

applescriptautomatorcommand linekeyboardservices

I wrote an Automator Service which executes a small applescript to set the volume and play a sound.

To activate the service, I copied it into /Library/Services and did a chmod +x.
To register a keyboard shortcut for the service, I ran defaults write -g NSUserKeyEquivalents '{"HelloWorld" = "$@~1";}' where HelloWorld is the name of the service as seen in the Services menu and $@~1 my shortcut (which resolves to ⌘+⌥+⇧+1)

After a reboot I can see the service in the menu with the shortcut next to it. When I press the desired key combination, an alert pops telling me that the script encountered an error. There also appears an error in the log:

Detected missing constraints for . It cannot be placed because there are not enough constraints to fully define the size and origin. Add the missing constraints, or set translatesAutoresizingMaskIntoConstraints=YES and constraints will be generated for you. If this view is laid out manually on macOS 10.12 and later, you may choose to not call [super layout] from your override. Set a breakpoint on DETECTED_MISSING_CONSTRAINTS to debug. This error will only be logged once.

This suggests an error with some view that is not initialized correctly, but I'm not using any views.

Here's the content of the Automator Workflow:

on run

    if (get output volume of (get volume settings)) < 50 then
        set volume output volume 50
    end if

    do shell script "afplay /Library/Application\ Support/HelloWorld/hello.mp3"

    return
end run

Best Answer

Since it was requested in the comments (sorry it took that long), here's a working version of the script. The trick is to put the filename (which contains spaces) into double quotes, because those can be escaped.

on run

    if (get output volume of (get volume settings)) < 50 then
        set volume output volume 50
    end if

    do shell script "afplay \"/Library/Application Support/HelloWorld/hello.mp3\""

    return
end run