Applescript conflict with the iTunes constants 'error' and 'duplicate'

In AppleScript the keyword 'error' is a reserved word. Unfortunately iTunes also defines a constant called 'error used as a possible value for e.g. a track's iCloud status. As a result code such as:


if aTrack's cloud status is equal to purchased then

set tCloudStatus to "purchased"

else if aTrack's cloud status is equal to matched then

set tCloudStatus to "matched"

else if aTrack's cloud status is equal to uploaded then

set tCloudStatus to "uploaded"

else if aTrack's cloud status is equal to ineligible then

set tCloudStatus to "ineligible"

else if aTrack's cloud status is equal to removed then

set tCloudStatus to "removed"

else if aTrack's cloud status is equal to duplicate then

set tCloudStatus to "duplicate"

else if aTrack's cloud status is equal to subscription then

set tCloudStatus to "subscription"

else if aTrack's cloud status is equal to no longer available then

set tCloudStatus to "no longer available"

else if aTrack's cloud status is equal to not uploaded then

set tCloudStatus to "not uploaded"

else if aTrack's cloud status is equal to error then

set tCloudStatus to "error"

else

set tCloudStatus to "unknown"

end if


Fails to even save as it complains about the use of 'error''. There is also a similar problem with 'duplicate' (that is also an AppleScript term).


Does anyone know how I can force the code to use the iTunes meaning of these terms and not the AppleScript meaning? I tried 'using terms from application "iTunes"' but that did not resolve the problem.


Thanks for any pointers...


Chris

if aTrack's cloud

else

set tCloudStatus to "unknown"

end if

Posted on May 29, 2016 8:37 AM

Reply
15 replies

May 29, 2016 9:51 AM in response to Niel

Hi Niel,


Thanks for the reply. I has already tried that - sadly what I get then are values like this:


«constant ****kUnk»

«constant ****kErr»


But this is just some strange representation since if I try and compare the actual value to these strings they do not match.


Sometimes I just hate AppleScript! So powerful and yet so obscure and so poorly documented.

May 29, 2016 12:23 PM in response to ChrisJenkins

Those «constant ****kUnk» things are the raw codes for the application constants. These constants are (usually) shown in the Script Editor as a convenience, but for comparisons and use outside of the application tell statement, you should coerce them to text/string as first posted by Neil.


Doing it the long way as in your example it would be something like:

if (aTrack's cloud status as text) is equal to "error" then
--

May 30, 2016 6:59 AM in response to red_menace

I get the same as you running the following code within Script Editor...


tell application "iTunes"

set aTrack to cloud status of current track --> currently playing track

set aTrack to aTrack as text

end tell

display alert "aTrack: " & aTrack as informational


Gives me this:

User uploaded file


But if I save this as a script and run it via osascript, or if I save it as an application and run that then it does not work. Instead I get:

User uploaded file

So something seems to be broken...

May 30, 2016 9:51 AM in response to ChrisJenkins

Ah. Yes, that does seem to be broken.

In that case, you'll just need to beat on it the hard way (at least until it gets fixed):


# iTunes 'cloud status' constants:
property kPurchased : «constant ****kPur»
property kMatched : «constant ****kMat»
property kUploaded : «constant ****kUpl»
property kIneligible : «constant ****kRej»
property kRemoved : «constant ****kRem»
property kError : «constant ****kErr»
property kDuplicate : «constant ****kDup»
property kSubscription : «constant ****kSub»
property kNotAvailable : «constant ****kRev»
property kNotUploaded : «constant ****kUpP»
property kUnknown : «constant ****kUnk»

tell application "iTunes"
  set aTrack to current track -- current track
  set status to aTrack's cloud status
  if status = kPurchased then
    set tCloudStatus to "purchased"
  else if status = kMatched then
    set tCloudStatus to "matched"
  else if status = kUploaded then
    set tCloudStatus to "uploaded"
  else if status = kIneligible then
    set tCloudStatus to "ineligible"
  else if status = kRemoved then
    set tCloudStatus to "removed"
  else if status = kDuplicate then
    set tCloudStatus to "duplicate"
  else if status = kSubscription then
    set tCloudStatus to "subscription"
  else if status = kNotAvailable then
    set tCloudStatus to "no longer available"
  else if status = kNotUploaded then
    set tCloudStatus to "not uploaded"
  else if status = kError then
    set tCloudStatus to "error"
  else
    set tCloudStatus to "unknown"
  end if
end tell

display dialog tCloudStatus

May 30, 2016 11:19 AM in response to ChrisJenkins

Hello


Well, it is a known issue. AppleScript terminology is not usually loaded at run time but at compile time. Fortunately there's workaround.


To get termilogy string from constant at run time in complied script run in applet or run by osascript, you need to manually load terminology. One way is to issue a magic event ascrgdut (applescript get dynamic user terminology) in tell block targeted to the application of which termilogy is to be loaded.


The following script demonstrates how to use it.



--loadTerminology(me) -- to load global terminology such as OSAX. tell application "iTunes" activate my loadTerminology(it) -- to load the target application's terminology display dialog (player state as string) end tell on loadTerminology(a) (* reference a : reference of which terminology is to be loaded. *) try tell a to «event ascrgdut» -- (AppleSCRipt : Get Dynamic User Terminology) end try end loadTerminology




You may save this as a complied script and run it by osascript or save as an applet and run it, and see the effect of loadTerminology(). Without loading terminology, raw constant code will be shown, whereas with loading it, raw code is properly decompiled into corresponding terminology.


(Note that if you compile and execute AppleScript source code (.applescript file) by osascript, raw code is decompiled into terminology without calling loadTerminology(). This is because osascript loads terminology in order to compile the source code and no need to reload it at run time.)



Good luck,

H


PS. Although this and other methods already provided may work around the immediate problem, the terminology conflicts which cause the problem in the first place remain to be reported for correction.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Applescript conflict with the iTunes constants 'error' and 'duplicate'

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.