Question

I have a Ruby program that, amongst other things, executes user-defined blocks of code (the code is on Github, here). Many of these blocks contain system commands, and I'm trying to debug a very specific case of executing a piece of AppleScript within a bash script that I'm calling. The script in question is for controlling Spotify by the command line, and this is the code.

The problem I'm getting is that an abort trap error is occurring every time the script is run:

spotify: line 57: 94108 Abort trap    osascript -e 'tell application "Spotify" to pause'

This must be related to the environment of the Ruby script where the call is made, but I'm having trouble seeing why. Here's the block of code calling it:

# Ruby code
reaction :hello do |event|
  system "spotify pause"
  system "say 'Hello world'"
  system "spotify play"
end

This block is run periodically via a yield statement, when a particular event is triggered through a socket connection. The script is also daemonized, using the ruby gem "daemons", to allow it to run in the background (but still as the current user, according to ps and the ruby environment variable). Not only this, but each socket connection starts a new thread, so the code is run in a separate thread from the main one.

In my debugging I've tried running the line system "spotify pause" after the daemon starts up the script, without a problem. It also runs fine in the following script:

Thread.new do
  system("spotify pause")
end

So it isn't starting a new thread or daemonizing in itself that causes the problem, but it seems to be a combination of these things and maybe the fact that the block is being yielded that means that an abort trap is being raised by the AppleScript in the bash script.

If you can drag yourself up the many, many layers of complication back into reality, I would appreciate any suggestions you have for solving or even debugging this weird case!

Was it helpful?

Solution

As @regulus6633 mentioned, this is due to Apple's NSApplescript not being thread safe. This combined with daemonizing leads to unpredictable behaviour.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top