ChrisJenkins

Q: 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 1:02 PM

Close

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

  • All replies
  • Helpful answers

Page 1 Next
  • by Niel,

    Niel Niel May 29, 2016 9:24 AM in response to ChrisJenkins
    Level 10 (313,733 points)
    Mac OS X
    May 29, 2016 9:24 AM in response to ChrisJenkins

    Try using:

     

    set tCloudStatus to aTrack's cloud status as string

     

    (142604)

  • by ChrisJenkins,

    ChrisJenkins ChrisJenkins May 29, 2016 9:51 AM in response to Niel
    Level 1 (23 points)
    Apple Music
    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.

  • by VikingOSX,

    VikingOSX VikingOSX May 29, 2016 11:58 AM in response to ChrisJenkins
    Level 7 (21,165 points)
    Mac OS X
    May 29, 2016 11:58 AM in response to ChrisJenkins

    Have you tried |duplicate| and |error|?

  • by red_menace,

    red_menace red_menace May 29, 2016 12:23 PM in response to ChrisJenkins
    Level 6 (15,546 points)
    Desktops
    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
    --
    
  • by Hiroto,

    Hiroto Hiroto May 29, 2016 10:24 PM in response to ChrisJenkins
    Level 5 (7,341 points)
    May 29, 2016 10:24 PM in response to ChrisJenkins

    Hello

     

    Terminology conflict is one of the most annoying and the ugliest things in AppleScript language.

     

    Obviously they have never tested those AppleScript terminologies of iTunes in question to work in real script. I'd recommend your sending bug report to Apple on this matter.

     

    Regards,

    H

  • by ChrisJenkins,

    ChrisJenkins ChrisJenkins May 30, 2016 2:28 AM in response to red_menace
    Level 1 (23 points)
    Apple Music
    May 30, 2016 2:28 AM in response to red_menace

    I tried |error| and |duplicate| - they give a runtime error saying that those variables are not defined...

  • by ChrisJenkins,

    ChrisJenkins ChrisJenkins May 30, 2016 2:34 AM in response to red_menace
    Level 1 (23 points)
    Apple Music
    May 30, 2016 2:34 AM in response to red_menace

    As I said to Neil, that does not work. Coercing them to strings does not result in any kind of meaningful string (certainly not the string "error" for example)...

  • by ChrisJenkins,

    ChrisJenkins ChrisJenkins May 30, 2016 2:33 AM in response to Hiroto
    Level 1 (23 points)
    Apple Music
    May 30, 2016 2:33 AM in response to Hiroto

    I think I will have to log a bug, and then wait months/years for them to decide to fix it :-(

  • by VikingOSX,

    VikingOSX VikingOSX May 30, 2016 3:31 AM in response to ChrisJenkins
    Level 7 (21,165 points)
    Mac OS X
    May 30, 2016 3:31 AM in response to ChrisJenkins

    I suggested these as I was writing some AppleScript/Objective-C the other day where the word error was part of a method invocation, and AppleScript threw a fit until I put the word error in its corral. Yours, in retrospect, is a different issue, as Hiroto has pointed out.

  • by red_menace,

    red_menace red_menace May 30, 2016 6:35 AM in response to ChrisJenkins
    Level 6 (15,546 points)
    Desktops
    May 30, 2016 6:35 AM in response to ChrisJenkins

    It works fine on my machine running Yosemite - what kind of result do you get?

     

    tell application "iTunes"
      set aTrack to cloud status of current track --> currently playing track
      log class of aTrack --> constant
      set aTrack to aTrack as text
      log class of aTrack --> text
    end tell
    if aTrack is "unknown" then beep -- status usable outside tell statement
    
  • by ChrisJenkins,

    ChrisJenkins ChrisJenkins May 30, 2016 6:59 AM in response to red_menace
    Level 1 (23 points)
    Apple Music
    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:

    Screen Shot 2016-05-30 at 14.58.23.png

     

    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:

    Screen Shot 2016-05-30 at 14.57.34.png

    So something seems to be broken...

  • by red_menace,Solvedanswer

    red_menace red_menace May 30, 2016 9:51 AM in response to ChrisJenkins
    Level 6 (15,546 points)
    Desktops
    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
    
  • by ChrisJenkins,

    ChrisJenkins ChrisJenkins May 30, 2016 9:52 AM in response to red_menace
    Level 1 (23 points)
    Apple Music
    May 30, 2016 9:52 AM in response to red_menace

    Thanks you so much; that works a treat!

  • by Jacques Rioux,

    Jacques Rioux Jacques Rioux May 30, 2016 10:29 AM in response to ChrisJenkins
    Level 4 (3,418 points)
    Mac OS X
    May 30, 2016 10:29 AM in response to ChrisJenkins

    To avoid value in raw code via the  osascript command:

     

    Save the script in the text format (not compiled)

    osascript /path/to/myscript.applescript
    

     

     

    Or, use the osascript command with the -e switches

    osascript -e 'tell application "iTunes" to (cloud status of current track) as string' -e 'display alert "cloud status: " & result as informational'
    
Page 1 Next