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@370
|
35 QStringList iTunesNowPlaying(){
|
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@368
|
40 \
|
dan@368
|
41 tell application \"iTunes\" \n\
|
dan@368
|
42 if player state is not stopped then \n\
|
dan@368
|
43 set aTrack to current track \n\
|
dan@368
|
44 else \n\
|
dan@368
|
45 set sel to selection \n\
|
dan@368
|
46 if sel is not {} then --and (length of sel) is 1 then \n\
|
dan@368
|
47 set aTrack to item 1 of sel \n\
|
dan@368
|
48 else \n\
|
dan@368
|
49 return \"\" \n\
|
dan@368
|
50 end if \n\
|
dan@368
|
51 end if \n\
|
dan@368
|
52 \
|
dan@370
|
53 return the POSIX path of (location of aTrack as text) & \"\n\" & (genre of aTrack) \n\
|
dan@368
|
54 end tell \n\
|
dan@368
|
55 "
|
dan@367
|
56 ];
|
dan@367
|
57
|
dan@367
|
58 NSLog([scriptObject source]);
|
dan@367
|
59
|
dan@367
|
60 [scriptObject compileAndReturnError: &errorDict];
|
dan@367
|
61
|
dan@367
|
62 if(![scriptObject isCompiled]){
|
dan@367
|
63 NSLog(@"SV ERROR: applescript object not compiled");
|
dan@367
|
64 NSLog([errorDict description]);
|
dan@367
|
65 }
|
dan@367
|
66
|
dan@367
|
67 NSAppleEventDescriptor *eventDesc = [scriptObject executeAndReturnError: &errorDict];
|
dan@367
|
68 NSString *nsResultString = [eventDesc stringValue];
|
dan@367
|
69
|
dan@367
|
70 NSLog(@"iTunesNowPlayingPath: ");
|
dan@367
|
71 NSLog(nsResultString);
|
dan@367
|
72
|
dan@367
|
73 QString resultString = qt_mac_NSStringToQString(nsResultString);
|
dan@367
|
74
|
dan@367
|
75 [scriptObject release];
|
dan@370
|
76 return resultString.split(QChar('\n'));
|
dan@367
|
77 }
|