获取主iTunes资料库的路径并从中播放歌曲

时间:2014-09-09 20:21:34

标签: macos applescript itunes

我目前有一个Applescript,可以让你输入一首歌并播放它。

这是:

    set userInput to text returned of (display dialog "Type something" default answer "")
if userInput contains "Play " then
    set {TID, text item delimiters} to {text item delimiters, {"Play "}}
    if length of userInput is greater than or equal to 2 then set resultString to text item 2 of userInput
    set text item delimiters to TID
    set playSong to (resultString as string)
    tell application "iTunes"
        set mySongs to every track of library playlist 1 whose name is playSong
        repeat with aSong in mySongs
        play aSong
        end repeat

        if (count of mySongs) = 0 then
            say "Song not found"
        end if
    end tell
end if

基本上,我需要获取主iTunes资料库的路径并播放它的歌曲。目前,要搜索歌曲,iTunes必须打开。如果它找不到这首歌,它就会保持开放状态。我想搜索实际的iTunes目录,如果iTunes无法找到一首歌,它就不会打开

我不知道该怎么做。

由于

2 个答案:

答案 0 :(得分:1)

以下是在" iTunes Library.xml "中搜索的脚本。文件。

set XMLFile to my get_iTunes_Library_xml()
set userInput to text returned of (display dialog "Type something" default answer "Play ")
if userInput begins with "Play " and length of userInput > 5 then
    set playSong to text 6 thru -1 of userInput
    set searchString to "<key>Name<\\/key><string>" & playSong & "<\\/string>" --  to match exact name 
    if (my searchTrackName(searchString, XMLFile)) is not "" then
        tell application "iTunes" to play (tracks whose name is playSong)
    else
        say "Song not found"
    end if
end if

on searchTrackName(t, f) -- search in iTunes Library.xml file
    if "&" is in t then set t to do shell script "sed  \"s/&/&#38;/g\" <<<" & quoted form of t -- to replace "&" by "&#38;"
    try -- return a count of matching lines
        return do shell script "grep -c -m1 -i " & (quoted form of t) & " " & f -- "-i" equal case insensitive, "-m 1" to exit at first match
    end try
    return ""
end searchTrackName

on get_iTunes_Library_xml() -- get the path
    do shell script "defaults read com.apple.iApps iTunesRecentDatabases | sed -En 's:^ *\"(.*)\"$:\\1:p' |/usr/bin/perl -MURI -e 'print URI->new(<>)->file;'"
    return quoted form of the result
end get_iTunes_Library_xml

&#34; com.apple.iApps.plist &#34;文件包含您的iTunes库的路径(如果您有多个),脚本将获得&#34; iTunes Library.xml &#34;的路径。当前图书馆的文件。

答案 1 :(得分:0)

不是一个完成的解决方案,但也许是一个起点。

我没有找到自动获取iTunes资料库路径的方法。 使用StandardAdditions,您可以通过

获取主文件夹中主音乐文件夹的路径
set searchPath to POSIX path of music folder

您可以设置路径手册,如下所示:

set searchPath to quoted form of "/Users/USERNAME/Music/iTunes/iTunes Music/"

要搜索没有iTunes的音乐文件,我使用shell命令&#34; mdfind&#34;

do shell script "mdfind -count -onlyin PATH SEARCHSTRING"

使用count-Flag我们得到匹配的总数。如果你想看看mdfind找到了什么,省略count-Flag。

tell application "System Events"
    set userInput to text returned of (display dialog "Search for music" default answer "madonna")
    set searchPath to POSIX path of music folder
end tell

set shellCommand to "mdfind -count -onlyin " & searchPath & " " & quoted form of userInput        
set searchResult to (do shell script shellCommand) as number

if searchResult = 0 then
    say "Song not found"
else if searchResult >= 1 then
    say "Found some songs"
end if

使用mdfind,您可以搜索元数据,例如,您只能搜索MP3文件:

mdfind "kMDItemContentType=='public.mp3'"