Fetching iTunes Comments wraps it in Quotes and Adds Escapes

Hi all, I want to grab the content in my iTunes track Comments and insert it into the middle of a running script (sending out my serial port), which is working, but I’m having two issues:

  1. The entire comment is being returned enclosed in double quotes, which I do not want.
  2. Inside the comment is a hex code which needs to be quoted, but AppleScript is adding an escape backslash in front of the quotes when it returns it. I’ve tried to use " instead of the quotes in the Comment but AS just adds additional backslashes when it brings it over.

In Comments and what I want to copy and insert into my script: “[1A22C]” to myPort
What AppleScript inserts into my script: [b]“[/b]”[1A22C][b][/b]" to myPort"

So is there a way to not have AppleScript wrap the text in double quotes and not have it add the escape backslashes?

FYI, using iTunes 10.7 (21)

Hi. Welcome to MacScripter.

It sounds as if you’re looking at the AppleScript source code for the string you want. For instance:

set myText to "\"[1A22C]\" to myPort"

display dialog myText

The text has to be in double quotes in Script Editor to signify that it’s text. Double-quote characters within the text have to be “escaped” with backslashes to show that they’re characters and not AppleScript text markers. But the text represented is what you want.

Hi Nigel, thank you for the quick reply. So some clarification…

myPort is set for sending out my serial port as:
set myPort to serialport open use_port bps rate 1200 data bits 8 parity 0 stop bits 1 handshake 0

And I need to grab the comment from iTunes:
tell application “iTunes”
set current_tracks_comment to comment of current track
end tell

And then I want to insert it into the running script to send out the serial port just like the other serialport write’s:
tell application “AppleScript Editor”
serialport write “[1A2CE]” to myPort
serialport write “[1A215]” to myPort
serialport write current_tracks_comment
serialport write “[1A212]” to myPort
serialport close
serialport close myPort
end tell

So, when I use “[1A22C]” to myPort in my Comment – I get:
serialport write “[1A215]” to 0
serialport write “"[1A22C]" to myPort”
serialport write “[1A212]” to 0
Note: I need to have the brackets and double quotes surrounding the hex code for my serial device to recognize it.

And when I change to what I thought you were suggesting (set myText to “"[1A22C]" to myPort”) in my Comment as “"[1A22C]" to myPort” – then I get:
serialport write “[1A215]” to 8
serialport write “"\"[1A22C]\" to myPort"”
serialport write “[1A212]” to 8

So as you see, when it comes over from iTunes it looks like AppleScript is adding in the escapes and surrounding the comment in double quotes, of which I don’t want either.

Sorry chrispy1961. You’ve lost me completely.

AppleScript Editor (or Script Editor as it is now) doesn’t have ‘serialport’ commands. I don’t know where they come from.

The script I posted previously was a demonstration that the additional quotes and escaping you see in Script Editor aren’t actually in the text but are necessary to represent that text in a script. If you run the script, you see the actual text contents in the dialog.

However, I notice you have ‘to myPort’ in your track comment. That looks as if it should be part of the script code rather than the text you want to send to the serial port. I think you should be giving the tracks the comments you actually want to send ” such as [1A22C] (without quotes) ” and writing the script something like this:

set myPort to serialport open use_port bps rate 1200 data bits 8 parity 0 stop bits 1 handshake 0

tell application "iTunes"
	set current_tracks_comment to comment of current track        
end tell

serialport write "[1A2CE]" to myPort
serialport write "[1A215]" to myPort
serialport write current_tracks_comment to myPort -- 'to myPort' part of the script code.
serialport write "[1A212]" to myPort
serialport close -- This looks superfluous.
serialport close myPort

Hello Nigel

I guess that the asker is using Arduino.

Yvan KOENIG running Sierra 10.12.0 in French (VALLAURIS, France) lundi 17 octobre 2016 20:55:46

Sorry for the confusion Nigel, I’m using SerialPort X (https://www.macupdate.com/app/mac/20440/serialport-x) which is working fine.

And I understand that escaping I see in Script Editor aren’t actually in the text, yet when I grab the comment from iTunes, it adds them in when I try to insert the comment into my script. I can do a Display Dialog current_tracks_comment and it shows the text exactly how I want to see it, but when it gets inserted into my script the quotes and escapes get added (I see this under the Replies/Result tab) and the hex code does not get sent out my serial port as I need it to.

I think you may be able see this yourself if you add the following into a song track’s Get Info/Comment in iTunes and use the following AppleScript on the playing track:

“[1A22C]” to myPort


tell application "iTunes"
   set current_tracks_comment to comment of current track        
end tell

I want/need “[1A22C]” to myPort to send through the serial port, but I get “"[1A22C]" to myPort” instead (as seen in the Result).

Lastly, I actually have five unique hex code’s (not just the one) I need to send out through the serial port, from the Comment, so I was hoping to have them follow one another in the middle of the script, so I can’t pull out the to myPort as suggested.

Actually I’m not. I’m trying to control an old Pioneer CD Changer through a semi-custom built serial controller, which converts hex codes to something the Pioneer understands. I would be interested though if the Arduino could be made to do the same thing…

If this helps, here’s what I’m trying to grab from the iTunes track comment and the script itself. I really appreciate the help guys! :slight_smile:

iTunes Comment:
“[1A22C]” to myPort
delay 1
serialport write “[1A227]” to myPort
delay 1
serialport write “[1A21C]” to myPort
delay 1
serialport write “[1A22C]” to myPort
delay 1
serialport write “[1A227]” to myPort

Script:


set use_port to item 1 of (get serialport list)
set myPort to serialport open use_port bps rate 1200 data bits 8 parity 0 stop bits 1 handshake 0
delay 1
if myPort is equal to -1 then
	display dialog " could not open port "
else -- send command 	
	tell application "iTunes"
		set current_tracks_comment to comment of current track
	end tell
	tell application "AppleScript Editor"
		-- POWER ON   'in case it was off
		serialport write "[1A2CD]" to myPort
		delay 1
		-- STOP       'in case it was on and playing
		serialport write "[1A2CE]" to myPort
		delay 1
		-- STOP       'clear program step
		serialport write "[1A2CE]" to myPort
		delay 1
		-- STOP       'clear program mode
		serialport write "[1A2CE]" to myPort
		delay 1
		-- PROGRAM    'put player into program mode
		serialport write "[1A215]" to myPort
		delay 1
		
		serialport write current_tracks_comment
		
		delay 1
		-- TRACK SET  'finalize the track command
		serialport write "[1A212]" to myPort
		delay 1
		-- PLAY       'set player to play cut...
		serialport write "[1A2CF]" to myPort
		delay 1
		serialport close myPort
	end tell
end if

Hi chrispy1961.

I’d just found SerialPort X myself and was about to look at it. :slight_smile:

But I think you still have a couple of misunderstandings here.

Firstly, if you’re talking about the iTunes result as seen in Script Editor’s Result pane, that’s exactly right. It’s an AppleScript code representation of text (indicated by the outer quotes) which itself contains a couple of quote characters. These are escaped with backslashes to indicate that they’re characters and not the things indicating the ends of the text in the AppleScript code. The actual text, stored in the variable current_tracks_comment, is exactly what’s come out of the iTunes comment.

The other thing is that the text in the comment is text. You can’t just write serialport write current_tracks_comment and expect the text in the variable to be treated as compiled script code. But you may be able to get round that with the ‘run script’ command:

-- Blah blah blah.

-- PROGRAM 'put player into program mode
serialport write "[1A215]" to myPort
delay 1

run script ("serialport write " & current_tracks_comment)

delay 1
-- TRACK SET 'finalize the track command
serialport write "[1A212]" to myPort

-- More blah.

Hi Nigel, the Run Script did the same thing with the text in the iTunes Comment, it was wrapped it in quotes and escaped the other quotes.

I kinda found a workaround as I can use some of the unused fields (i.e. grouping and composer) to put my hex commands in the without quotes, and when I call those they come in correctly. Not elegant but workable.

Disappointing there is not a way around AppleScript forcing the formatting and also disappointing I can’t add custom fields in the Get Info window, but I’ll make it work.

Thanks for all the help! Appreciate it!

chrispy,

I’d need more information to understand if there’s a reason why you have to do it this way, but it’s a weird, hack-y sort of thing to put executable code in the contents of iTunes comments and then extract that and run it as script commands. Do the commands ever change, or just the hex codes?

When you say “Disappointing there is not a way around AppleScript forcing the formatting,” I’m pretty sure, from my use of Applescript, that that is not actually the case here, it’s just that what you’re trying to do here is basically metascripting - your script writes and executes another script - and things get complicated really fast when you do that. Since I don’t know the use case, I don’t know if there’s a good reason to be writing a script that writes and runs another script, getting the code for the second script out of comments on iTunes files, but my guess is going to be “no.” I think it’s getting unnecessarily complicated. I’m guessing that the only information that has to change per track is the codes, not the script commands.

I’m also not sure why you have the

tell application “Applescript Editor”

in there.

Can you just make the track comment the list of codes you need to send, separated by spaces?

So in this case, make the track comment in iTunes exactly:

[1A22C] [1A227] [1A21C] [1A22C] [1A227]

And then use this script.


set use_port to item 1 of (get serialport list)
set myPort to serialport open use_port bps rate 1200 data bits 8 parity 0 stop bits 1 handshake 0
delay 1
if myPort is equal to -1 then
	display dialog " could not open port "
else -- send command 	
	tell application "iTunes"
		set current_tracks_comment to comment of current track
	end tell
	-- Parse ports from text comment to list
	set delimitHolder to AppleScript's text item delimiters
	set AppleScript's text item delimiters to " "
	set trackCodes to the text items of current_tracks_comment
	set AppleScript's text item delimiters to delimitHolder
	
	set initilizationCodes to {"[1A2CD]", "[1A2CE]", "[1A2CE]", "[1A2CE]", "[1A215]"}
	set finalCodes to {"[1A212]", "[1A2CF]"}
	
	send_cd_command(initilizationCodes, myPort)
	send_cd_command(trackCodes, myPort)
	send_cd_command(finalCodes, myPort)
end if

on send_cd_command(portList, myPort)
	repeat with aPort in portList
	serialport write aPort to myPort
	delay 1
        end repeat
end send_cd_command

Let me know if that helps.

Note - I did not install SerialPort X, so I was unable to compile the script, so it may contain typos.

WHOA! AWESOME, t.spoon, that worked! Thank you! The only addition I had to add was to close the serial port.

Some answers to your questions, and then my next step/issue:

Q: I’d need more information to understand if there’s a reason why you have to do it this way
A: I’m trying to do the following, in order: (1) iTunes kicks off AS when song in playlist (all CD changer songs) changes → (2) AS grabs song comments → (3) AS sends Stop command out serial port (USB) to CD changer(s) (4) AS parses/assembles hex codes & commands and sends out serial port with Play command → (5) serial box h/w device routes (#3 & #4) to correct CD changer (6) physical CD plays on CD changer (7) back to #1 —^

Q: Do the commands ever change, or just the hex codes?
A: The hex codes are the commands. Example, within the code (i.e. [1A22C]) it defines which CD changer number (“1”)(I have 4 changers), that they are Pioneer (“A2”)(versus Sony) and number (“2C” = 1). You first send the Disc numbers and then set the disc with [1A21C] and then the Track numbers and set the track with [1A212]. There’s also codes for turning on the changer, set it to program mode, and etc.

Q: I’m guessing that the only information that has to change per track is the codes, not the script commands
A: Correct, CD Changer, Disc, and Track actually.

Q: I’m also not sure why you have the tell application “Applescript Editor” in there.
A: That was my way of jumping back & forth from iTunes, to putting the codes together, to sending out the serial port. Obviously I’m a novice and I appreciate the help!

Which leads to my next pieces of the puzzle, which I think I have “A” semi-figured out, but need help figuring out “B.” Obviously the Comments will hold the codes according to the CD changer it is in, but the other codes in the script will need to be dynamic (i.e. Play command). Thoughts on this?

A. I need to Stop the currently playing CD changer song before sending out the next Play command (Stop Commands: [1A2CE] or [2A2CE] or [3A2CE] or [4A2CE]) – I’m thinking I just send out Stop command to all four CD changers (versus tracking which one the CD is playing on).
B. I have 4 CD changers, with specific CD changer song Play Commands ([1A2CF] or [2A2CF] or [3A2CF] or [4A2CF]), other commands, and of course digit numbers:

Digit Changer 1 Changer 2 Changer 3 Changer 4
0 [1A222] [2A222] [3A222] [4A222]
1 [1A22C] [2A22C] [3A22C] [4A22C]
2 [1A227] [2A227] [3A227] [4A227]
3 [1A22B] [2A22B] [3A22B] [4A22B]
4 [1A221] [2A221] [3A221] [4A221]
5 [1A224] [2A224] [3A224] [4A224]
6 [1A22E] [2A22E] [3A22E] [4A22E]
7 [1A22F] [2A22F] [3A22F] [4A22F]
8 [1A229] [2A229] [3A229] [4A229]
9 [1A22A] [2A22A] [3A22A] [4A22A]

I realized the hex codes were “Commands” to the CD Player(s), but I meant Applescript commands. To Applescript, the hex codes are data and the commands are the terms like “serialport write.”

This is too much for me to just write for you, but some thoughts:

  • I’d add another piece of data to the comment track that says which CD changer the track is on, and then parse that back out separately from your codes.
  • I’d build an Applescript record, with the top level items of the record representing each changer, and then subrecords using the functions you need to perform as keys and the hex codes to do them on each player as values. Then you can use the Player information pulled from the track comment to know which record to extract the codes from.

Hi t.spoon, your first comment made me realize I was doing everything way too hard. Being that the codes drive everything, I was able to get everything working by just putting “all” the codes in the Comments, and let your script cleanup piece fix everything and send out the serial port. So that part works great! Thanks!

BUT now I can’t get the On Idle (stay open application) piece to work, so when the song changes it sends out the next set of codes. Thought it’d be as simple as wrapping everything between the On/End Idle, but nope. Then I thought it needed to be on just the part where it detects a song change, but it only works for the first song. I’ve tried all kinds of combinations and just can’t get it to do what I need it to do…monitor when iTunes starts playing a new song and grab the Comments.

Here’s the script which is not working fully for me (plays one song). Anybody have any suggestions?


global latest_song

-- Set Serial Port
set use_port to item 1 of (get serialport list)
set myPort to serialport open use_port bps rate 1200 data bits 8 parity 0 stop bits 1 handshake 0
delay 1

if myPort is equal to -1 then
	display dialog " could not open port "
end if

-- Set variable latest_song to nothing
set latest_song to ""

-- Check iTunes if new song is playing - if new song then copy comments
on idle
	tell application "iTunes"
		copy name of current track to current_tracks_name
		if current_tracks_name is not latest_song then
			copy current_tracks_name to latest_song
			set current_tracks_comment to comment of current track
		end if
		return 3
	end tell
end idle

-- Fix iTunes Comments code
set delimitHolder to AppleScript's text item delimiters
set AppleScript's text item delimiters to " "
set trackCodes to the text items of current_tracks_comment
set AppleScript's text item delimiters to delimitHolder

-- Send code out serial port
send_cd_command(trackCodes, myPort)
on send_cd_command(portList, myPort)
	repeat with aPort in portList
		serialport write aPort to myPort
		delay 1
	end repeat
end send_cd_command
delay 2
serialport close myPort