ebaauw / homebridge-music

Homebridge plugin for iTunes with AirPlay speakers
Apache License 2.0
92 stars 7 forks source link

applescript error execution error (-1700) #8

Closed martinorob closed 5 years ago

martinorob commented 6 years ago

Hi and thanks for your work.

I have a strange issue,

######### config:

{ "platform": "Music", "service" : "light", "script" : "iTunes", "speakername": ".*", "heartrate" : 5 } ###############

error:

Loading 5 platforms... [9/5/2018, 7:36:04 PM] Plugin /usr/local/lib/node_modules/homebridge-music requires Node version of ^8.11.4 which does not satisfy the current Node version of v10.8.0. You may need to upgrade your installation of Node. [9/5/2018, 7:36:04 PM] [Music] Initializing Music platform... [9/5/2018, 7:36:04 PM] [Music] homebridge-music v0.1.10, node v10.8.0, homebridge v0.4.44 [9/5/2018, 7:36:04 PM] [Music] warning: not using recommended node version v8.11.4 LTS [9/5/2018, 7:36:05 PM] [Music] applescript returned invalid json (SyntaxError: Unexpected token u in JSON at position 0) [9/5/2018, 7:36:05 PM] [Music] applescript: set lib to (load script "/usr/local/lib/node_modules/homebridge-music/scripts/iTunes.scpt") tell lib to getState() [9/5/2018, 7:36:05 PM] [Music] applescript result: undefined

[9/5/2018, 7:41:44 PM] [Music] Music: set on from false to true [9/5/2018, 7:41:44 PM] [Music] applescript error execution error: iTunes ha trovato un errore: Non posso trasformare alcuni dati nel tipo previsto. (-1700)

[9/5/2018, 7:41:44 PM] [Music] applescript: set lib to (load script "/usr/local/lib/node_modules/homebridge-music/scripts/iTunes.scpt") tell lib to setPlayerOn(true, "") [9/5/2018, 7:41:44 PM] [Music] Music: could not set on to true

##############

may you help me?

Thanks

ebaauw commented 6 years ago
[9/5/2018, 7:36:05 PM] [Music] applescript returned invalid json (SyntaxError: Unexpected token u in JSON at position 0)
[9/5/2018, 7:36:05 PM] [Music] applescript: set lib to (load script "/usr/local/lib/node_modules/homebridge-music/scripts/iTunes.scpt")
tell lib to getState()
[9/5/2018, 7:36:05 PM] [Music] applescript result: undefined

Not sure about this one. Could be related to #7. What version of macOS and iTunes are you running? What model mac?

[9/5/2018, 7:41:44 PM] [Music] Music: set on from false to true
[9/5/2018, 7:41:44 PM] [Music] applescript error execution error: iTunes ha trovato un errore: Non posso trasformare alcuni dati nel tipo previsto. (-1700)

[9/5/2018, 7:41:44 PM] [Music] applescript: set lib to (load script "/usr/local/lib/node_modules/homebridge-music/scripts/iTunes.scpt")
tell lib to setPlayerOn(true, "")
[9/5/2018, 7:41:44 PM] [Music] Music: could not set on to true

You're asking iTunes to play, but it has no current track. Select a track in iTunes, or set "track" in config.json to specify a default track.

martinorob commented 6 years ago

Thanks @ebaauw for the quick response. macOS 10.13.6 and iTunes 12.8.0.150

node version: v10.8.0 npm version: 6.4.1

okay, I'll put the default track on the track config.

For the error I have found this:

"JSON.parse is choking because unlike Javascript, JSON requires all key names to be in quotes [0], so your JSON should be

{"userna me":rv,"password":password} The error "Unexpected token u..." is occurring when the JSON parser encounters the "u" at the beginning of "username", when it expected a quotation mark.

[0] There is a very readable summary of the JSON spec at http://json.org."

source: https://stackoverflow.com/questions/34355770/syntaxerror-unexpected-token-u-at-object-parse-native-nodejs-for-json-parse

May be this the problem?

ebaauw commented 6 years ago

The invalid json is indeed caused by the Applescript response undefined. I don’t know what causes Applescript to return this.

martinorob commented 6 years ago

so I have to troubleshoot on the /usr/local/lib/node_modules/homebridge-music/scripts/iTunes.scpt script?

is there any way to run that script passing some parameter and then verify the response?

(so I can fix my problem without disturb you)

martinorob commented 6 years ago

I think I have found the problem,

the response of the setPlayerOn give me a wrong json without the ""

Original get "{\"on\":" & (player state is playing) & ",\"track\":\"" & t & "\"}"

CORRECT:

    get "{\"on\":\"" & (player state is playing) & "\",\"track\":\"" & t & "\"}"
ebaauw commented 6 years ago

Yes, you can open the script in Script Editor (just double-click on the file in Finder), and add the getState() command to the end and run it.

I haven't seen your entire log, but it would seem the getState() command only returned undefined once, or homebridge-music wouldn't have exposed any HomeKit accessories at all. You might want to run homebridge -D to see all applescript commands generated and their return values.

ebaauw commented 6 years ago

Original

No, looks like you changed the script, see https://github.com/ebaauw/homebridge-music/blob/9eaf1f18e02bb508d2246712bc09737b5886b20d/scripts/iTunes.applescript#L35

martinorob commented 6 years ago

very strange...

i have copy pasted the iTunes.scpt and iTunes.applescript from GitHub and the error persist..

the strange think Is that without play/pause, the script one time return undefined and the second time returns the correct state of iTunes:

logs:

[9/6/2018, 7:26:21 PM] [Music] applescript timeout [9/6/2018, 7:26:21 PM] [Music] applescript: set lib to (load script "/usr/local/lib/node_modules/homebridge-music/scripts/iTunes.scpt") tell lib to getState() [9/6/2018, 7:26:28 PM] [Music] applescript returned invalid json (SyntaxError: Unexpected token u in JSON at position 0) [9/6/2018, 7:26:28 PM] [Music] applescript: set lib to (load script "/usr/local/lib/node_modules/homebridge-music/scripts/iTunes.scpt") tell lib to getState() [9/6/2018, 7:26:28 PM] [Music] applescript result: undefined [9/6/2018, 7:26:32 PM] [Music] applescript: set lib to (load script "/usr/local/lib/node_modules/homebridge-music/scripts/iTunes.scpt") tell lib to getState() [9/6/2018, 7:26:32 PM] [Music] applescript result: {"on":true,"volume":29,"track":"Oltremare (feat. Eric Legnini, Lars Danielsson & Manu Katché)","speakers":{"Computer":{"on":false,"volume":0},"BagnoSatellite":{"on":true,"volume":16},"Camera":{"on":true,"volume":20},"Salotto":{"on":true,"volume":28},"Bagno":{"on":false,"volume":28},"CASA":{"on":false,"volume":0},"iM@rtinoMBP":{"on":false,"volume":28},"TV":{"on":false,"volume":28}}}

ebaauw commented 6 years ago

As I thought, it's an intermittent problem, the error only occurs occasionally, not every time. Probably related to the system load, or iTunes being stuck or busy with something else (like serving the library to another computer, syncing an iPhone or iPad, displaying a popup, ...). I don't like it, but it's safe to ignore the error, as long as this happens occasionally, and homebridge-music starts ok (exposing the player and speakers to HomeKit).

[9/6/2018, 7:26:21 PM] [Music] applescript timeout

This means that getState() didn't return any response at all within 5 seconds. Unfortunately, this duration is hard-coded, see https://github.com/ebaauw/homebridge-music/issues/7#issuecomment-417944662.

[9/6/2018, 7:26:28 PM] [Music] applescript result: undefined

The next polling cycle, getState() returns undefined, or basically an empty response. As I said earlier, I don't understand how that's possible - I've never seen this before and I'm not able to reproduce it here.

[9/6/2018, 7:26:32 PM] [Music] applescript result: {"on":true,"volume":29,"track":"Oltremare (feat. Eric Legnini, Lars Danielsson & Manu Katché)","speakers":{"Computer":{"on":false,"volume":0},"BagnoSatellite":{"on":true,"volume":16},"Camera":{"on":true,"volume":20},"Salotto":{"on":true,"volume":28},"Bagno":{"on":false,"volume":28},"CASA":{"on":false,"volume":0},"iM@rtinoMBP":{"on":false,"volume":28},"TV":{"on":false,"volume":28}}}

Now, it again returns the expected response.

ebaauw commented 6 years ago

Look what I found:

[2018-9-6 22:05:51] [Music] applescript result: {"on":false,"volume":80,"track":"","speakers":{"Computer":{"on":false,"volume":80},"apex":{"on":false,"volume":80},"Jacco TV":{"on":false,"volume":80},"Living Room TV":{"on":false,"volume":80}}}
^C[2018-9-6 22:05:56] Got SIGINT, shutting down Homebridge...
[2018-9-6 22:05:56] [Music] applescript returned invalid json (SyntaxError: Unexpected token u in JSON at position 0)
[2018-9-6 22:05:56] [Music] applescript: set lib to (load script "/Users/ebaauw/GitHub/homebridge-music/scripts/iTunes.scpt")
tell lib to getState()
[2018-9-6 22:05:56] [Music] applescript result: undefined

This happened when I pressed ctrl-C to quit homebridge, which was running in foreground. Apparently, the child process running osascript got the interrupt as well. Makes me suspect the error could be related to having not enough resources to run the child process (too many processes, open files, ...?).

martinorob commented 6 years ago

yes, is a random error.. but the "u" token what can be? I have tried to change something starting with "u" with a different character (like "a") around applescript.. but no changes.. (only to understand what is the "u" token..

ebaauw commented 6 years ago

It’s not returned by the script. The applescript library that executes the applescript command does not set a return value, which is converted to the string undefined in Javascript.

martinorob commented 6 years ago

ohhhhhhhh that's the "u"!!!!!!!

martinorob commented 6 years ago

may I add a timeout exit code like this?

(top of iTunes.applescript) try with timeout of 10seconds

scripts here

(on end of script)

end timeout on error end try

do you think that will work?

ebaauw commented 6 years ago

It’s already there, see #7.