将HEX iTunes Persistent ID转换为高和低32位格式

时间:2011-01-26 17:09:27

标签: javascript itunes hex

在Mac上,我可以从“iTunes Music Library.xml”文件中提取特定歌曲的“持久ID”,然后使用Applescript播放该歌曲:

tell application "iTunes"   
    set thePersistentId to "F040658A7687B12D"
    set theSong to (some track of playlist "Music" whose persistent ID is thePersistentId)  
    play theSong with once  
end tell  

在PC上,我可以用同样的方式从XML文件中提取“持久ID”。 iTunes COM接口的文档说“ItemByPersistentId”函数有两个参数:“highID”(64位持久ID的高32位)和“lowID”(64位持久ID的低32位) 。但我无法弄清楚如何将基于十六进制的值转换为ItemByPersistentId函数所需的高和低32位值。

var thePersistentId = "F040658A7687B12D";  
var iTunes = WScript.CreateObject("iTunes.Application"); 
var n = parseInt(thePersistentId);  
var high = (do something with n?);  
var low = (do something else with n?);  
iTunes.LibraryPlaylist.tracks.ItemByPersistentId(high,low).play();  

1 个答案:

答案 0 :(得分:1)

我不知道Windows脚本主机上的情况是什么,但是有些JS客户端有32位int,所以你的var n = parseInt(thePersistentId);更加麻烦。在使用parseInt时,您应始终包含基数(例如parseInt(hexValue, 16)parseInt(octValue, 8))。

为避免32位脚本解释器限制,您可以单独提取低32位和高32位。每个十六进制数字是4位,所以低32位是持久ID的最后8​​个字符,高32位是剩余8位(除非它有0x前缀,那么它是下一个最右边的一个块8个字符)。

var hexId = "XXXXX";   
var iTunes = WScript.CreateObject("iTunes.Application");  

//the following two statements assume you have a valid 64 bit hex, 
//you may want to verify the length of the string

//grab and parse the last 8 characters of your string
var low = parseInt(hexId.substr(hexId.length - 8), 16);
//grab and parse the next last 8 characters of your string
var high = parseInt(hexId.substr(hexId.length - 16, 8), 16);
iTunes.LibraryPlaylist.tracks.ItemByPersistentId(high,low).play(); 

编辑:从我在iTuner source code中读到的内容来看,highID实际上是高4字节,lowID低4字节,而不是8字节(因此丢弃persistentID ...的中间8个字节。这是一个经过修改的尝试:

//assumes hex strings with no "0x" prefix
//grab and parse the last 4 characters of your string
var low = parseInt(hexId.substr(hexId.length - 4), 16);
//grab, pad and parse the first 4 characters of your string
var high = parseInt(hexId.substr(0, 4) + "0000", 16);