annotate osx/svitunes.mm @ 367:726e1c1382f3 macness

Beginnings of AppleScript querying of iTunes
author Dan Stowell <dan.stowell@eecs.qmul.ac.uk>
date Tue, 12 Oct 2010 13:04:50 +0100
parents
children d0c6a13ae6d2
rev   line source
dan@367 1 /*
dan@367 2 iTunes connection for
dan@367 3 Sonic Visualiser
dan@367 4 An audio file viewer and annotation editor.
dan@367 5 Centre for Digital Music, Queen Mary, University of London.
dan@367 6 This file copyright 2010 Dan Stowell and QMUL.
dan@367 7
dan@367 8 This program is free software; you can redistribute it and/or
dan@367 9 modify it under the terms of the GNU General Public License as
dan@367 10 published by the Free Software Foundation; either version 2 of the
dan@367 11 License, or (at your option) any later version. See the file
dan@367 12 COPYING included with this distribution for more information.
dan@367 13 */
dan@367 14
dan@367 15 #include "svitunes.h"
dan@367 16
dan@367 17 #include <Foundation/NSAppleScript.h>
dan@367 18
dan@367 19 #import <Foundation/Foundation.h>
dan@367 20
dan@367 21 QString qt_mac_NSStringToQString(const NSString *nsstr)
dan@367 22 {
dan@367 23 NSRange range;
dan@367 24 range.location = 0;
dan@367 25 range.length = [nsstr length];
dan@367 26
dan@367 27 unichar *chars = new unichar[range.length + 1];
dan@367 28 chars[range.length] = 0;
dan@367 29 [nsstr getCharacters:chars range:range];
dan@367 30 QString result = QString::fromUtf16(chars, range.length);
dan@367 31 delete chars;
dan@367 32 return result;
dan@367 33 }
dan@367 34
dan@367 35 QString iTunesNowPlayingPath(){
dan@367 36 NSDictionary *errorDict;
dan@367 37 NSAppleScript *scriptObject = [[NSAppleScript alloc] initWithSource:@" \
dan@367 38 tell application \"System Events\" to set iTunesIsRunning to (name of processes) contains \"iTunes\" \n\
dan@367 39 if iTunesIsRunning is false then return \"\" \n\
dan@367 40 return \"twelve-bar blues track\""
dan@367 41 ];
dan@367 42
dan@367 43 NSLog([scriptObject source]);
dan@367 44
dan@367 45 [scriptObject compileAndReturnError: &errorDict];
dan@367 46
dan@367 47 if(![scriptObject isCompiled]){
dan@367 48 NSLog(@"SV ERROR: applescript object not compiled");
dan@367 49 NSLog([errorDict description]);
dan@367 50 }
dan@367 51
dan@367 52 NSAppleEventDescriptor *eventDesc = [scriptObject executeAndReturnError: &errorDict];
dan@367 53 NSString *nsResultString = [eventDesc stringValue];
dan@367 54
dan@367 55 NSLog(@"iTunesNowPlayingPath: ");
dan@367 56 NSLog(nsResultString);
dan@367 57
dan@367 58 QString resultString = qt_mac_NSStringToQString(nsResultString);
dan@367 59
dan@367 60 [scriptObject release];
dan@367 61 return resultString;
dan@367 62 }