# HG changeset patch # User Dan Stowell # Date 1286885090 -3600 # Node ID 726e1c1382f32c83a7f52df649e349ddd519d055 # Parent d0c5831d6528f562a6ab94d0caef9cfdc66aee65 Beginnings of AppleScript querying of iTunes diff -r d0c5831d6528 -r 726e1c1382f3 main/main.cpp --- a/main/main.cpp Mon Oct 11 16:55:24 2010 +0100 +++ b/main/main.cpp Tue Oct 12 13:04:50 2010 +0100 @@ -39,6 +39,7 @@ #include #include #ifdef Q_WS_MAC + #include "osx/svitunes.h" void qt_mac_set_dock_menu(QMenu *menu); // must declare it ourselves, weirdly enough #endif @@ -219,7 +220,11 @@ void setupDockMenu() { std::cerr << "SV adding mac dock menu" << std::endl; QMenu *dockMenu = new QMenu(); - dockMenu->addAction("a SV DOCK action"); + QString theText = iTunesNowPlayingPath(); + if (theText == ""){ + theText = "[[No current track in iTunes]]"; + } + dockMenu->addAction(theText); qt_mac_set_dock_menu(dockMenu); } #endif diff -r d0c5831d6528 -r 726e1c1382f3 osx/svitunes.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/osx/svitunes.h Tue Oct 12 13:04:50 2010 +0100 @@ -0,0 +1,23 @@ +/* + iTunes connection for + Sonic Visualiser + An audio file viewer and annotation editor. + Centre for Digital Music, Queen Mary, University of London. + This file copyright 2010 Dan Stowell and QMUL. + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. See the file + COPYING included with this distribution for more information. +*/ + +#include +#include + +//LATER: bool iTunesRunning(); + +QString iTunesNowPlayingPath(); + +//LATER: QStringList iTunesSelectedPaths(); + diff -r d0c5831d6528 -r 726e1c1382f3 osx/svitunes.mm --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/osx/svitunes.mm Tue Oct 12 13:04:50 2010 +0100 @@ -0,0 +1,62 @@ +/* + iTunes connection for + Sonic Visualiser + An audio file viewer and annotation editor. + Centre for Digital Music, Queen Mary, University of London. + This file copyright 2010 Dan Stowell and QMUL. + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. See the file + COPYING included with this distribution for more information. +*/ + +#include "svitunes.h" + +#include + +#import + +QString qt_mac_NSStringToQString(const NSString *nsstr) +{ + NSRange range; + range.location = 0; + range.length = [nsstr length]; + + unichar *chars = new unichar[range.length + 1]; + chars[range.length] = 0; + [nsstr getCharacters:chars range:range]; + QString result = QString::fromUtf16(chars, range.length); + delete chars; + return result; +} + +QString iTunesNowPlayingPath(){ + NSDictionary *errorDict; + NSAppleScript *scriptObject = [[NSAppleScript alloc] initWithSource:@" \ +tell application \"System Events\" to set iTunesIsRunning to (name of processes) contains \"iTunes\" \n\ +if iTunesIsRunning is false then return \"\" \n\ +return \"twelve-bar blues track\"" + ]; + + NSLog([scriptObject source]); + + [scriptObject compileAndReturnError: &errorDict]; + + if(![scriptObject isCompiled]){ + NSLog(@"SV ERROR: applescript object not compiled"); + NSLog([errorDict description]); + } + + NSAppleEventDescriptor *eventDesc = [scriptObject executeAndReturnError: &errorDict]; + NSString *nsResultString = [eventDesc stringValue]; + + NSLog(@"iTunesNowPlayingPath: "); + NSLog(nsResultString); + + QString resultString = qt_mac_NSStringToQString(nsResultString); + + [scriptObject release]; + return resultString; +} diff -r d0c5831d6528 -r 726e1c1382f3 sonic-visualiser.pro --- a/sonic-visualiser.pro Mon Oct 11 16:55:24 2010 +0100 +++ b/sonic-visualiser.pro Tue Oct 12 13:04:50 2010 +0100 @@ -40,6 +40,10 @@ main/PreferencesDialog.cpp \ main/Surveyer.cpp -# for mac integration -QMAKE_INFO_PLIST = osx/Info.plist +mac { + QMAKE_INFO_PLIST = osx/Info.plist + + OBJECTIVE_SOURCES += osx/svitunes.mm + LIBS += -framework Foundation +}