2011-10-06 27 views
2

這裏有一些相似的問題,但沒有涉及NSApplescript。如何在Xcode中使用NSApplescript將藝術品設置爲iTunes歌曲?

我試圖iTunes插圖添加到選定的曲目,我的代碼是:

set newFile to add aFileName as POSIX file to ipod_lib 
set current_track to newFile 
set jpegFilename to ":temp:artwork.jpg" as string 
set data of artwork 1 of current_track to (read (file jpegFilename) as picture) 
tell current_track 
    set name to "Song Name" as string 
    set artist to "Custom Artist" as string 
    set album to "Custom Album" as string 
    set year to "2011" as string 
    set track number to "1" as number 
    set track count to "38" as number 
end tell 

凡aFileName是一個mp3的路徑。 我正在使用Script Debugger 4.5測試它,它工作正常,但是當我將代碼複製到我的Xcode項目並運行它時,不會將其他元數據設置爲藝術作品。 但如果我評論的「設置藝術作品的數據。」行,則它設置其他元數據(名稱,藝術家等)

我的Xcode的代碼是:

NSString *scriptote = @"with timeout of 600 seconds\n" 
    "tell application \"iTunes\"\n" 
... 
    "set newFile to add aFileName as POSIX file to ipod_lib\n" 
    "set current_track to newFile\n" 
    "set jpegFilename to \":temp:artwork.jpg\" as string\n" 
    // If a comment the following line, everything else works, if I left this line, no meta data is set. 
    "set data of artwork 1 of current_track to (read (file jpegFilename) as picture)\n" 
    "tell current_track\n" 
    "set name to \"Song Name\" as string\n" 
    "set artist to \"Custom Artist\" as string\n" 
    "set album to \"Custom Album\" as string\n" 
    "set year to \"2011\" as string\n" 
    "set track number to \"1\" as number\n" 
    "set track count to \"38\" as number\n" 
    "end tell\n" 
...  
; 

    if ((ascript = [[NSAppleScript alloc] initWithSource:scriptote])) { 
     status = [[ascript executeAndReturnError:&errorInfo] stringValue]; 
     if (!errorInfo) { 
      // do something 
      NSLog(@"NO Error"); 
     } else { 
      // process errors 
      // NSRange errorRange = [[errorInfo objectForKey:@"NSAppleScriptErrorRange"] rangeValue]; 
      NSLog(@"Error\n Error line: "); 
     } 
     [ascript release]; 
    } 

我一直谷歌搜索了很多小時,但沒有運氣。 我不想使用ScriptBridge框架,因爲我需要翻譯大量的applescript,所以它現在不適合我。

任何幫助將不勝感激。

回答

3

發現問題,以防萬一有人在未來到達這裏,問題出在HFS路徑格式風格上。

我正在使用NSSearchPathForDirectoriesInDomains獲取我的作品的路徑,然後替換@「:」的所有@「/」來格式化HFS風格的路徑。

問題是HFS路徑格式樣式需要包含卷名稱和NSSearchPathForDirectoriesInDomain的完整路徑,並且任何其他NSFileManager方法返回以/ Users/....開頭的路徑,我需要這樣的路徑:Macintosh HD/Users/...

爲了讓我用下面的代碼的HFS完整路徑:這解決了問題

// Get an HFS-style reference to a specified file 
// (imagePath is an NSString * containing a POSIX-style path to the artwork image file) 
NSURL *fileURL = [NSURL fileURLWithPath:imagePath]; 
NSString *pathFormatted = (NSString *)CFURLCopyFileSystemPath((CFURLRef)fileURL, kCFURLHFSPathStyle); 

,使用pathFormatted字符串設定稿的數據,它應該工作。

希望這會幫助別人有一天 - )

+2

謝謝你謝謝你謝謝你。 – roocell

相關問題