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