0

I am using scripting bridge to control Spotify from my osx application.

I am creating an Application Object like so:

var spotify: AnyObject = SBApplication(bundleIdentifier: "com.spotify.client")!

then when I try to do:

spotify.soundVolume = 10

I get the error

 cannot assign to property: 'self' is immutable

What am I doing wrong? I should be able to set the volume according to Spotify's documentation: https://developer.spotify.com/applescript-api/

/*
 * Spotify.h
 */

#import <AppKit/AppKit.h>
#import <ScriptingBridge/ScriptingBridge.h>


@class SpotifyApplication, SpotifyTrack, SpotifyApplication;

enum SpotifyEPlS {
    SpotifyEPlSStopped = 'kPSS',
    SpotifyEPlSPlaying = 'kPSP',
    SpotifyEPlSPaused = 'kPSp'
};
typedef enum SpotifyEPlS SpotifyEPlS;



/*
 * Spotify Suite
 */

// The Spotify application.
@interface SpotifyApplication : SBApplication

@property (copy, readonly) SpotifyTrack *currentTrack;  // The current playing track.
@property NSInteger soundVolume;  // The sound output volume (0 = minimum, 100 = maximum)
@property (readonly) SpotifyEPlS playerState;  // Is Spotify stopped, paused, or playing?
@property double playerPosition;  // The player’s position within the currently playing track in seconds.
@property (readonly) BOOL repeatingEnabled;  // Is repeating enabled in the current playback context?
@property BOOL repeating;  // Is repeating on or off?
@property (readonly) BOOL shufflingEnabled;  // Is shuffling enabled in the current playback context?
@property BOOL shuffling;  // Is shuffling on or off?

- (void) nextTrack;  // Skip to the next track.
- (void) previousTrack;  // Skip to the previous track.
- (void) playpause;  // Toggle play/pause.
- (void) pause;  // Pause playback.
- (void) play;  // Resume playback.
- (void) playTrack:(NSString *)x inContext:(NSString *)inContext;  // Start playback of a track in the given context.

@end

// A Spotify track.
@interface SpotifyTrack : SBObject

@property (copy, readonly) NSString *artist;  // The artist of the track.
@property (copy, readonly) NSString *album;  // The album of the track.
@property (readonly) NSInteger discNumber;  // The disc number of the track.
@property (readonly) NSInteger duration;  // The length of the track in seconds.
@property (readonly) NSInteger playedCount;  // The number of times this track has been played.
@property (readonly) NSInteger trackNumber;  // The index of the track in its album.
@property (readonly) BOOL starred;  // Is the track starred?
@property (readonly) NSInteger popularity;  // How popular is this track? 0-100
- (NSString *) id;  // The ID of the item.
@property (copy, readonly) NSString *name;  // The name of the track.
@property (copy, readonly) NSString *artworkUrl;  // The URL of the track%apos;s album cover.
@property (copy, readonly) NSImage *artwork;  // The property is deprecated and will never be set. Use the 'artwork url' instead.
@property (copy, readonly) NSString *albumArtist;  // That album artist of the track.
@property (copy) NSString *spotifyUrl;  // The URL of the track.


@end



/*
 * Standard Suite
 */

// The application's top level scripting object.
@interface SpotifyApplication (StandardSuite)

@property (copy, readonly) NSString *name;  // The name of the application.
@property (readonly) BOOL frontmost;  // Is this the frontmost (active) application?
@property (copy, readonly) NSString *version;  // The version of the application.

@end
user3916570
  • 780
  • 1
  • 9
  • 23
  • Show your swift code. – Alexander Jul 04 '16 at 18:43
  • I showed the only 2 relevant lines...? – user3916570 Jul 04 '16 at 18:45
  • Where are you executing `spotify.soundVolume = 10`? Most likely you are performing this attempted mutation in a method of a _structure_ (`struct`), in case you might need to mark that method as `mutating` (`mutating func ...`). – dfrib Jul 04 '16 at 18:48
  • @user3916570 For the method in which you are (presumable) mutating `spotify.soundVolume` (you must perform this mutation within some _scope_, presumably in a method), you mark this method with the keyword `mutating` (e.g. if your method is name `func foo() { ... }` you mark it as `mutating foo() { ... }`). Note also that all the code posted in the large code block above is totally irrelevant in helping us attempt to help you. Please post the part of your code _where you attempt to mutate `spotify.soundVolume`). – dfrib Jul 04 '16 at 18:55

1 Answers1

1

It turns out a viable option is to actually do something like this:

spotify.setValue(sender.integerValue, forKey: "soundVolume")

user3916570
  • 780
  • 1
  • 9
  • 23