do shell script with sed runs OK in Editor but not in .app

Hello,

this is killing me softly…

tell application "iTunes" -- have some tracks from the internal HD selected
selected
	set classlist to (get class of selection of front window)
	set OKTraks to (do shell script "echo " & classlist & " | sed -E s/'device track|file track'/something/g")
	display dialog the result
end tell

It runs fine from the Script Editor (returns “somethingsomethingsomething…”, but saved as application or in an XCode built it returns a row of «class cFIT»s.

Grateful for your input,
LG

The problem you are running into is that the objects in the list returned by get class of selection of front window are not strings, they are class objects. The ‘raw’ name for iTune’s file track class is «class cFlT». When you concatenate that list of class objects onto a string (. "echo " & classlist .), the class objects have to be converted into strings. For whatever reason ScriptEditor is able to convert them to “human readable” forms, but when saved as an applet (or XCode application, etc.), the applet only knows about the raw forms. I suspect the reason is that ScriptEditor has loaded the dictionary required for encoding (compile) and decoding (display) the raw forms, but the applet has no need to load the dictionary (the code is already compiled and does not need to be displayed), so it does not bother to decode them.

You could try extending your sed program to handle the raw forms. On US keyboards you can type « with Option-Backslash and » with Shift-Option-Backslash. Be sure to copy and paste the raw form text though. It looks like you mistyped it in your post (on my system, file track is lower-C upper-F, lower-L, upperT (lower-L, not upper-I)). Here is some code that might be useful for tracking for other raw forms:

set tempScriptPath to "/tmp/tempScript.scpt"
set termText to "file track"
do shell script "/usr/bin/osacompile -e 'using terms from app \"iTunes\"' -e " & quoted form of termText & " -e 'end' -o " & quoted form of tempScriptPath & ";/usr/bin/osascript " & quoted form of tempScriptPath

Trying to ‘massage’ AppleScript classes with sed just seems like a bad idea to me. What are you really trying to accomplish here? If you are really just doing some string substitution, you do that without sed and reliably use whatever substitution string you want for each class object.

-- inside an iTunes block (either tell or using terms)
	set stringlist to {}
	repeat with c in classlist
		set c to contents of c
		if c is equal to file track then
			set end of stringlist to "a file track"
		else if c is equal to device track then
			set end of stringlist to "an iPod track"
		else
			set end of stringlist to "UNKNOWN"
		end if
	end repeat
	stringlist -- OR "stringlist as text" to combine them into a single string (uses text item delimiters)

Chris,

A big Thank You for the reply. It was most helpful, tacking my problems from all sides. I was not aware that a class had a content, nor of the human readable forms. With your help I got my routine to work. :slight_smile:

The sed was indeed a despaired attempt to work around the issue with ‘get class of every item of’ and ‘get contents of class of every item of’ that only work on a single object – I wanted to avoid looping through the tracks/classes for performance reasons. Seems impossible to do with AppleScript.

And your code for raw forms goes to the ‘prescious tricks’ repository. Thanks for that, too!

LG

I once saw this idea some where, to get the class as a string.
Could not find the actual example…

I used finder here but should work in iTune also.

tell application "Finder"
	set stringlist to {}
	set TheSelection to (get selection)
	repeat with i from 1 to number of items in TheSelection
		set this_item to item i of TheSelection
		try
			get name of class of this_item
		on error ermsg
			set ClassType to characters -2 thru 19 of ermsg as string
			copy ClassType to end of stringlist 
		end try
	end repeat
end tell
 stringlist 

Mark,

Thank you! I think that should do the trick.

LG